Advertisement
Guest User

XStream serialization

a guest
Jul 10th, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. XSTest.java:
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6.  
  7. import com.thoughtworks.xstream.XStream;
  8.  
  9. public class XSTest {
  10. private static final String XML_FILE = "c:\\temp\\items.xml";
  11. private XStream xs;
  12.  
  13. public XStream getXS()
  14. {
  15. if(xs == null)
  16. {
  17. xs = new XStream();
  18. xs.processAnnotations(Store.class);
  19. xs.processAnnotations(Item.class);
  20. xs.processAnnotations(Weapon.class);
  21. xs.processAnnotations(Armor.class);
  22. }
  23.  
  24. return xs;
  25. }
  26.  
  27. public void createModel()
  28. {
  29. Store s = new Store();
  30. s.items.add(new Weapon(1, "Short sword", "data/items/shortsword.png", "swords", "data/sprites/swordattack.png"));
  31. s.items.add(new Weapon(2, "Bastard sword", "data/items/bastardsword.png", "swords", "data/sprites/swordattack.png"));
  32. s.items.add(new Weapon(3, "Long sword", "data/items/longsword.png", "swords", "data/sprites/swordattack.png"));
  33. s.items.add(new Armor(4, "Studded leather armor", "data/items/studdedleather.png", "Chest", 20));
  34.  
  35. save(s, XML_FILE);
  36. }
  37.  
  38. public Store loadStore()
  39. {
  40. XStream xs = getXS();
  41. return (Store) xs.fromXML(new File(XML_FILE));
  42. }
  43.  
  44. public boolean save(Object obj, String location)
  45. {
  46. try
  47. {
  48. File f = new File(location);
  49. File dir = f.getParentFile();
  50. if(!dir.exists())
  51. dir.mkdirs();
  52. XStream xs = getXS();
  53. String xml = xs.toXML(obj);
  54. FileWriter fstream = new FileWriter(f);
  55. BufferedWriter out = new BufferedWriter(fstream);
  56. out.write(xml);
  57. out.close();
  58. System.out.println(String.format("Generated xml '%s':\n%s", location, xml));
  59. return true;
  60. }
  61. catch(Exception ex)
  62. {
  63. return false;
  64. }
  65. }
  66.  
  67. public static void main(String[] args)
  68. {
  69. XSTest test = new XSTest();
  70. test.createModel();
  71. Store s = test.loadStore();
  72.  
  73. for(Item i : s.items)
  74. {
  75. System.out.println(i.toString());
  76. }
  77. }
  78. }
  79.  
  80. Store.java:
  81.  
  82. import java.util.ArrayList;
  83.  
  84. import com.thoughtworks.xstream.annotations.XStreamAlias;
  85.  
  86. @XStreamAlias("store")
  87. public class Store {
  88.  
  89. public ArrayList<Item> items;
  90.  
  91. public Store()
  92. {
  93. items = new ArrayList<Item>();
  94. }
  95. }
  96.  
  97. Item.java:
  98.  
  99. import com.thoughtworks.xstream.annotations.XStreamAlias;
  100.  
  101. @XStreamAlias("item")
  102. public abstract class Item {
  103. public int id;
  104. public String name;
  105. public String inventorySheet;
  106.  
  107. public Item()
  108. {
  109.  
  110. }
  111. public Item(int id, String name, String inventorySheet)
  112. {
  113. this.id = id;
  114. this.name = name;
  115. this.inventorySheet = inventorySheet;
  116. }
  117.  
  118. public abstract String getTypeName();
  119.  
  120. @Override
  121. public String toString()
  122. {
  123. return String.format("%s (%d): %s", getTypeName(), id, name);
  124. }
  125. }
  126.  
  127. Armor.java:
  128.  
  129.  
  130. import com.thoughtworks.xstream.annotations.XStreamAlias;
  131.  
  132. @XStreamAlias("armor")
  133. public class Armor extends Item{
  134. public String armorType;
  135. public int armorValue;
  136.  
  137. public Armor()
  138. {
  139.  
  140. }
  141. public Armor(int id, String name, String sheet, String type, int value)
  142. {
  143. super(id, name, sheet);
  144. this.armorType = type;
  145. this.armorValue = value;
  146. }
  147. @Override
  148. public String getTypeName() {
  149. return "Armor";
  150. }
  151.  
  152. @Override
  153. public String toString()
  154. {
  155. return String.format("%s, type: %s, armor value: %d", super.toString(), armorType, armorValue);
  156. }
  157. }
  158.  
  159. Weapon.java:
  160.  
  161.  
  162. import com.thoughtworks.xstream.annotations.XStreamAlias;
  163.  
  164. @XStreamAlias("weapon")
  165. public class Weapon extends Item{
  166. public String weaponType;
  167. public String attackSheet;
  168.  
  169. public Weapon(){}
  170. public Weapon(int id, String name, String inventorySheet, String type, String attackSheet )
  171. {
  172. super(id, name, inventorySheet);
  173. this.weaponType = type;
  174. this.attackSheet = attackSheet;
  175. }
  176. @Override
  177. public String getTypeName() {
  178. return "Weapon";
  179. }
  180.  
  181. @Override
  182. public String toString()
  183. {
  184. return String.format("%s, type: %s, attack sheet: %s", super.toString(), weaponType, attackSheet);
  185. }
  186. }
  187.  
  188. The generated XML:
  189. <store>
  190. <items>
  191. <weapon>
  192. <id>1</id>
  193. <name>Short sword</name>
  194. <inventorySheet>data/items/shortsword.png</inventorySheet>
  195. <weaponType>swords</weaponType>
  196. <attackSheet>data/sprites/swordattack.png</attackSheet>
  197. </weapon>
  198. <weapon>
  199. <id>2</id>
  200. <name>Bastard sword</name>
  201. <inventorySheet>data/items/bastardsword.png</inventorySheet>
  202. <weaponType>swords</weaponType>
  203. <attackSheet>data/sprites/swordattack.png</attackSheet>
  204. </weapon>
  205. <weapon>
  206. <id>3</id>
  207. <name>Long sword</name>
  208. <inventorySheet>data/items/longsword.png</inventorySheet>
  209. <weaponType>swords</weaponType>
  210. <attackSheet>data/sprites/swordattack.png</attackSheet>
  211. </weapon>
  212. <armor>
  213. <id>4</id>
  214. <name>Studded leather armor</name>
  215. <inventorySheet>data/items/studdedleather.png</inventorySheet>
  216. <armorType>Chest</armorType>
  217. <armorValue>20</armorValue>
  218. </armor>
  219. </items>
  220. </store>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement