001 ///////////////////////////////////////////////////////////////////////////////
002 // Copyright (c) 2006, Frank S. Nestel, All Rights Reserved.
003 //
004 // This library is free software; you can redistribute it and/or
005 // modify it under the terms of the GNU Lesser General Public
006 // License as published by the Free Software Foundation; either
007 // version 2.1 of the License, or (at your option) any later version.
008 //
009 // This library is distributed in the hope that it will be useful,
010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 // GNU General Public License for more details.
013 //
014 // You should have received a copy of the GNU Lesser General Public
015 // License along with this program; if not, write to the Free Software
016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017 ///////////////////////////////////////////////////////////////////////////////
018
019 package de.spieleck.app.turn.rendering;
020
021 import java.io.IOException;
022 import java.io.File;
023 import java.io.FileOutputStream;
024
025 import com.thoughtworks.xstream.XStream;
026 import com.thoughtworks.xstream.io.xml.DomDriver;
027 import com.thoughtworks.xstream.converters.Converter;
028 import com.thoughtworks.xstream.converters.MarshallingContext;
029 import com.thoughtworks.xstream.converters.UnmarshallingContext;
030 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
031 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
032
033 import de.spieleck.app.turn.scoring.Mue.MueScore;
034 import de.spieleck.app.turn.scoring.Zoff.ZoffScore;
035 import de.spieleck.app.turn.scoring.Chess.ChessScore;
036 import de.spieleck.app.turn.Player;
037 import de.spieleck.app.turn.PlayerRegistry;
038 import de.spieleck.app.turn.pairing.GameImpl;
039 import de.spieleck.app.turn.GameResult;
040 import de.spieleck.app.turn.ScoringMode;
041
042 /**
043 * A facade to the XStream library.
044 *
045 * <p><a href="$URL: https://svn.sourceforge.net/svnroot/jtourney/src/de/spieleck/app/turn/rendering/XStreamBuilder.java $">$URL: https://svn.sourceforge.net/svnroot/jtourney/src/de/spieleck/app/turn/rendering/XStreamBuilder.java $</a></p>
046 *
047 * @author Frank S. Nestel
048 * @author $Author: nestefan $
049 * @version $Revision: 2 $ $Date: 2006-03-20 14:33:27 +0100 (Mo, 20 Mrz 2006) $ $Author: nestefan $
050 */
051 public class XStreamBuilder
052 {
053 public final static XStream XSTREAM;
054
055 static
056 {
057 XSTREAM = new XStream(new DomDriver());
058 a("player", Player.class);
059 // a("game", Game.class);
060 a("game", GameImpl.class);
061 a("gameresult", GameResult.class);
062 a("mue-score", MueScore.class);
063 a("zoff-score", ZoffScore.class);
064 a("chess-score", ChessScore.class);
065 // XSTREAM.omitField(Player.class, "id");
066 XSTREAM.omitField(MueScore.class, "outer-class");
067 XSTREAM.omitField(MueScore.class, "outer.class");
068 XSTREAM.omitField(Player.class, "registry");
069 XSTREAM.omitField(Player.class, "gameResults");
070 XSTREAM.omitField(Player.class, "opponents");
071 // Tut so noch nix!!
072 XSTREAM.registerConverter(new Converter()
073 {
074 public boolean canConvert(Class clazz)
075 {
076 // System.out.println("++"+clazz);
077 return clazz == Integer.TYPE;
078 }
079
080 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context)
081 {
082 // System.out.println("--"+source);
083 }
084
085 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context)
086 {
087 return null;
088 }
089
090
091 }
092 );
093 }
094
095 private final static void a(String n, Class clazz)
096 {
097 XSTREAM.alias(n, clazz);
098 }
099
100 public static XStream getXStream()
101 {
102 return XSTREAM;
103 }
104
105 public static void toXML(Object o, File fi)
106 throws IOException
107 {
108 XSTREAM.toXML(o, new FileOutputStream(fi));
109 }
110
111 public static void main(String[] args)
112 {
113 String xml;
114 ScoringMode sm = new de.spieleck.app.turn.scoring.Mue();
115 PlayerRegistry pr = new Player.StandardPlayerRegistry(sm);
116 Player p = pr.registerPlayer("shortName", "name", "remark");
117 xml = XSTREAM.toXML(p);
118 System.out.println(xml);
119 }
120
121 }