/Prototipo/Servlet/lib/xstream-distribution-1.4.1-bin/xstream-1.4.1/docs/graphs.html

http://prototipomemoria.googlecode.com/ · HTML · 326 lines · 246 code · 63 blank · 17 comment · 0 complexity · 032f8f73fea4cd1b93eb2877091b3c8e MD5 · raw file

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <!--
  4. Copyright (C) 2005, 2006 Joe Walnes.
  5. Copyright (C) 2006, 2007, 2008 XStream committers.
  6. All rights reserved.
  7. The software in this package is published under the terms of the BSD
  8. style license a copy of which has been included with this distribution in
  9. the LICENSE.txt file.
  10. Created on 29. January 2005 by Joe Walnes
  11. -->
  12. <head>
  13. <title>XStream - Object references</title>
  14. <link rel="stylesheet" type="text/css" href="style.css"/>
  15. <!-- Google analytics -->
  16. <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
  17. </script>
  18. <script type="text/javascript">
  19. _uacct = "UA-110973-2";
  20. urchinTracker();
  21. </script>
  22. </head>
  23. <body>
  24. <div id="banner">
  25. <a href="index.html"><img id="logo" src="logo.gif" alt="XStream"/></a>
  26. </div>
  27. <div id="center" class="Content2Column"> <!-- Content3Column for index -->
  28. <div id="content">
  29. <h1 class="FirstChild">Object references</h1>
  30. <!-- ...................................................... -->
  31. <h2 id="references">How does XStream deals with duplicate and circular references?</h2>
  32. <p>It depends on XStream's mode, the default is uses XPath to allow serialized objects to be treated as graphs instead of simple trees (typical XML usage).</p>
  33. <p>Sometimes it's not desirable to work this way, let's take a look in a simple example in order to switch between XStream's modes and see what its capable of.</p>
  34. <p>We start with a simple compact-disc class, with id and a bonus cd fields:</p>
  35. <div class="Source Java"><pre>package com.thoughtworks.xstream;
  36. public class Cd {
  37. private String id;
  38. private Cd bonusCd;
  39. Cd(String id, Cd bonusCd) {
  40. this.id = id;
  41. this.bonusCd = bonusCd;
  42. }
  43. Cd(String id) {
  44. this.id = id;
  45. }
  46. public String getId() {
  47. return id;
  48. }
  49. public Cd getBonusCd() {
  50. return bonusCd;
  51. }
  52. }</pre></div>
  53. <p>And let's create an order with the same cd twice and the order itself, so we can simulate
  54. two references to the same object and a back-reference.</p>
  55. <div class="Source Java"><pre>Cd bj = new Cd("basement_jaxx_singles");
  56. List order = new ArrayList();
  57. // adds the same cd twice (two references to the same object)
  58. order.add(bj);
  59. order.add(bj);
  60. // adds itself (cycle)
  61. order.add(order);
  62. XStream xstream = new XStream();
  63. xstream.alias("cd", Cd.class);
  64. System.out.println(xstream.toXML(order));
  65. </pre></div>
  66. <p>If we execute the above code, XStream's uses its <b>default mode</b> called XPATH_RELATIVE_REFERENCES
  67. based on the W3C XPath specification. Cross and back references are treated in a way that it's (almost) human
  68. readable:</p>
  69. <div class="Source Java"><pre>
  70. &lt;list&gt;
  71. &lt;cd&gt;
  72. &lt;id&gt;maria rita&lt;/id&gt;
  73. &lt;/cd&gt;
  74. &lt;cd&gt;
  75. &lt;id&gt;basement_jaxx_singles&lt;/id&gt;
  76. &lt;/cd&gt;
  77. &lt;cd reference="../cd[2]"/&gt;
  78. &lt;list reference=".."/&gt;
  79. &lt;/list&gt;
  80. </pre></div>
  81. <p>The second reference to the Basement Jaxx cd was serialized as "../cd[2]" while the order inside
  82. itself used the ".." path. The XPath Relative mode allows any type of graphs to be used as both cross and
  83. back-references are supported.</p>
  84. <p>In order to make use of the XPath Relative mode one can implicitly call:</p>
  85. <div class="Source Java"><pre>
  86. xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
  87. </pre></div>
  88. <!-- ...................................................... -->
  89. <h2 id="rel-abs">Relative x Absolute</h2>
  90. <p>There is an absolute mode which is easy to use and understand. It works
  91. using the same XPath specification and also supports all types of graphs.</p>
  92. <div class="Source Java"><pre>
  93. xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
  94. </pre></div>
  95. <p>The changes in the resulting xml is a little more 'clutter':</p>
  96. <div class="Source Java"><pre>
  97. &lt;list&gt;
  98. &lt;cd&gt;
  99. &lt;id&gt;maria rita&lt;/id&gt;
  100. &lt;/cd&gt;
  101. &lt;cd&gt;
  102. &lt;id&gt;basement_jaxx_singles&lt;/id&gt;
  103. &lt;/cd&gt;
  104. &lt;cd reference="/list/cd[2]"/&gt;
  105. &lt;list reference="/list"/&gt;
  106. &lt;/list&gt;
  107. </pre></div>
  108. <!-- ...................................................... -->
  109. <h2 id="single-node">Single Node Selectors</h2>
  110. <p>In some cases where the XML is used later on or is generated by someone else, the
  111. XPath selectors can be forced to select always a single node instead of a node list where
  112. the first element is taken. Therefore two more modes exist:</p>
  113. <div class="Source Java"><pre>
  114. xstream.setMode(XStream.SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES);
  115. xstream.setMode(XStream.SINGLE_NODE_XPATH_RELATIVE_REFERENCES);
  116. </pre></div>
  117. <p>The changes in the resulting xml is even more 'clutter':</p>
  118. <div class="Source Java"><pre>
  119. &lt;list&gt;
  120. &lt;cd&gt;
  121. &lt;id&gt;maria rita&lt;/id&gt;
  122. &lt;/cd&gt;
  123. &lt;cd&gt;
  124. &lt;id&gt;basement_jaxx_singles&lt;/id&gt;
  125. &lt;/cd&gt;
  126. &lt;cd reference="/list[1]/cd[2]"/&gt;
  127. &lt;list reference="/list[1]"/&gt;
  128. &lt;/list&gt;
  129. </pre></div>
  130. <p>For XStream is the notation with the single node selectors absolutely equivalent to the
  131. one without. These two notations are completely transparent at deserialization time.</p>
  132. <!-- ...................................................... -->
  133. <h2 id="ids">Id mode</h2>
  134. <p>Both modes displayed until now are not so easy to write by hand. XStream has another
  135. mode which makes it is easier to read/write by a human being:</p>
  136. <div class="Source Java"><pre>
  137. xstream.setMode(XStream.ID_REFERENCES);
  138. </pre></div>
  139. <p>The result is a XML which generates an "id" attribute for each new object marshaled,
  140. and whenever it finds back or cross-references, it uses a "reference" attribute to so
  141. it doesn't copy the entire object.</p>
  142. <p>In our example, the list has id 1, the Maria Rita cd 2, and the Basement Jaxx 3. Therefore
  143. the cross-reference to our cd should contain a reference attribute to object number 2 and
  144. the back-reference to our order a reference to object 1. The result is:</p>
  145. <div class="Source Java"><pre>
  146. &lt;list id="1"&gt;
  147. &lt;cd id="2"&gt;
  148. &lt;id&gt;maria rita&lt;/id&gt;
  149. &lt;/cd&gt;
  150. &lt;cd id="3"&gt;
  151. &lt;id&gt;basement_jaxx_singles&lt;/id&gt;
  152. &lt;/cd&gt;
  153. &lt;cd reference="3"/&gt;
  154. &lt;list reference="1"/&gt;
  155. &lt;/list&gt;
  156. </pre></div>
  157. <!-- ...................................................... -->
  158. <h2>No references</h2>
  159. <p>For some uses of XStream we do not desire any kind of back or cross references like a graph, but a simple
  160. tree. The most famous example is when using XStream to generate XML for B2B services.</p>
  161. <p>In such scenarios it's impossible to represent a graph cycle (remember: no tree contains such structure), therefore
  162. we have to remove the last <b>add</b> call from our example:</p>
  163. <div class="Source Java"><pre>Cd bj = new Cd("basement_jaxx_singles");
  164. List order = new ArrayList();
  165. // adds the same cd twice (two references to the same object)
  166. order.add(bj);
  167. order.add(bj);
  168. XStream xstream = new XStream();
  169. xstream.alias("cd", Cd.class);
  170. System.out.println(xstream.toXML(order));
  171. </pre></div>
  172. <p>Now if we add the NO_REFERENCES option, every reference shall be completed serialized.</p>
  173. <div class="Source Java"><pre>
  174. xstream.setMode(XStream.NO_REFERENCES);
  175. </pre></div>
  176. <p>The result are three references:</p>
  177. <div class="Source Java"><pre>
  178. &lt;list&gt;
  179. &lt;cd&gt;
  180. &lt;id&gt;maria rita&lt;/id&gt;
  181. &lt;/cd&gt;
  182. &lt;cd&gt;
  183. &lt;id&gt;basement_jaxx_singles&lt;/id&gt;
  184. &lt;/cd&gt;
  185. &lt;cd&gt;
  186. &lt;id&gt;basement_jaxx_singles&lt;/id&gt;
  187. &lt;/cd&gt;
  188. &lt;/list&gt;
  189. </pre></div>
  190. <p>After reading from the above XML, you will get three <b>different</b> Cd instances.</p>
  191. <p>Remember: it's impossible to support back-references (cycles) with NO_REFERENCES mode activated therefore
  192. a CircularReferenceException is thrown if you ever try to do so.</p>
  193. <br/>
  194. </div>
  195. </div>
  196. <div class="SidePanel" id="left">
  197. <div class="MenuGroup">
  198. <h1>Software</h1>
  199. <ul>
  200. <li><a href="index.html">About XStream</a></li>
  201. <li><a href="news.html">News</a></li>
  202. <li><a href="changes.html">Change History</a></li>
  203. <li><a href="versioning.html">About Versioning</a></li>
  204. </ul>
  205. </div>
  206. <div class="MenuGroup">
  207. <h1>Evaluating XStream</h1>
  208. <ul>
  209. <li><a href="tutorial.html">Two Minute Tutorial</a></li>
  210. <li class="currentLink">Object references</li>
  211. <li><a href="manual-tweaking-output.html">Tweaking the Output</a></li>
  212. <li><a href="license.html">License</a></li>
  213. <li><a href="download.html">Download</a></li>
  214. <li><a href="references.html">References</a></li>
  215. <li><a href="parser-benchmarks.html">Parser Benchmarks</a></li>
  216. <li><a href="http://www.ohloh.net/projects/3459">Code Statistics</a></li>
  217. </ul>
  218. </div>
  219. <div class="MenuGroup">
  220. <h1>Using XStream</h1>
  221. <ul>
  222. <li><a href="architecture.html">Architecture Overview</a></li>
  223. <li><a href="converters.html">Converters</a></li>
  224. <li><a href="faq.html">Frequently Asked Questions</a></li>
  225. <li><a href="list-user.html">Users' Mailing List</a></li>
  226. <li><a href="issues.html">Reporting Issues</a></li>
  227. </ul>
  228. </div>
  229. <div class="MenuGroup">
  230. <h1>Javadoc</h1>
  231. <ul>
  232. <li><a href="javadoc/index.html">XStream Core</a></li>
  233. <li><a href="hibernate-javadoc/index.html">Hibernate Extensions</a></li>
  234. <li><a href="benchmark-javadoc/index.html">Benchmark Module</a></li>
  235. </ul>
  236. </div>
  237. <div class="MenuGroup">
  238. <h1>Tutorials</h1>
  239. <ul>
  240. <li><a href="tutorial.html">Two Minute Tutorial</a></li>
  241. <li><a href="alias-tutorial.html">Alias Tutorial</a></li>
  242. <li><a href="annotations-tutorial.html">Annotations Tutorial</a></li>
  243. <li><a href="converter-tutorial.html">Converter Tutorial</a></li>
  244. <li><a href="objectstream.html">Object Streams Tutorial</a></li>
  245. <li><a href="persistence-tutorial.html">Persistence API Tutorial</a></li>
  246. <li><a href="json-tutorial.html">JSON Tutorial</a></li>
  247. </ul>
  248. </div>
  249. <div class="MenuGroup">
  250. <h1>Developing XStream</h1>
  251. <ul>
  252. <li><a href="how-to-contribute.html">How to Contribute</a></li>
  253. <li><a href="list-dev.html">Developers' Mailing List</a></li>
  254. <li><a href="team.html">Development Team</a></li>
  255. <li><a href="repository.html">Source Repository</a></li>
  256. <li><a href="http://bamboo.ci.codehaus.org/browse/XSTREAM">Continuous Integration</a></li>
  257. </ul>
  258. </div>
  259. </div>
  260. </body>
  261. </html>