/Prototipo/Servlet/lib/xstream-distribution-1.4.1-bin/xstream-1.4.1/docs/manual-tweaking-output.html

http://prototipomemoria.googlecode.com/ · HTML · 323 lines · 222 code · 76 blank · 25 comment · 0 complexity · 3fd1be8eccb0ff886bd44a57d4df0dd6 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 - Tweaking the Output</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">Tweaking the Output</h1>
  30. <p>Out of the box, XStream is able to serialize most objects without the need for custom mappings to be setup.
  31. The XML produced is clean, however sometimes it's desirable to make tweaks to it. The most common use for this is
  32. when using XStream to read configuration files and some more human-friendly XML is needed.</p>
  33. <!-- ************ -->
  34. <h1 id="Configuration">Modification by configuration</h1>
  35. <p>A big part of the standard output of XStream can be configured. It is possible to set aliases for class types
  36. and field names that are mapped to XML tag or attribute names. Objects that can be represented as simple string
  37. value can be written as attributes. It is possible to omit fields or to flatten the structure for collections.</p>
  38. <!-- .................. -->
  39. <h2 id="Configuration_Aliases">Aliases</h2>
  40. <p>Aliases offer a simple way to use different tag or attribute names in the XML. The simplest and most commonly
  41. used tweak in XStream is to alias a fully qualified class to a shorter name. Another use case is a different field name
  42. for a class member. Aliases can be set for following elements:</p>
  43. <ul>
  44. <li>Class types mapped to XML tags</li>
  45. <li>Package names mapped to XML tags</li>
  46. <li>Field names mapped to XML tags</li>
  47. <li>Internal attributes names of XStream</li>
  48. </ul>
  49. <p>The bold elements in the following example are affected:</p>
  50. <div class="Source XML"><pre>&lt;<b>cat</b>&gt;
  51. &lt;<b>age</b>&gt;4&lt;/<b>age</b>&gt;
  52. &lt;<b>name</b>&gt;Garfield&lt;/<b>name</b>&gt;
  53. &lt;<b>owner</b> <b>type</b>="<b>StandardPerson</b>"&gt;
  54. &lt;<b>name</b>&gt;Jon Arbuckle&lt;/<b>name</b>&gt;
  55. &lt;/<b>owner</b>&gt;
  56. &lt;/<b>cat</b>&gt;</pre></div>
  57. <p>Have a look at the <a href="alias-tutorial.html">Alias Tutorial</a> for examples.</p>
  58. <!-- .................. -->
  59. <h2 id="Configuration_Attributes">Attributes</h2>
  60. <p>XML is quite clumsy to read for fields in separate tags that can represent their content in a short single
  61. string value. In such a case attributes can help to shorten the XML and increase readability:</p>
  62. <div class="Source XML"><pre>&lt;cat <b>age="4" name="Garfield"</b>&gt;
  63. &lt;owner class="StandardPerson" <b>name="Jon Arbuckle"</b>/&gt;
  64. &lt;/cat&gt;</pre></div>
  65. <p>Attributes are also presented in the <a href="alias-tutorial.html">Alias Tutorial</a>.</p>
  66. <!-- .................. -->
  67. <h2 id="Configuration_OmitField">Omitted Fields</h2>
  68. <p>For a proper deserialization XStream has to write the complete object graph into XML that is referenced by a
  69. single object. Therefore XStream has to find a representation that contains all aspects to recreate the
  70. objects.</p>
  71. <p>However, some parts might be superfluous e.g. if a member field is lazy initialized and its content
  72. can be easily recreated. In such a case a field can be omitted using
  73. <a href="javadoc/com/thoughtworks/xstream/XStream.html">XStream.omitField(Class, String)</a>.</p>
  74. <!-- .................. -->
  75. <h2 id="Configuration_ImplicitCollection">Implicit Collections, Arrays and Maps</h2>
  76. <p>Another use case are collections, arrays and maps. If a class has a field that is a one of those types, by
  77. default all of its elements are embedded in an element that represents the container object itself. By
  78. configuring the XStream with the
  79. <a href="javadoc/com/thoughtworks/xstream/XStream.html#addImplicitCollection">XStream.addImplicitCollection()</a>,
  80. <a href="javadoc/com/thoughtworks/xstream/XStream.html#addImplicitArray">XStream.addImplicitArray()</a>, and
  81. <a href="javadoc/com/thoughtworks/xstream/XStream.html#addImplicitMap">XStream.addImplicitMap()</a> methods it
  82. is possible to keep the elements directly as child of the class and the surrounding tag for the container object
  83. is omitted. It is even possible to declare more than one implicit collection, array or map for a class, but
  84. the elements must then be distinguishable to populate the different containers correctly at deserialization.</p>
  85. <p>In the following example the Java type representing the farm may have two containers, one for cats and one
  86. for dogs:</p>
  87. <div class="Source XML"><pre>&lt;farm&gt;
  88. &lt;<b>cat</b>&gt;Garfield&lt;/<b>cat</b>&gt;
  89. &lt;<b>cat</b>&gt;Arlene&lt;/<b>cat</b>&gt;
  90. &lt;<b>cat</b>&gt;Nermal&lt;/<b>cat</b>&gt;
  91. &lt;<b>dog</b>&gt;Odie&lt;/<b>dog</b>&gt;
  92. &lt;/farm&gt;</pre></div>
  93. <p>The container might be a Collection, Array, or even a Map. In the latter case a member field of the value
  94. must have been defined, that is used as key in the deserialized map.</p>
  95. <!-- .................. -->
  96. <h2 id="Configuration_FieldOrder">Field order</h2>
  97. <p>XStream is delivered with a lot of <a href="converters.html">converters</a> for standard types.
  98. Nevertheless most objects are processed by converters based on reflection. They will write the fields of a
  99. class in the sequence they are defined. It is possible to implement an algorithm for a different sequence or
  100. use an implementation that allows to define the sequence for each type separately using a
  101. <a href="javadoc/com/thoughtworks/xstream/converters/reflection/FieldKeySorter.html">FieldKeySorter</a>.
  102. Similar functionality exists for Java Beans with the
  103. <a href="javadoc/com/thoughtworks/xstream/converters/javabean/PropertySorter.html">PropertySorter</a>.</p>
  104. <!-- ************ -->
  105. <h1 id="Enhancing">Enhancing XStream</h1>
  106. <p>Sometimes customization is simply not enough to tweak the output. Depending on the use case it is fortunate to
  107. use specialized converters for own types, mapper implementations that control naming of things more globally or
  108. use specialized writers to influence the complete output.</p>
  109. <!-- .................. -->
  110. <h2 id="Specialized_Converters">Specialized Converters</h2>
  111. <p>Not all converters that are part of the XStream package are automatically registered. Some will only make
  112. sense for special types, others have parameters to tweak the behaviour. Most useful in the table of
  113. <a href="converters.html">converters</a> are the JavaBeanConverter, ToStringConverter, and
  114. ToAttributesValueConverter. Not to mention the separate Hibernate package of XStream.</p>
  115. <!-- .................. -->
  116. <h2 id="Enhancing_Converters">Custom Converters</h2>
  117. <p>Sometimes the object to serialize contains fields or elements, that have no friendly representation for
  118. human beings e.g. if a long value represents in reality a time stamp. In such cases XStream supports custom
  119. converters for arbitrary types. Have a look at the <a href="converter-tutorial.html">Converter Tutorial</a>
  120. for advanced possibilities. Note, that a custom converter is not different to one that is delivered by
  121. XStream. It's simply your code.</p>
  122. <!-- .................. -->
  123. <h2 id="Enhancing_Mappers">Custom Mappers</h2>
  124. <p>In case of global adjustments it can be helpful to implement an own mapper. A mapper is used to name things
  125. and map between the name in the Java world to the name used in the XML representation. The alias mechanism
  126. described above is implemented as such a mapper that can be configured. A typical use case is dropping all
  127. prefixes for field names like underscores in the resulting XML or omitting the package part of class names.</p>
  128. <p class="highlight">However, keep in mind that the algorithm must work in both directions to support deserialization.</p>
  129. <!-- .................. -->
  130. <h2 id="Enhancing_Writer">Custom Writer</h2>
  131. <p>A custom writer can be used to affect the output completely. XStream itself delivers solutions for different
  132. use cases like the <a href="javadoc/com/thoughtworks/xstream/io/xml/CompactWriter.html">CompactWriter</a>
  133. that does not insert any white spaces between the XML tags or the
  134. <a href="javadoc/com/thoughtworks/xstream/io/json/JsonHierarchicalStreamWriter.html">JSON writer</a> that
  135. produces a complete different output format.</p>
  136. <p>Another use case for such a writer is to drop unwanted XML elements that XStream omits on its own.
  137. Especially if the written XML is not used for deserialization it can be useful to ignore internal attributes by
  138. a custom writer</p>
  139. <!-- ************ -->
  140. <h1 id="OwnCode">Tweaking the own implementation</h1>
  141. <p>As shown, XStream can be configured and enhanced in multiple way, but sometimes it is easier to tweak the
  142. implementation of the serialized classes:</p>
  143. <ul>
  144. <li>Declaring a field transparent will omit it automatically from the processing</li>
  145. <li>Implementing a readResolve method that can be used to initialize additional fields</li>
  146. <li>Usage of annotations</li>
  147. </ul>
  148. <!-- ************ -->
  149. <h1 id="Processing">Preprocessing or postprocessing</h1>
  150. <h2>XML Transformations</h2>
  151. <p>Never forget, you're dealing with XML! It is easy to transform XML with an XSLT. XStream is delivered with a SAXSource
  152. implementation, that allows an XStream instance to be the source of a XML transformer.</p>
  153. <h3>Example</h3>
  154. <p>Look at the following stylesheet:</p>
  155. <div class="Source XML"><pre>
  156. &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  157. &lt;xsl:output method="xml" omit-xml-declaration="yes" indent="no"/&gt;
  158. &lt;xsl:template match="/cat"&gt;
  159. &lt;xsl:copy&gt;
  160. &lt;xsl:apply-templates select="mName"/&gt;
  161. &lt;/xsl:copy&gt;
  162. &lt;/xsl:template&gt;
  163. &lt;/xsl:stylesheet&gt;</pre></div>
  164. <p>It is used here to remove the age of the cat on the fly (assuming XSLT is a string with the stylesheet above):</p>
  165. <div class="Source Java"><pre>
  166. XStream xstream = new XStream();
  167. xstream.alias("cat", Cat.class);
  168. TraxSource <b>traxSource</b> = new TraxSource(new Cat(4, "Garfield"), <b>xstream</b>);
  169. Writer buffer = new StringWriter();
  170. Transformer transformer = TransformerFactory.newInstance().newTransformer(
  171. new StreamSource(new StringReader(XSLT)));
  172. transformer.transform(<b>traxSource</b>, new StreamResult(buffer));</pre></div>
  173. <p>The result in the buffer:</p>
  174. <div class="Source XML"><pre>&lt;cat&gt;
  175. &lt;mName&gt;Garfield&lt;/mName&gt;
  176. &lt;/cat&gt;</pre></div>
  177. <br/>
  178. </div>
  179. </div>
  180. <div class="SidePanel" id="left">
  181. <div class="MenuGroup">
  182. <h1>Software</h1>
  183. <ul>
  184. <li><a href="index.html">About XStream</a></li>
  185. <li><a href="news.html">News</a></li>
  186. <li><a href="changes.html">Change History</a></li>
  187. <li><a href="versioning.html">About Versioning</a></li>
  188. </ul>
  189. </div>
  190. <div class="MenuGroup">
  191. <h1>Evaluating XStream</h1>
  192. <ul>
  193. <li><a href="tutorial.html">Two Minute Tutorial</a></li>
  194. <li><a href="graphs.html">Object references</a></li>
  195. <li class="currentLink">Tweaking the Output</li>
  196. <li><a href="license.html">License</a></li>
  197. <li><a href="download.html">Download</a></li>
  198. <li><a href="references.html">References</a></li>
  199. <li><a href="parser-benchmarks.html">Parser Benchmarks</a></li>
  200. <li><a href="http://www.ohloh.net/projects/3459">Code Statistics</a></li>
  201. </ul>
  202. </div>
  203. <div class="MenuGroup">
  204. <h1>Using XStream</h1>
  205. <ul>
  206. <li><a href="architecture.html">Architecture Overview</a></li>
  207. <li><a href="converters.html">Converters</a></li>
  208. <li><a href="faq.html">Frequently Asked Questions</a></li>
  209. <li><a href="list-user.html">Users' Mailing List</a></li>
  210. <li><a href="issues.html">Reporting Issues</a></li>
  211. </ul>
  212. </div>
  213. <div class="MenuGroup">
  214. <h1>Javadoc</h1>
  215. <ul>
  216. <li><a href="javadoc/index.html">XStream Core</a></li>
  217. <li><a href="hibernate-javadoc/index.html">Hibernate Extensions</a></li>
  218. <li><a href="benchmark-javadoc/index.html">Benchmark Module</a></li>
  219. </ul>
  220. </div>
  221. <div class="MenuGroup">
  222. <h1>Tutorials</h1>
  223. <ul>
  224. <li><a href="tutorial.html">Two Minute Tutorial</a></li>
  225. <li><a href="alias-tutorial.html">Alias Tutorial</a></li>
  226. <li><a href="annotations-tutorial.html">Annotations Tutorial</a></li>
  227. <li><a href="converter-tutorial.html">Converter Tutorial</a></li>
  228. <li><a href="objectstream.html">Object Streams Tutorial</a></li>
  229. <li><a href="persistence-tutorial.html">Persistence API Tutorial</a></li>
  230. <li><a href="json-tutorial.html">JSON Tutorial</a></li>
  231. </ul>
  232. </div>
  233. <div class="MenuGroup">
  234. <h1>Developing XStream</h1>
  235. <ul>
  236. <li><a href="how-to-contribute.html">How to Contribute</a></li>
  237. <li><a href="list-dev.html">Developers' Mailing List</a></li>
  238. <li><a href="team.html">Development Team</a></li>
  239. <li><a href="repository.html">Source Repository</a></li>
  240. <li><a href="http://bamboo.ci.codehaus.org/browse/XSTREAM">Continuous Integration</a></li>
  241. </ul>
  242. </div>
  243. </div>
  244. </body>
  245. </html>