/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
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <!--
- Copyright (C) 2005, 2006 Joe Walnes.
- Copyright (C) 2006, 2007, 2008 XStream committers.
- All rights reserved.
-
- The software in this package is published under the terms of the BSD
- style license a copy of which has been included with this distribution in
- the LICENSE.txt file.
-
- Created on 29. January 2005 by Joe Walnes
- -->
- <head>
- <title>XStream - Object references</title>
- <link rel="stylesheet" type="text/css" href="style.css"/>
-
-
-
- <!-- Google analytics -->
- <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
- </script>
- <script type="text/javascript">
- _uacct = "UA-110973-2";
- urchinTracker();
- </script>
- </head>
- <body>
- <div id="banner">
- <a href="index.html"><img id="logo" src="logo.gif" alt="XStream"/></a>
- </div>
- <div id="center" class="Content2Column"> <!-- Content3Column for index -->
- <div id="content">
- <h1 class="FirstChild">Object references</h1>
-
- <!-- ...................................................... -->
- <h2 id="references">How does XStream deals with duplicate and circular references?</h2>
- <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>
-
- <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>
- <p>We start with a simple compact-disc class, with id and a bonus cd fields:</p>
-
- <div class="Source Java"><pre>package com.thoughtworks.xstream;
- public class Cd {
- private String id;
- private Cd bonusCd;
- Cd(String id, Cd bonusCd) {
- this.id = id;
- this.bonusCd = bonusCd;
- }
- Cd(String id) {
- this.id = id;
- }
- public String getId() {
- return id;
- }
- public Cd getBonusCd() {
- return bonusCd;
- }
- }</pre></div>
- <p>And let's create an order with the same cd twice and the order itself, so we can simulate
- two references to the same object and a back-reference.</p>
-
- <div class="Source Java"><pre>Cd bj = new Cd("basement_jaxx_singles");
-
- List order = new ArrayList();
- // adds the same cd twice (two references to the same object)
- order.add(bj);
- order.add(bj);
- // adds itself (cycle)
- order.add(order);
- XStream xstream = new XStream();
- xstream.alias("cd", Cd.class);
- System.out.println(xstream.toXML(order));
- </pre></div>
- <p>If we execute the above code, XStream's uses its <b>default mode</b> called XPATH_RELATIVE_REFERENCES
- based on the W3C XPath specification. Cross and back references are treated in a way that it's (almost) human
- readable:</p>
- <div class="Source Java"><pre>
- <list>
- <cd>
- <id>maria rita</id>
- </cd>
- <cd>
- <id>basement_jaxx_singles</id>
- </cd>
- <cd reference="../cd[2]"/>
- <list reference=".."/>
- </list>
- </pre></div>
- <p>The second reference to the Basement Jaxx cd was serialized as "../cd[2]" while the order inside
- itself used the ".." path. The XPath Relative mode allows any type of graphs to be used as both cross and
- back-references are supported.</p>
- <p>In order to make use of the XPath Relative mode one can implicitly call:</p>
-
- <div class="Source Java"><pre>
- xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
- </pre></div>
- <!-- ...................................................... -->
- <h2 id="rel-abs">Relative x Absolute</h2>
-
- <p>There is an absolute mode which is easy to use and understand. It works
- using the same XPath specification and also supports all types of graphs.</p>
- <div class="Source Java"><pre>
- xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
- </pre></div>
- <p>The changes in the resulting xml is a little more 'clutter':</p>
- <div class="Source Java"><pre>
- <list>
- <cd>
- <id>maria rita</id>
- </cd>
- <cd>
- <id>basement_jaxx_singles</id>
- </cd>
- <cd reference="/list/cd[2]"/>
- <list reference="/list"/>
- </list>
- </pre></div>
- <!-- ...................................................... -->
- <h2 id="single-node">Single Node Selectors</h2>
- <p>In some cases where the XML is used later on or is generated by someone else, the
- XPath selectors can be forced to select always a single node instead of a node list where
- the first element is taken. Therefore two more modes exist:</p>
- <div class="Source Java"><pre>
- xstream.setMode(XStream.SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES);
- xstream.setMode(XStream.SINGLE_NODE_XPATH_RELATIVE_REFERENCES);
- </pre></div>
- <p>The changes in the resulting xml is even more 'clutter':</p>
- <div class="Source Java"><pre>
- <list>
- <cd>
- <id>maria rita</id>
- </cd>
- <cd>
- <id>basement_jaxx_singles</id>
- </cd>
- <cd reference="/list[1]/cd[2]"/>
- <list reference="/list[1]"/>
- </list>
- </pre></div>
- <p>For XStream is the notation with the single node selectors absolutely equivalent to the
- one without. These two notations are completely transparent at deserialization time.</p>
- <!-- ...................................................... -->
- <h2 id="ids">Id mode</h2>
-
- <p>Both modes displayed until now are not so easy to write by hand. XStream has another
- mode which makes it is easier to read/write by a human being:</p>
- <div class="Source Java"><pre>
- xstream.setMode(XStream.ID_REFERENCES);
- </pre></div>
- <p>The result is a XML which generates an "id" attribute for each new object marshaled,
- and whenever it finds back or cross-references, it uses a "reference" attribute to so
- it doesn't copy the entire object.</p>
- <p>In our example, the list has id 1, the Maria Rita cd 2, and the Basement Jaxx 3. Therefore
- the cross-reference to our cd should contain a reference attribute to object number 2 and
- the back-reference to our order a reference to object 1. The result is:</p>
- <div class="Source Java"><pre>
- <list id="1">
- <cd id="2">
- <id>maria rita</id>
- </cd>
- <cd id="3">
- <id>basement_jaxx_singles</id>
- </cd>
- <cd reference="3"/>
- <list reference="1"/>
- </list>
- </pre></div>
- <!-- ...................................................... -->
- <h2>No references</h2>
-
- <p>For some uses of XStream we do not desire any kind of back or cross references like a graph, but a simple
- tree. The most famous example is when using XStream to generate XML for B2B services.</p>
- <p>In such scenarios it's impossible to represent a graph cycle (remember: no tree contains such structure), therefore
- we have to remove the last <b>add</b> call from our example:</p>
-
- <div class="Source Java"><pre>Cd bj = new Cd("basement_jaxx_singles");
-
- List order = new ArrayList();
- // adds the same cd twice (two references to the same object)
- order.add(bj);
- order.add(bj);
- XStream xstream = new XStream();
- xstream.alias("cd", Cd.class);
- System.out.println(xstream.toXML(order));
- </pre></div>
- <p>Now if we add the NO_REFERENCES option, every reference shall be completed serialized.</p>
- <div class="Source Java"><pre>
- xstream.setMode(XStream.NO_REFERENCES);
- </pre></div>
- <p>The result are three references:</p>
- <div class="Source Java"><pre>
- <list>
- <cd>
- <id>maria rita</id>
- </cd>
- <cd>
- <id>basement_jaxx_singles</id>
- </cd>
- <cd>
- <id>basement_jaxx_singles</id>
- </cd>
- </list>
- </pre></div>
- <p>After reading from the above XML, you will get three <b>different</b> Cd instances.</p>
-
- <p>Remember: it's impossible to support back-references (cycles) with NO_REFERENCES mode activated therefore
- a CircularReferenceException is thrown if you ever try to do so.</p>
-
-
- <br/>
- </div>
- </div>
- <div class="SidePanel" id="left">
- <div class="MenuGroup">
- <h1>Software</h1>
- <ul>
- <li><a href="index.html">About XStream</a></li>
- <li><a href="news.html">News</a></li>
- <li><a href="changes.html">Change History</a></li>
- <li><a href="versioning.html">About Versioning</a></li>
- </ul>
- </div>
- <div class="MenuGroup">
- <h1>Evaluating XStream</h1>
- <ul>
- <li><a href="tutorial.html">Two Minute Tutorial</a></li>
- <li class="currentLink">Object references</li>
- <li><a href="manual-tweaking-output.html">Tweaking the Output</a></li>
- <li><a href="license.html">License</a></li>
- <li><a href="download.html">Download</a></li>
- <li><a href="references.html">References</a></li>
- <li><a href="parser-benchmarks.html">Parser Benchmarks</a></li>
- <li><a href="http://www.ohloh.net/projects/3459">Code Statistics</a></li>
- </ul>
- </div>
- <div class="MenuGroup">
- <h1>Using XStream</h1>
- <ul>
- <li><a href="architecture.html">Architecture Overview</a></li>
- <li><a href="converters.html">Converters</a></li>
- <li><a href="faq.html">Frequently Asked Questions</a></li>
- <li><a href="list-user.html">Users' Mailing List</a></li>
- <li><a href="issues.html">Reporting Issues</a></li>
- </ul>
- </div>
- <div class="MenuGroup">
- <h1>Javadoc</h1>
- <ul>
- <li><a href="javadoc/index.html">XStream Core</a></li>
- <li><a href="hibernate-javadoc/index.html">Hibernate Extensions</a></li>
- <li><a href="benchmark-javadoc/index.html">Benchmark Module</a></li>
- </ul>
- </div>
- <div class="MenuGroup">
- <h1>Tutorials</h1>
- <ul>
- <li><a href="tutorial.html">Two Minute Tutorial</a></li>
- <li><a href="alias-tutorial.html">Alias Tutorial</a></li>
- <li><a href="annotations-tutorial.html">Annotations Tutorial</a></li>
- <li><a href="converter-tutorial.html">Converter Tutorial</a></li>
- <li><a href="objectstream.html">Object Streams Tutorial</a></li>
- <li><a href="persistence-tutorial.html">Persistence API Tutorial</a></li>
- <li><a href="json-tutorial.html">JSON Tutorial</a></li>
- </ul>
- </div>
- <div class="MenuGroup">
- <h1>Developing XStream</h1>
- <ul>
- <li><a href="how-to-contribute.html">How to Contribute</a></li>
- <li><a href="list-dev.html">Developers' Mailing List</a></li>
- <li><a href="team.html">Development Team</a></li>
- <li><a href="repository.html">Source Repository</a></li>
- <li><a href="http://bamboo.ci.codehaus.org/browse/XSTREAM">Continuous Integration</a></li>
- </ul>
- </div>
- </div>
- </body>
- </html>