<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wikidot="http://www.wikidot.com/rss-namespace">

	<channel>
		<title>Elisha Peterson&#039;s Programming and Technology Notes</title>
		<link>http://elishapeterson.wikidot.com</link>
		<description></description>
				<copyright></copyright>
		<lastBuildDate>Tue, 09 Jun 2026 08:38:07 +0000</lastBuildDate>
		
					<item>
				<guid>http://elishapeterson.wikidot.com/technotes:javabeans</guid>
				<title>JavaBeans</title>
				<link>http://elishapeterson.wikidot.com/technotes:javabeans</link>
				<description>

&lt;p&gt;Elisha Peterson&lt;/p&gt;
&lt;p&gt;by &lt;span class=&quot;printuser avatarhover&quot;&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/avatar.php?userid=59229&amp;amp;amp;size=small&amp;amp;amp;timestamp=1780994286&quot; alt=&quot;elishapeterson&quot; style=&quot;background-image:url(http://www.wikidot.com/userkarma.php?u=59229)&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;elishapeterson&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
				<pubDate>Wed, 01 Jul 2009 14:30:11 +0000</pubDate>
												<content:encoded>
					<![CDATA[
						 <p>Elisha Peterson</p> <div class="content-separator" style="display: none:"></div> <p>This describes &quot;JavaBeans&quot; and how to use them. A Java &quot;bean&quot; is essentially a class that adopts a kind of contract regarding the names and nature of its methods. This contract allows the class to be easily handled when it comes to adjusting its properties dynamically or through property sheets, serializing (i.e. saving the class to a file and re-loading it), event handling, and many other situations. See also <a href="http://java.sun.com/docs/books/tutorial/javabeans/TOC.html">this official guide</a>.</p> <div class="content-separator" style="display: none:"></div> <table style="margin:0; padding:0"> <tr> <td style="margin:0; padding:0"> <div id="toc"> <div id="toc-action-bar"><a href="javascript:;" >Fold</a><a style="display: none" href="javascript:;" >Unfold</a></div> <div class="title">Table of Contents</div> <div id="toc-list"> <div style="margin-left: 1em;"><a href="#toc0">Code Patterns</a></div> <div style="margin-left: 2em;"><a href="#toc1">Properties</a></div> <div style="margin-left: 2em;"><a href="#toc2">Events</a></div> <div style="margin-left: 2em;"><a href="#toc3">Bound Properties</a></div> <div style="margin-left: 2em;"><a href="#toc4">Constrained Properties</a></div> <div style="margin-left: 1em;"><a href="#toc5">Persistence</a></div> <div style="margin-left: 2em;"><a href="#toc6">Automatic Persistence</a></div> <div style="margin-left: 2em;"><a href="#toc7">Decoding</a></div> <div style="margin-left: 2em;"><a href="#toc8">Persistence with Collections and Arrays</a></div> <div style="margin-left: 2em;"><a href="#toc9">Custom Persistence</a></div> <div style="margin-left: 2em;"><a href="#toc10">Bugs and Difficulties</a></div> <div style="margin-left: 3em;"><a href="#toc11">Arrays</a></div> <div style="margin-left: 3em;"><a href="#toc12">Enums</a></div> <div style="margin-left: 1em;"><a href="#toc13">Property Editors</a></div> <div style="margin-left: 2em;"><a href="#toc14">Display and Editing Options</a></div> <div style="margin-left: 2em;"><a href="#toc15">Standard Support Class</a></div> <div style="margin-left: 2em;"><a href="#toc16">Package Implementations</a></div> <div style="margin-left: 2em;"><a href="#toc17">Customizers</a></div> </div> </div> </td> </tr> </table> <div class="content-separator" style="display: none:"></div> <p><strong>Properties</strong> are single data values that have get and set methods. <strong>Beans</strong> contain several properties. The <strong>JavaBeans specification</strong> consists of design patterns that allow beans to be made easily portable. While a single property might use <tt>ChangeEvent</tt>s to notify listeners when a value has changed, a bean may use <tt>PropertyChangeEvent</tt>s to notify listeners of changes to a specific property. This event contains additional information about the old and new values of a property.</p> <p>The basic requirements for a Java &quot;bean&quot; are:</p> <ul> <li>must be a <tt>public</tt> class</li> <li>must implement the <tt>Serializable</tt> interface;</li> <li>must have a <tt>public</tt> no-argument constructor.</li> </ul> <p><em>Introspection</em> allows properties and event handling routines that are specified by particular code templates to be &quot;discovered&quot; by external classes. In particular, the Netbeans GUI builder can &quot;detect&quot; the bean properties and allow for customization. For properties of the standard types (<tt>boolean</tt>, <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, <tt>float</tt>, <tt>double</tt>, <tt>String</tt>, <tt>Color</tt>), property editing is automatically supported within the GUI's <em>property sheet</em>. The code generated by the builder first calls the no-argument constructor, and then calls various <tt>set</tt> methods for the properties that have been customized.</p> <h1><span>Code Patterns</span></h1> <p>Besides the no-argument constructor, <em>JavaBeans</em> use several other code patterns.</p> <h2><span>Properties</span></h2> <p>To allow introspection, <strong>properties</strong> should conform to get/set patterns:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">C</span><span class="hl-code"> </span><span class="hl-identifier">getXyz</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setXyz</span><span class="hl-brackets">(</span><span class="hl-identifier">C</span><span class="hl-code"> </span><span class="hl-identifier">newValue</span><span class="hl-brackets">)</span><span class="hl-code">;</span></pre></div> </div> <br /> where <tt>C</tt> is the class type and <tt>Xyz</tt> is the name of the property, e.g. &quot;<tt>Value</tt>&quot;. The lowercase version &quot;<tt>value</tt>&quot; is the <em>official</em> property identifier. <strong>Note:</strong> for <tt>boolean</tt> properties, the getter may take the form <tt>public boolean isXyz();</tt>. <p><strong>Indexed properties</strong>, typically arrays, support indexed get/set patterns also:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-identifier">C</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">getXyz</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setXyz</span><span class="hl-brackets">(</span><span class="hl-identifier">C</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">array</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-identifier">C</span><span class="hl-code"> </span><span class="hl-identifier">getXyz</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">index</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setXyz</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">index</span><span class="hl-code">, </span><span class="hl-identifier">C</span><span class="hl-code"> </span><span class="hl-identifier">element</span><span class="hl-brackets">)</span></pre></div> </div> <h2><span>Events</span></h2> <p>Similarly, <strong>event handling</strong> should conform to add/remove patterns:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">addXyzListener</span><span class="hl-brackets">(</span><span class="hl-identifier">XyzListener</span><span class="hl-code"> </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">removeXyzListener</span><span class="hl-brackets">(</span><span class="hl-identifier">XyzListener</span><span class="hl-code"> </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span></pre></div> </div> <br /> where <tt>Xyz</tt> is replaced by the type of listener&#8230; e.g. <tt>ChangeListener</tt> or <tt>ActionListener</tt>. <h2><span>Bound Properties</span></h2> <p><strong>Bound properties</strong> fire <tt>PropertyChangeEvent</tt>s when their value changes. This event includes (i) the <tt>String</tt> name of the property, (ii) the old value, and (iii) the new value. Any registered <tt>PropertyChangeListener</tt>s may respond to the change accordingly. The <tt>java.beans.PropertyChangeSupport</tt> class contains the necessary event handling. A typical <tt>set</tt> method differs for bound properties, as in the following example:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setTitle</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">title</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code"> ! </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">title</span><span class="hl-code">.</span><span class="hl-identifier">equals</span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">old</span><span class="hl-code"> = </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">title</span><span class="hl-code">; </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">title</span><span class="hl-code"> = </span><span class="hl-identifier">title</span><span class="hl-code">; </span><span class="hl-identifier">firePropertyChange</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">old</span><span class="hl-code">, </span><span class="hl-identifier">title</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span></pre></div> </div> <h2><span>Constrained Properties</span></h2> <p><strong>Constrained properties</strong> fire <tt>VetoableChangeEvent</tt>s when their value changes.</p> <h1><span>Persistence</span></h1> <p><strong>Persistence</strong> refers to the ability to save and reload classes. JavaBean code patterns allow for automated persistence through the use of the <tt>XMLEncoder</tt> class. For full details, see <a href="http://java.sun.com/products/jfc/tsc/articles/persistence4/">http://java.sun.com/products/jfc/tsc/articles/persistence4/</a>.</p> <h2><span>Automatic Persistence</span></h2> <p>The underlying procedure in saving and reloading is using the existing properties of a class, as defined via the above bean patterns, to store the class. Only attributes that are accessible through properties are stored, so the properties MUST reflect the entire state of the object. In summary, for automatic persistence to work:</p> <ul> <li>The class must have a no argument constructor.</li> <li>The entire state of the class must be reflected in its properties (get/set methods).</li> </ul> <p>Here is an example of a class that uses bean patterns to store the file, together with the output:</p> <table style="width:100%"> <tr style="vertical-align:top"> <td> <p><strong><span style="text-decoration: underline;">CODE</span></strong></p> <div class="code"> <div class="hl-main"> <pre><span class="hl-comment">/** This is the class definition for the object we wish to save. */</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">; </span><span class="hl-comment">// the state variables</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">weight</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Spot</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-number">20</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">// the public no-argument constructor (required)</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">weight</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-identifier">name</span><span class="hl-code">; </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">weight</span><span class="hl-code"> = </span><span class="hl-identifier">weight</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">// the bean properties</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setName</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-identifier">name</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">getName</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setWeight</span><span class="hl-brackets">(</span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">weight</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">weight</span><span class="hl-code"> = </span><span class="hl-identifier">weight</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">getWeight</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">weight</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">/** This is how to create and use the XMLEncoder */</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-comment">// create an object to save</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">dog</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-identifier">dog</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Spotty</span><span class="hl-quotes">&quot;</span><span class="hl-code">; </span><span class="hl-comment">// create the encoder and print the object</span><span class="hl-code"> </span><span class="hl-identifier">XMLEncoder</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">XMLEncoder</span><span class="hl-brackets">(</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">writeObject</span><span class="hl-brackets">(</span><span class="hl-identifier">dog</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">close</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> </td> <td> <p><strong><span style="text-decoration: underline;">OUTPUT</span></strong></p> <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;?</span><span class="hl-reserved">xml</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.0</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">encoding</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">UTF-8</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">?&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">java</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.5.0_21</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">java.beans.XMLDecoder</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">object</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Dog</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">property</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">name</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code">Spotty</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">object</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">java</span><span class="hl-brackets">&gt;</span></pre></div> </div> </td> </tr> </table> <p><em>Some comments:</em></p> <ul> <li>It is easy to write to a file instead of <tt>System.out</tt>. One can use for example <tt>new FileOutputStream(&quot;dog.xml&quot;)</tt>.</li> <li>Notice that only the name property is written to the file. That's because the weight is still the default value. If one changes the weight to be something other than 20, then it will show up in the xml. So if we had written instead <tt>Dog dog = new Dog{&quot;Curly&quot;, 15};</tt> the XML output would contain both the <tt>name</tt> and <tt>weight</tt> properties.</li> </ul> <h2><span>Decoding</span></h2> <p>The decoding process is similarly easy. The code below illustrates this. Note that this code acts as a sort of test to ensure that whatever is written to a file also reads in properly.</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-identifier">XMLEncoder</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-code">; </span><span class="hl-identifier">XMLDecoder</span><span class="hl-code"> </span><span class="hl-identifier">ed</span><span class="hl-code">; </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">dog</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-identifier">dog</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">Spotty</span><span class="hl-quotes">&quot;</span><span class="hl-code">; </span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">XMLEncoder</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FileOutputStream</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">test1.xml</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-identifier">ed</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">XMLDecoder</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FileInputStream</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">test1.xml</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">dog</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">writeObject</span><span class="hl-brackets">(</span><span class="hl-identifier">dog</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">close</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-identifier">dog</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">Dog</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-identifier">ed</span><span class="hl-code">.</span><span class="hl-identifier">readObject</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-identifier">ed</span><span class="hl-code">.</span><span class="hl-identifier">close</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">dog</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">FileNotFoundException</span><span class="hl-code"> </span><span class="hl-identifier">ex</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Logger</span><span class="hl-code">.</span><span class="hl-identifier">getLogger</span><span class="hl-brackets">(</span><span class="hl-identifier">Dog</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-code">.</span><span class="hl-identifier">getName</span><span class="hl-brackets">())</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">Level</span><span class="hl-code">.</span><span class="hl-identifier">SEVERE</span><span class="hl-code">, </span><span class="hl-reserved">null</span><span class="hl-code">, </span><span class="hl-identifier">ex</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> <h2><span>Persistence with Collections and Arrays</span></h2> <p>Persistence with Java's <tt>List</tt>s and arrays does not require much extra, but the resulting XML looks different. Here is another example below. Note particularly how the <tt>Vector</tt>'s <tt>add</tt> method is being encoded directly.</p> <table style="width:100%"> <tr style="vertical-align:top"> <td> <p><strong><span style="text-decoration: underline;">CODE</span></strong></p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DogPack</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Vector</span><span class="hl-code">&lt;</span><span class="hl-identifier">Dog</span><span class="hl-code">&gt; </span><span class="hl-identifier">dogs</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">DogPack</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">dogs</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Vector</span><span class="hl-code">&lt;</span><span class="hl-identifier">Dog</span><span class="hl-code">&gt;</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Vector</span><span class="hl-code">&lt;</span><span class="hl-identifier">Dog</span><span class="hl-code">&gt; </span><span class="hl-identifier">getDogs</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">dogs</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setDogs</span><span class="hl-brackets">(</span><span class="hl-identifier">Vector</span><span class="hl-code">&lt;</span><span class="hl-identifier">Dog</span><span class="hl-code">&gt; </span><span class="hl-identifier">dogs</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">dogs</span><span class="hl-code"> = </span><span class="hl-identifier">dogs</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">Override</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">toString</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">dogs</span><span class="hl-code">.</span><span class="hl-identifier">toString</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">dog</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-identifier">dog</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Spotty</span><span class="hl-quotes">&quot;</span><span class="hl-code">; </span><span class="hl-identifier">DogPack</span><span class="hl-code"> </span><span class="hl-identifier">pack</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">DogPack</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-identifier">pack</span><span class="hl-code">.</span><span class="hl-identifier">dogs</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Curly</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-number">15</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-identifier">pack</span><span class="hl-code">.</span><span class="hl-identifier">dogs</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-identifier">dog</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">XMLEncoder</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">XMLEncoder</span><span class="hl-brackets">(</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">writeObject</span><span class="hl-brackets">(</span><span class="hl-identifier">pack</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">close</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> </td> <td> <p><strong><span style="text-decoration: underline;">OUTPUT</span></strong></p> <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;?</span><span class="hl-reserved">xml</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.0</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">encoding</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">UTF-8</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">?&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">java</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.5.0_21</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">java.beans.XMLDecoder</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">object</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">testpersist.Dog$DogPack</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">property</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">dogs</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">method</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">add</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">object</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">testpersist.Dog</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">property</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">name</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code">Curly</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">property</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">weight</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">float</span><span class="hl-brackets">&gt;</span><span class="hl-code">15.0</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">float</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">object</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">method</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">add</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">object</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">testpersist.Dog</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">property</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">name</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code">Spotty</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">object</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">object</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">java</span><span class="hl-brackets">&gt;</span></pre></div> </div> </td> </tr> </table> <p>The case of writing arrays is similar, although the xml looks different. Here note the indexing properties, and also note the use of the <tt>idref</tt> attribute to refer to a previously-defined object (ensuring proper re-creation of pointers).</p> <table style="width:100%"> <tr style="vertical-align:top"> <td> <p><strong><span style="text-decoration: underline;">CODE</span></strong></p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DogPack2</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">dogs</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">DogPack2</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">dogs</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">[</span><span class="hl-number">3</span><span class="hl-brackets">]</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">getDogs</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">dogs</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setDogs</span><span class="hl-brackets">(</span><span class="hl-identifier">Dog</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">dogs</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">dogs</span><span class="hl-code"> = </span><span class="hl-identifier">dogs</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">Override</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">toString</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">Arrays</span><span class="hl-code">.</span><span class="hl-identifier">toString</span><span class="hl-brackets">(</span><span class="hl-identifier">dogs</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">dog</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-identifier">dog</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Spotty</span><span class="hl-quotes">&quot;</span><span class="hl-code">; </span><span class="hl-identifier">DogPack2</span><span class="hl-code"> </span><span class="hl-identifier">pack2</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">DogPack2</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-identifier">pack2</span><span class="hl-code">.</span><span class="hl-identifier">setDogs</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">[]{</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Moe</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-number">12</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-identifier">dog</span><span class="hl-code">, </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Mic</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-number">4</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-identifier">dog</span><span class="hl-brackets">})</span><span class="hl-code">; </span><span class="hl-identifier">XMLEncoder</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">XMLEncoder</span><span class="hl-brackets">(</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">writeObject</span><span class="hl-brackets">(</span><span class="hl-identifier">pack2</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">close</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> </td> <td> <p><strong><span style="text-decoration: underline;">OUTPUT</span></strong></p> <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;?</span><span class="hl-reserved">xml</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.0</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">encoding</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">UTF-8</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">?&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">java</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.5.0_21</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">java.beans.XMLDecoder</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">object</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">testpersist.Dog$DogPack2</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">property</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">dogs</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">array</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">testpersist.Dog</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">length</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">4</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">index</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">0</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">object</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">testpersist.Dog</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">property</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">name</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code">Moe</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">property</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">weight</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">float</span><span class="hl-brackets">&gt;</span><span class="hl-code">12.0</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">float</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">object</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">index</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">object</span><span class="hl-code"> </span><span class="hl-var">id</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Dog0</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">testpersist.Dog</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">property</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">name</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code">Spotty</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">object</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">index</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">2</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">object</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">testpersist.Dog</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">property</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">name</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code">Mic</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">string</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">property</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">weight</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">float</span><span class="hl-brackets">&gt;</span><span class="hl-code">4.0</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">float</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">object</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-var">index</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">3</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">object</span><span class="hl-code"> </span><span class="hl-var">idref</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Dog0</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">array</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">void</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">object</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">java</span><span class="hl-brackets">&gt;</span></pre></div> </div> </td> </tr> </table> <h2><span>Custom Persistence</span></h2> <p>There is a standard procedure for &quot;delegating&quot; persistence to another object. This is particularly useful when the base class does not support the required bean patterns. One object of this type is <tt>java.awt.geom.Point2D.Double</tt>, which has two parameters <tt>x</tt> and <tt>y</tt>, and <tt>get</tt> methods for these parameters, but no corresponding <tt>set</tt> methods. Here is an example of a something that might go wrong.</p> <table style="width:100%"> <tr style="vertical-align:top"> <td> <p><strong><span style="text-decoration: underline;">CODE</span></strong></p> <div class="code"> <div class="hl-main"> <pre><span class="hl-identifier">Point2D</span><span class="hl-code">.</span><span class="hl-identifier">Double</span><span class="hl-code"> </span><span class="hl-identifier">p</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Point2D</span><span class="hl-code">.</span><span class="hl-identifier">Double</span><span class="hl-brackets">(</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">XMLEncoder</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">XMLEncoder</span><span class="hl-brackets">(</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">writeObject</span><span class="hl-brackets">(</span><span class="hl-identifier">p</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">close</span><span class="hl-brackets">()</span><span class="hl-code">;</span></pre></div> </div> </td> <td> <p><strong><span style="text-decoration: underline;">OUTPUT</span></strong></p> <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;?</span><span class="hl-reserved">xml</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.0</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">encoding</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">UTF-8</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">?&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">java</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.5.0_21</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">java.beans.XMLDecoder</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">object</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">java.awt.geom.Point2D$Double</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">java</span><span class="hl-brackets">&gt;</span></pre></div> </div> </td> </tr> </table> <p>If we try to save and reload the file, then we end up with the coordinate (0,0) instead of (3,2). The work-around is to create a custom persistence delegate, which is actually really easy, so instead use:</p> <table style="width:100%"> <tr style="vertical-align:top"> <td> <p><strong><span style="text-decoration: underline;">CODE</span></strong></p> <div class="code"> <div class="hl-main"> <pre><span class="hl-identifier">Point2D</span><span class="hl-code">.</span><span class="hl-identifier">Double</span><span class="hl-code"> </span><span class="hl-identifier">p</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Point2D</span><span class="hl-code">.</span><span class="hl-identifier">Double</span><span class="hl-brackets">(</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">XMLEncoder</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">XMLEncoder</span><span class="hl-brackets">(</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">setPersistenceDelegate</span><span class="hl-brackets">(</span><span class="hl-identifier">Point2D</span><span class="hl-code">.</span><span class="hl-identifier">Double</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-code">, </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">DefaultPersistenceDelegate</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-brackets">[]{</span><span class="hl-quotes">&quot;</span><span class="hl-string">x</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">y</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">}))</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">writeObject</span><span class="hl-brackets">(</span><span class="hl-identifier">p</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">close</span><span class="hl-brackets">()</span><span class="hl-code">;</span></pre></div> </div> </td> <td> <p><strong><span style="text-decoration: underline;">OUTPUT</span></strong></p> <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;?</span><span class="hl-reserved">xml</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.0</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">encoding</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">UTF-8</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">?&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">java</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.5.0_21</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">java.beans.XMLDecoder</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">object</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">java.awt.geom.Point2D$Double</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">double</span><span class="hl-brackets">&gt;</span><span class="hl-code">3.0</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">double</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">double</span><span class="hl-brackets">&gt;</span><span class="hl-code">2.0</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">double</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">object</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">java</span><span class="hl-brackets">&gt;</span></pre></div> </div> </td> </tr> </table> <p>This tells the encoder to store the values obtained by <tt>getX()</tt> and <tt>getY()</tt>, and then to use them in the default constructor when the object is restored using the <tt>XMLDecoder</tt>.<br /> There is no need to set the persistence delegate for the decoder as well&#8230; I think the decoder puts the values saved in the xml directly into the constructor. (So if you switch the order of &quot;x&quot; and &quot;y&quot; the values read in incorrectly.)</p> <h2><span>Bugs and Difficulties</span></h2> <h3><span>Arrays</span></h3> <p>I have found occasional problems when storing a class with a <tt>Point2D.Double[]</tt> property. For some reason, the value of the object is not always stored, even if the old array differs from the new array, unless the array is first recreated. I suspect this is a system bug, partly due to the <tt>equals()</tt> method in <tt>Point2D.Double</tt>, which is defined in the abstract class <tt>Point2D.Double</tt>, since an equivalent class with this method redefined works properly. This may also happen in some other circumstances&#8230; I'm not quite sure. Here are possible solutions:</p> <ul> <li>In the class with the point array, DO NOT SET UP any initial values for the points, or set them up with <strong>null</strong> values. Things seem to work properly when initial values are <strong>null</strong>.</li> <li>Create new get and set methods that will handle the persistence, e.g. use a <tt>double[][]</tt> type to get and set values instead. Then ensure that the persistence delegate only uses this value.</li> <li>Create a subtype of <tt>Point2D.Double</tt> that overrides the &quot;equals&quot; method and use objects of this type instead&#8230; this works even if you only use it for the default values.</li> </ul> <h3><span>Enums</span></h3> <p>Java's enums also do not work properly with <tt>XMLEncoder</tt>, at least in earlier versions (see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5015403">http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5015403</a>). I have found that the workaround specified there tends to work for enums that are declared with just constants, but not if they are declared with custom methods or any non-standard methods. A quick workaround is to use the <tt>String</tt> value returned by the <tt>name()</tt> method to serialize rather than the enum itself. So in the bean, instead of using</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">YourEnumName</span><span class="hl-code"> </span><span class="hl-identifier">getYourEnum</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">yourEnum</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setYourEnum</span><span class="hl-brackets">(</span><span class="hl-identifier">YourEnumName</span><span class="hl-code"> </span><span class="hl-identifier">newValue</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">yourEnum</span><span class="hl-code"> = </span><span class="hl-identifier">newValue</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> <br /> use the following: <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">getYourEnum</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">yourEnum</span><span class="hl-code">==</span><span class="hl-reserved">null</span><span class="hl-code"> ? </span><span class="hl-quotes">&quot;&quot;</span><span class="hl-code"> : </span><span class="hl-identifier">yourEnum</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setYourEnum</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">newValue</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">yourEnum</span><span class="hl-code"> = </span><span class="hl-identifier">YourEnumName</span><span class="hl-code">.</span><span class="hl-identifier">valueOf</span><span class="hl-brackets">(</span><span class="hl-identifier">newValue</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> <h1><span>Property Editors</span></h1> <p><tt><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/PropertyEditor.html">PropertyEditor</a></tt>s are intended to <em>&quot;provide support for GUIs that want to allow users to edit a property value of a given type&quot;</em>. In doing so, they handle <tt>PropertyChangeEvent</tt>s for underlying beans. When the value is changed, the class fires an event notifying any registered listeners of the change.</p> <p>The <tt>PropertyEditor</tt> API encapsulates a single bean, with <tt>getValue</tt> and <tt>setValue</tt> methods, and <tt>PropertyChangeEvent</tt> handling.</p> <p>The API also includes <tt>getJavaInitializationString</tt>, which provides Java constructor code to get the value underlying the component. This can be used by GUI builders to automatically generate the underlying object, or a copy of that object. I'm not entirely sure about its purpose.</p> <h2><span>Display and Editing Options</span></h2> <p>There are three display options:</p> <ol> <li>Textual display, via <tt>getAsText</tt>.</li> <li>Combobox display, via <tt>getAsText</tt> and <tt>String[] getTags</tt>.</li> <li>Custom component display, via <tt>isPaintable</tt> and <tt>paintRectangle</tt>.</li> </ol> <p>Similarly, there are three editing options:</p> <ol> <li>Textual editing, via <tt>setAsText</tt>.</li> <li>Combobox editing, via <tt>setAsText</tt> and <tt>String[] getTags</tt>.</li> <li>Custom component editing, via <tt>supportsCustomEditor</tt> and <tt>Component getCustomEditor</tt>.</li> </ol> <h2><span>Standard Support Class</span></h2> <p>The default implementation is the <tt><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/PropertyEditorSupport.html">PropertyEditorSupport</a></tt> class, which has simple text editing support.</p> <ul> <li>Allows customizing the source of <tt>PropertyChangeEvent</tt>s: this defaults to the class, but also may be set directly via the <em>&quot;source&quot;</em> bean property.</li> <li>Supports <tt>getAsText</tt> and <tt>setAsText</tt>, but an exception is thrown in the <em>set</em> case unless the value is a <tt>String</tt>.</li> <li>Does not support custom editors or tags.</li> </ul> <h2><span>Package Implementations</span></h2> <p>Several editors are already included within the <tt><a href="http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules-sun/beans/sun.beans.editors.htm">sun.beans.editors</a></tt> package. Most of these support textual editing only. Exceptions are noted.</p> <ul> <li><tt>BooleanEditor</tt> (tags)</li> <li><tt>ByteEditor</tt></li> <li><tt>ColorEditor</tt> (panel)</li> <li><tt>DoubleEditor</tt></li> <li><tt>EnumEditor</tt> (tags)</li> <li><tt>FloatEditor</tt></li> <li><tt>FontEditor</tt> (panel)</li> <li><tt>IntegerEditor</tt></li> <li><tt>LongEditor</tt></li> <li><tt>NumberEditor</tt></li> <li><tt>ShortEditor</tt></li> <li><tt>StringEditor</tt></li> </ul> <h2><span>Customizers</span></h2> <p>The <tt><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Customizer.html">Customizer</a></tt> interface has basic <tt>PropertyChangeEvent</tt> support, as well as a <tt>setObject(Object bean)</tt> method that sets the argument based on the value within the customizer. These are intended to provide for more advanced customization capabilities.</p> <p>by <span class="printuser avatarhover"><a href="http://www.wikidot.com/user:info/elishapeterson" ><img class="small" src="http://www.wikidot.com/avatar.php?userid=59229&amp;amp;size=small&amp;amp;timestamp=1780994286" alt="elishapeterson" style="background-image:url(http://www.wikidot.com/userkarma.php?u=59229)" /></a><a href="http://www.wikidot.com/user:info/elishapeterson" >elishapeterson</a></span></p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://elishapeterson.wikidot.com/technotes:dynamic-latex-with-codecogs</guid>
				<title>Dynamic LaTeX with CodeCogs</title>
				<link>http://elishapeterson.wikidot.com/technotes:dynamic-latex-with-codecogs</link>
				<description>

&lt;p&gt;Elisha Peterson, United States Military Academy&lt;/p&gt;
&lt;p&gt;by &lt;span class=&quot;printuser avatarhover&quot;&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/avatar.php?userid=59229&amp;amp;amp;size=small&amp;amp;amp;timestamp=1780994286&quot; alt=&quot;elishapeterson&quot; style=&quot;background-image:url(http://www.wikidot.com/userkarma.php?u=59229)&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;elishapeterson&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
				<pubDate>Sun, 22 Nov 2009 12:37:01 +0000</pubDate>
												<content:encoded>
					<![CDATA[
						 <p>Elisha Peterson, United States Military Academy</p> <div class="content-separator" style="display: none:"></div> <p>This note describes how to generate LaTeX images in an html document using a web service&#8230; all it takes is inserting the LaTeX into an image link.</p> <div class="content-separator" style="display: none:"></div> <div class="content-separator" style="display: none:"></div> <p>Sometimes when writing your own webpages, it's quite a pain to get LaTeX to show up on your website. There are a number of ways to do this. (On this <em>Wikidot</em> site, the LaTeX is generated from the wiki markup.) Without that option, using the <a href="http://www.codecogs.com">http://www.codecogs.com</a> web service is one of the easiest and quickest techniques I have used.</p> <strong>Example:</strong> <img src="http://www.codecogs.com/gif.latex?2^nx^i" alt="gif.latex?2^nx^i" class="image" /> is generated by linking to the image <tt><a href="http://www.codecogs.com/gif.latex?2^nx^i">http://www.codecogs.com/gif.latex?2^nx^i</a></tt>, which in html looks like <div class="code"> <pre><code>&lt;image src=&quot;http://www.codecogs.com/gif.latex?2^nx^i&quot;&gt;&lt;/image&gt;</code></pre></div> <br /> This also works to generate <tt>.png</tt>, <tt>.swf</tt>, and <tt>.pdf</tt> images. Here is the flash example (<tt>.swf</tt>), which can be resized beautifully: <div class="code"> <pre><code>&lt;embed width=&quot;600&quot; height=&quot;100&quot; src=&quot;http://www.codecogs.com/swf.latex?1+sin(x)&quot; quality=&quot;high&quot; pluginspage=&quot;http://www.macromedia.com/go/getflashplayer&quot; align=&quot;top&quot; scale=&quot;showall&quot; wmode=&quot;window&quot; devicefont=&quot;false&quot; bgcolor=&quot;#ffffff&quot; menu=&quot;true&quot; allowFullScreen=&quot;true&quot; type=&quot;application/x-shockwave-flash&quot;&gt;&lt;/embed&gt;</code></pre></div> <br /> <iframe src="http://www.codecogs.com/swf.latex?1+sin(x)" align="" frameborder="" height="" scrolling="" width="" class="" style=""></iframe><br /> See the website (<a href="http://www.codecogs.com">http://www.codecogs.com</a>) for more information. <p>by <span class="printuser avatarhover"><a href="http://www.wikidot.com/user:info/elishapeterson" ><img class="small" src="http://www.wikidot.com/avatar.php?userid=59229&amp;amp;size=small&amp;amp;timestamp=1780994286" alt="elishapeterson" style="background-image:url(http://www.wikidot.com/userkarma.php?u=59229)" /></a><a href="http://www.wikidot.com/user:info/elishapeterson" >elishapeterson</a></span></p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://elishapeterson.wikidot.com/technotes:3d-graphics-blaise</guid>
				<title>3D Graphics (Blaise)</title>
				<link>http://elishapeterson.wikidot.com/technotes:3d-graphics-blaise</link>
				<description>

&lt;p&gt;Elisha Peterson, United States Military Academy&lt;/p&gt;
&lt;p&gt;by &lt;span class=&quot;printuser avatarhover&quot;&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/avatar.php?userid=59229&amp;amp;amp;size=small&amp;amp;amp;timestamp=1780994286&quot; alt=&quot;elishapeterson&quot; style=&quot;background-image:url(http://www.wikidot.com/userkarma.php?u=59229)&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;elishapeterson&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
				<pubDate>Wed, 14 Jan 2009 12:26:47 +0000</pubDate>
												<content:encoded>
					<![CDATA[
						 <p>Elisha Peterson, United States Military Academy</p> <div class="content-separator" style="display: none:"></div> <p>This preliminary note will describe how I go about rendering points, curves, and surfaces in three dimensions for <a href="http://www.dean.usma.edu/math/people/peterson/blaise/">blaise</a>. See <a href="http://blaisemath.googlecode.com/">http://blaisemath.googlecode.com/</a> for the complete source code.</p> <div class="content-separator" style="display: none:"></div> <table style="margin:0; padding:0"> <tr> <td style="margin:0; padding:0"> <div id="toc"> <div id="toc-action-bar"><a href="javascript:;" >Fold</a><a style="display: none" href="javascript:;" >Unfold</a></div> <div class="title">Table of Contents</div> <div id="toc-list"> <div style="margin-left: 1em;"><a href="#toc0">Scene Modeling and Projections</a></div> <div style="margin-left: 2em;"><a href="#toc1">Camera Setup</a></div> <div style="margin-left: 2em;"><a href="#toc2">Projection of an Arbitrary Point</a></div> <div style="margin-left: 2em;"><a href="#toc3">Pixel Conversion</a></div> <div style="margin-left: 2em;"><a href="#toc4">Inverse Transformation</a></div> <div style="margin-left: 1em;"><a href="#toc5">Multiple Viewpoints &amp; Anaglyphs</a></div> <div style="margin-left: 2em;"><a href="#toc6">Color Filtering</a></div> <div style="margin-left: 1em;"><a href="#toc7">Rotations</a></div> <div style="margin-left: 1em;"><a href="#toc8">Source Code</a></div> <div style="margin-left: 1em;"><a href="#toc9">References</a></div> </div> </div> </td> </tr> </table> <div class="content-separator" style="display: none:"></div> <h1><span>Scene Modeling and Projections</span></h1> <p>In what follows, we use lowercase (<em>d</em>) for scalars, uppercase (<em>P</em>) for points in space, lowercase bold (<strong>x</strong>) for arbitrary vectors, and uppercase bold (<strong>T</strong>) for unit vectors.</p> <h2><span>Camera Setup</span></h2> <p>Let <em>C</em> be the camera location, with orientation <strong>T</strong>, <strong>N</strong>, <strong>B</strong>, where <strong>T</strong> is the direction the camera is facing, <strong>N</strong> is the &quot;up&quot; direction, and <strong>B</strong>=<strong>T</strong>x<strong>N</strong> is the perpendicular direction. Assume the viewing screen is located at a distance <em>d</em> from the camera, and is centered relative to the camera. Thus, we assume the the viewscreen is perpendicular to <strong>T</strong> and the center <em>V</em> of the viewscreen is such that <span class="math-inline">$\overrightarrow{CV} = d \vec{\mathbf{T}}$</span>.</p> <p>As indicated above, the center of the viewscreen is <span class="math-inline">$V=C+d\vec{\mathbf{T}}$</span>. Since the screen is parallel to <strong>B</strong> and <strong>N</strong>, an arbitrary point on the screen may be written as <em>V</em>+<em>x</em><strong>B</strong>+<em>y</em><strong>N</strong>. In this case, we call <span class="math-inline">$\langle x,y\rangle_\pi$</span>, the <strong>viewscreen coordinates</strong>. Let <em>w</em> be the width of the viewscreen and <em>h</em> the height; the viewscreen's four corner points are <span class="math-inline">$V\pm\frac{w}{2}\vec{\mathbf{B}}\pm\frac{h}{2}\vec{\mathbf{N}}$</span>. In general, a vector <strong>y</strong> starting at the camera may be expressed as</p> <span class="equation-number">(1)</span> <div class="math-equation" id="equation-529073-1">\begin{align} \vec{\mathbf{y}} = (\vec{\mathbf{y}}\cdot\vec{\mathbf{T}})\vec{\mathbf{T}} + (\vec{\mathbf{y}}\cdot\vec{\mathbf{N}})\vec{\mathbf{N}} + (\vec{\mathbf{y}}\cdot\vec{\mathbf{B}})\vec{\mathbf{B}}. \end{align}</div> <p>If the vector ends on the viewscreen, its viewscreen coordinates are therefore <span class="math-inline">$\langle\vec{\mathbf{y}}\cdot\vec{\mathbf{B}}, \vec{\mathbf{y}}\cdot\vec{\mathbf{N}}\rangle_\pi$</span>.</p> <h2><span>Projection of an Arbitrary Point</span></h2> <p>Let <em>A<sub>π</sub></em> denote the projection of a point <em>A</em> onto the viewscreen; this is where the viewscreen intersects the line between <em>A</em> and <em>C</em>. Let <span class="math-inline">$\vec{\mathbf{x}}=\overrightarrow{CA}$</span> be the vector pointing from the camera <em>C</em> to the point. We can express this point as <em>A<sub>π</sub></em>=<em>C</em>+λ<strong>x</strong> for some λ. The value λ is easiest to compute by focusing on the <strong>T</strong> direction. Since <em>d</em> is the point from <em>C</em> to the plane, and <strong>x</strong>·<strong>T</strong> is the distance in the same direction from the camera to the point, one has simply <span class="math-inline">$\lambda=d/\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}$</span>. So</p> <span class="equation-number">(2)</span> <div class="math-equation" id="equation-529073-2">\begin{align} A_\pi = C + \left(\frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}\right)\overrightarrow{CA}. \end{align}</div> <p>In viewscreen coordinates, one considers the vector <span class="math-inline">$\overrightarrow{CA_\pi}=\left(\frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}\right)\overrightarrow{CA}$</span>, so that the point <em>A</em> has representation in viewscreen coordinates as follows:</p> <span class="equation-number">(3)</span> <div class="math-equation" id="equation-529073-3">\begin{align} A_\pi \quad \longleftrightarrow \quad \frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}} \left\langle\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}, \vec{\mathbf{x}}\cdot\vec{\mathbf{N}}\right\rangle_\pi = d \left\langle\frac{\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}, \frac{\vec{\mathbf{x}}\cdot\vec{\mathbf{N}}}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}\right\rangle_\pi. \end{align}</div> <h2><span>Pixel Conversion</span></h2> <p>As a final step, one must convert the viewscreen coordinates <span class="math-inline">$\langle x,y\rangle_\pi$</span> to window coordinates. If the window has pixels ranging from <span class="math-inline">$(0,0)$</span> in the upper-lefthand corner to <span class="math-inline">$(W,H)$</span> in the lower-righthand corner (the usual way of marking window coordinates), then the pixel coordinates of this point are</p> <span class="equation-number">(4)</span> <div class="math-equation" id="equation-529073-4">\begin{align} \langle x,y\rangle_\pi \quad \longleftrightarrow \quad \left(\tfrac{W}{w}(x+\tfrac{w}{2}), -\tfrac{H}{h}(y-\tfrac{h}{2})\right). \end{align}</div> <p>Note that the ratios <span class="math-inline">$\frac{W}{w}$</span> and <span class="math-inline">$\frac{H}{h}$</span> will typically be the same and represent <em>the number of pixels per unit length</em>. If this is represented by a constant factor η, the formula becomes <span class="math-inline">$\eta\left(x+\frac{w}{2},-y+\frac{h}{2}\right) = \left(\frac{W}{2} + \eta x, \frac{H}{2}-\eta y\right)$</span>.</p> <p>Combining the above discussion, if <strong>x</strong> represents the vector from <em>C</em> to <em>A</em>, then the pixel coordinates of <em>A</em>, as it appears on the viewscreen, should be given by the mapping</p> <span class="equation-number">(5)</span> <div class="math-equation" id="equation-529073-5">\begin{align} A \quad \longmapsto \quad \left(\tfrac{W}{2}, \tfrac{H}{2}\right) + \frac{\eta d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}} \left(\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}, -\vec{\mathbf{x}}\cdot\vec{\mathbf{N}}\right). \end{align}</div> <h2><span>Inverse Transformation</span></h2> <p>A point (<em>x<sub>p</sub></em>,<em>y<sub>p</sub></em>) in pixel coordinates transforms in viewscreen coordinates to <span class="math-inline">$\left\langle\frac{x_p-W/2}{\eta}, -\frac{y_p-H/2}{\eta}\right\rangle = \left\langle\frac{x_p}{\eta}-\frac{w}{2}, -\frac{y_p}{\eta}+\frac{h}{2}\right\rangle$</span>. Hence, the corresponding point on the viewscreen is</p> <span class="equation-number">(6)</span> <div class="math-equation" id="equation-529073-6">\begin{align} (x_p,y_p) \quad \longmapsto \quad V + \left(\frac{x_p}{\eta}-\frac{w}{2}\right)\vec{\mathbf{B}} + \left(\frac{y_p}{\eta}+\frac{h}{2}\right)\vec{\mathbf{N}}. \end{align}</div> <p>The full line of potential inverse mappings is</p> <span class="equation-number">(7)</span> <div class="math-equation" id="equation-529073-7">\begin{align} C + t \left(d\vec{\mathbf{T}} + \left(\frac{x_p}{\eta}-\frac{w}{2}\right)\vec{\mathbf{B}} + \left(\frac{y_p}{\eta}+\frac{h}{2}\right)\vec{\mathbf{N}}\right). \end{align}</div> <h1><span>Multiple Viewpoints &amp; Anaglyphs</span></h1> <p><a href="http://en.wikipedia.org/wiki/anaglyph_image" >Anaglyphs</a> are images rendered separately for each eye, typically with a different color so they can be seen using 3D glasses. In this case, one must use two different projections to obtain the image of the two cameras.</p> <p>We will look at how the above formulas change for a camera <em>C<sub>ε</sub></em> that is shifted from the original location so that <em>C<sub>ε</sub></em> = <em>C</em> + <strong>ε</strong> = <em>C</em> + <em>ε<sub>x</sub></em><strong>B</strong> + <em>ε<sub>y</sub></em><strong>N</strong> (a translation parallel to the viewscreen). Let <em>A<sub>πε</sub></em> represent the point on the viewscreen as seen by the new camera. Note that <em>V<sub>ε</sub></em> = <em>V</em> + <strong>ε</strong> as well, since the translation is parallel to the viewscreen. One therefore has</p> <span class="equation-number">(8)</span> <div class="math-equation" id="equation-529073-8">\begin{align} A_{\pi\epsilon} \quad \longleftrightarrow \quad \frac{d}{\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{T}}} \left\langle\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{B}}, \vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{N}}\right\rangle_{\pi\varepsilon} = \frac{d}{\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{T}}} \left\langle\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{B}}, \vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{N}}\right\rangle_\pi + \left\langle\varepsilon_x,\varepsilon_y\right\rangle_\pi. \end{align}</div> <p>This can be further simplifed. Since <span class="math-inline">$\vec{\mathbf{x}}_\varepsilon=\overrightarrow{C_\varepsilon A}=\overrightarrow{CA}-\mathbf{\vec\varepsilon}=\vec{\mathbf{x}}-\mathbf{\vec\varepsilon}$</span>, we have <span class="math-inline">$\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{T}}=\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}$</span>, <span class="math-inline">$\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{B}}=\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}-\varepsilon_x$</span>, and <span class="math-inline">$\vec{\mathbf{x}}_\varepsilon\cdot\vec{\mathbf{N}}=\vec{\mathbf{x}}\cdot\vec{\mathbf{N}}-\varepsilon_y$</span>. This shows that</p> <span class="equation-number">(9)</span> <div class="math-equation" id="equation-529073-9">\begin{align} A_{\pi\epsilon} \quad \longleftrightarrow \quad \frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}} \left\langle\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}-\varepsilon_x, \vec{\mathbf{x}}\cdot\vec{\mathbf{N}}-\varepsilon_y\right\rangle_\pi + \left\langle\varepsilon_x,\varepsilon_y\right\rangle_\pi = \frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}} \left\langle\vec{\mathbf{x}}\cdot\vec{\mathbf{B}}, \vec{\mathbf{x}}\cdot\vec{\mathbf{N}}\right\rangle_\pi + \left(1-\frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}\right)\left\langle\varepsilon_x,\varepsilon_y\rangle_\pi. \end{align}</div> <p>Therefore, the parallel camera which is shifted by <strong>ε</strong> will give points that are shifted by (1-λ)<strong>ε</strong> (with the vector <strong>ε</strong> considered in the proper coordinate system.)</p> <p>When reduced to the window's coordinate system, recall that <span class="math-inline">$\langle x,y\rangle_\pi \leftrightarrow (\frac{W}{2},\frac{H}{2}) + \eta(x,-y)$</span>, where <span class="math-inline">$\eta=\frac{W}{w}=\frac{H}{h}$</span> is the number of pixels per unit length. Therefore, one has</p> <span class="equation-number">(10)</span> <div class="math-equation" id="equation-529073-10">\begin{align} \langle x+\Delta x,y+\Delta y\rangle \leftrightarrow (\tfrac{W}{2},\tfrac{H}{2}) + \eta(x,-y) + \eta(dx,-dy). \end{align}</div> <p>So the endstate of a camera shift is a change of</p> <span class="equation-number">(11)</span> <div class="math-equation" id="equation-529073-11">\begin{align} \eta(1-\lambda)\langle\varepsilon_x, \varepsilon_y\rangle = \eta\left(1-\frac{d}{\vec{\mathbf{x}}\cdot\vec{\mathbf{T}}}\right)\vec{\mathbf{\varepsilon}} \end{align}</div> <p>in window coordinates.</p> <p>A typical two-camera construction (e.g. our eyes) will have cameras at <em>C</em> ± <em>ε</em><strong>B</strong> for a scalar <em>ε</em> representing one half the distance between the eyes. So in the viewscreen coordinates, the <em>x</em> values are shifted by ±(1-λ)<em>ε</em>. And in the window coordinates, the values are shifted by ±η(1-λ)<em>ε</em>.</p> <h2><span>Color Filtering</span></h2> <p>Two-color 3d images work by providing two images with different color schemes, that depend upon the color of lenses in a pair of 3d glasses. When looking through a red lens, one cannot see the color red; therefore, the image seen through the red lens should be comprised of colors using only green and blue channels. When looking through a second lens (e.g. cyan), one may use the red channel. For example, if the left lens is red, the image projected by the left camera (left eye) should be colored with only green and blue. Similarly, if the right lens is cyan, the image projected by the right camera (right eye) should be colored by red only.</p> <h1><span>Rotations</span></h1> <p>Rotations are achieved by the following formula [<a href="javascript:;" class="bibcite" id="bibcite-170281-1-41434a" >1</a>]:</p> <span class="equation-number">(12)</span> <div class="math-equation" id="equation-529073-12">\begin{align} \mathbf{r} \longmapsto \mathbf{r}\cos\Phi + \hat{\mathbf{n}}(\hat{\mathbf{n}}\cdot\mathbf{r})(1-\cos\Phi)+(\mathbf{r}\times\hat{\mathbf{n}})\sin\Phi. \end{align}</div> <p>Here, the vector <strong>r</strong> is being rotated by an angle of Φ around the axis <strong>n</strong>.</p> <p>The motion for rotating the figure is &quot;dragging&quot; the screen. The natural interaction is to treat the rendered scene as existing inside a ball, and rotating that ball around its center to see how the image changes. In our case, the center of the ball will be denoted by <em>S</em>=<em>C</em>+Δ<strong>T</strong>, where Δ&gt;<em>d</em> and the radius of that ball will be Δ–<em>d</em>. (Recall that <em>d</em> is the distance to the viewscreen. Think of Δ as the distance to the center of the scene of interest, since it defines the point of rotation.) When the user drags the mouse, points are created for the start and end of that motion, call them <em>R</em><sub>1</sub> and <em>R</em><sub>2</sub>. Let <strong>r</strong><sub>i</sub> be the unit vector <span class="math-inline">$\overrightarrow{SR_i}/\overrightarrow{SR_i}$</span> formed by normalizing the vector from <em>S</em> to <em>R</em><sub>i</sub>. Then the axis of rotation is the unit vector</p> <span class="equation-number">(13)</span> <div class="math-equation" id="equation-529073-13">\begin{align} \hat{\mathbf{n}}=\hat{\mathbf{r}}_1\times\hat{\mathbf{r}}_2, \end{align}</div> <p>and the angle of rotation is given by the dot product formula</p> <span class="equation-number">(14)</span> <div class="math-equation" id="equation-529073-14">\begin{align} \cos\Phi = \hat{\mathbf{r}}_1\cdot\hat{\mathbf{r}}_2. \end{align}</div> <p>The Euler rotation formula can then be used to transform the camera's <strong>T</strong>,<strong>N</strong>,<strong>B</strong> frame, and thereby recenter the camera to the location <em>S</em>–Δ<strong>T</strong> (in the transformed <strong>T</strong>).</p> <p>In some cases, it may be preferable to allow for the user to make an image rotate faster or slower. This is easily accomplished by scaling the rotation angle Φ to some <em>C</em>Φ. One may also animate a rotation, by keeping the axis of rotation fixed, and increasing the rotation by a small angle dΦ at each time step.</p> <h1><span>Source Code</span></h1> <p><em>Coming soon&#8230; see page history for old source code.</em></p> <h1><span>References</span></h1> <div class="bibitems"> <div class="bibitem" id="bibitem-170281-1">1. Weisstein, Eric W. &quot;Rotation Formula.&quot; From MathWorld&#8212;A Wolfram Web Resource. <a href="http://mathworld.wolfram.com/RotationFormula.html">http://mathworld.wolfram.com/RotationFormula.html</a></div> </div> <p>by <span class="printuser avatarhover"><a href="http://www.wikidot.com/user:info/elishapeterson" ><img class="small" src="http://www.wikidot.com/avatar.php?userid=59229&amp;amp;size=small&amp;amp;timestamp=1780994286" alt="elishapeterson" style="background-image:url(http://www.wikidot.com/userkarma.php?u=59229)" /></a><a href="http://www.wikidot.com/user:info/elishapeterson" >elishapeterson</a></span></p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://elishapeterson.wikidot.com/technotes:xml-output-with-jaxp</guid>
				<title>XML Output with JAXB 2.0</title>
				<link>http://elishapeterson.wikidot.com/technotes:xml-output-with-jaxp</link>
				<description>

&lt;p&gt;Elisha Peterson&lt;/p&gt;
&lt;p&gt;by &lt;span class=&quot;printuser avatarhover&quot;&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/avatar.php?userid=59229&amp;amp;amp;size=small&amp;amp;amp;timestamp=1780994287&quot; alt=&quot;elishapeterson&quot; style=&quot;background-image:url(http://www.wikidot.com/userkarma.php?u=59229)&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;elishapeterson&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
				<pubDate>Thu, 18 Sep 2008 20:33:31 +0000</pubDate>
												<content:encoded>
					<![CDATA[
						 <p>Elisha Peterson</p> <div class="content-separator" style="display: none:"></div> <p>This note covers some basics of JAXB (Java API for XML Binding). See also the <a href="http://en.wikipedia.org/wiki/JAXB" >Wikipedia entry</a>, the <a href="https://jaxb.dev.java.net/">development site</a>, a <a href="http://blogs.sun.com/CoreJavaTechTips/entry/exchanging_data_with_xml_and">tutorial by Sun</a>, and a <a href="http://www.caucho.com/resin/doc/jaxb-annotations.xtp">description of JAXB annotations</a>.</p> <div class="content-separator" style="display: none:"></div> <p><strong>Keywords:</strong> Java, XML, JAXB, JavaBeans, Binding</p> <table style="margin:0; padding:0"> <tr> <td style="margin:0; padding:0"> <div id="toc"> <div id="toc-action-bar"><a href="javascript:;" >Fold</a><a style="display: none" href="javascript:;" >Unfold</a></div> <div class="title">Table of Contents</div> <div id="toc-list"> <div style="margin-left: 1em;"><a href="#toc0">Marshalling</a></div> <div style="margin-left: 2em;"><a href="#toc1">Automating the Process</a></div> <div style="margin-left: 2em;"><a href="#toc2">Working with Vectors of Data</a></div> <div style="margin-left: 1em;"><a href="#toc3">Unmarshalling</a></div> <div style="margin-left: 2em;"><a href="#toc4">Source Code</a></div> <div style="margin-left: 1em;"><a href="#toc5">Working with Inheritance</a></div> <div style="margin-left: 2em;"><a href="#toc6">First Technique</a></div> <div style="margin-left: 3em;"><a href="#toc7">Collections</a></div> <div style="margin-left: 2em;"><a href="#toc8">Second Technique</a></div> <div style="margin-left: 1em;"><a href="#toc9">Customizing the XML</a></div> </div> </div> </td> </tr> </table> <div class="content-separator" style="display: none:"></div> <h1><span>Marshalling</span></h1> <p>Suppose you want to automatically generate an XML file for your object. In JAXB, this process is called &quot;marshalling&quot;. The idea is to annotate the appropriate fields that you want to store in the XML file. It handles the file-generation automatically. Here's a simple example:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Main</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">; @</span><span class="hl-identifier">XmlElement</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-code">; @</span><span class="hl-identifier">XmlElement</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">width</span><span class="hl-code">; </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(){</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Spot</span><span class="hl-quotes">&quot;</span><span class="hl-code">;</span><span class="hl-identifier">height</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-identifier">width</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">throws</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">JAXBContext</span><span class="hl-code"> </span><span class="hl-identifier">jc</span><span class="hl-code"> = </span><span class="hl-identifier">JAXBContext</span><span class="hl-code">.</span><span class="hl-identifier">newInstance</span><span class="hl-brackets">(</span><span class="hl-identifier">Dog</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">jc</span><span class="hl-code">.</span><span class="hl-identifier">createMarshaller</span><span class="hl-brackets">()</span><span class="hl-code">.</span><span class="hl-identifier">marshal</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FileOutputStream</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">dog.xml</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span></pre></div> </div> <p>Here <tt>@XmlRootElement</tt> identifies the root element of the XML document tree (DOM), which is required for &quot;well-formed&quot; SML. <tt>@XmlAttribute</tt> specifies an attribute supplied &quot;inline&quot; with the object definition, while <tt>@XmlElement</tt> identifies attributes that are specified separately. Running this file produces the following output:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;?</span><span class="hl-reserved">xml</span><span class="hl-code"> </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.0</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">encoding</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">UTF-8</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">standalone</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">yes</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-brackets">?&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Spot</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code">10.0</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">width</span><span class="hl-brackets">&gt;</span><span class="hl-code">10.0</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">width</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">dog</span><span class="hl-brackets">&gt;</span></pre></div> </div> <h2><span>Automating the Process</span></h2> <p>You can shorten the code a bit by telling the marshaller to automatically put fields into the output file. The command for this is <tt>@XmlAccessorType</tt>, as shown in this abbreviated version:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Main</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">; </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-code">; </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">width</span><span class="hl-code">; </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(){</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Spot</span><span class="hl-quotes">&quot;</span><span class="hl-code">;</span><span class="hl-identifier">height</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-identifier">width</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">throws</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">JAXBContext</span><span class="hl-code"> </span><span class="hl-identifier">jc</span><span class="hl-code"> = </span><span class="hl-identifier">JAXBContext</span><span class="hl-code">.</span><span class="hl-identifier">newInstance</span><span class="hl-brackets">(</span><span class="hl-identifier">Dog</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">jc</span><span class="hl-code">.</span><span class="hl-identifier">createMarshaller</span><span class="hl-brackets">()</span><span class="hl-code">.</span><span class="hl-identifier">marshal</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FileOutputStream</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">dog.xml</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span></pre></div> </div> <h2><span>Working with Vectors of Data</span></h2> <p>We can easily modify the above code to work with deeper nesting structures and vectors of data. Here is the code for marshalling a &quot;pack&quot; of dogs:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Main</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Pack</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Vector</span><span class="hl-code">&lt;</span><span class="hl-identifier">Dog</span><span class="hl-code">&gt; </span><span class="hl-identifier">dog</span><span class="hl-code">; </span><span class="hl-identifier">Pack</span><span class="hl-brackets">(){</span><span class="hl-identifier">dog</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Vector</span><span class="hl-code">&lt;</span><span class="hl-identifier">Dog</span><span class="hl-code">&gt;</span><span class="hl-brackets">()</span><span class="hl-code">;</span><span class="hl-identifier">dog</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">())</span><span class="hl-code">;</span><span class="hl-identifier">dog</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">())</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">; @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-code">; @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">width</span><span class="hl-code">; @</span><span class="hl-identifier">XmlElement</span><span class="hl-code"> </span><span class="hl-identifier">Coat</span><span class="hl-code"> </span><span class="hl-identifier">coat</span><span class="hl-code">; </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(){</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Spot</span><span class="hl-quotes">&quot;</span><span class="hl-code">;</span><span class="hl-identifier">height</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-identifier">width</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-identifier">coat</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Coat</span><span class="hl-brackets">()</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Coat</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlElement</span><span class="hl-code"> </span><span class="hl-identifier">ColorX</span><span class="hl-code"> </span><span class="hl-identifier">color</span><span class="hl-code">; @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">thickness</span><span class="hl-code">; @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">type</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Coat</span><span class="hl-brackets">(){</span><span class="hl-identifier">color</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ColorX</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-identifier">thickness</span><span class="hl-code">=</span><span class="hl-number">2</span><span class="hl-code">; </span><span class="hl-identifier">type</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">solid</span><span class="hl-quotes">&quot;</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ColorX</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Color</span><span class="hl-code"> </span><span class="hl-identifier">col</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">ColorX</span><span class="hl-brackets">(){</span><span class="hl-identifier">col</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Color</span><span class="hl-brackets">(</span><span class="hl-number">33</span><span class="hl-code">,</span><span class="hl-number">33</span><span class="hl-code">,</span><span class="hl-number">33</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">getRed</span><span class="hl-brackets">(){</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getRed</span><span class="hl-brackets">()</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setRed</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">r</span><span class="hl-brackets">){</span><span class="hl-identifier">col</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Color</span><span class="hl-brackets">(</span><span class="hl-identifier">r</span><span class="hl-code">,</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getGreen</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getBlue</span><span class="hl-brackets">())</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">getGreen</span><span class="hl-brackets">(){</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getGreen</span><span class="hl-brackets">()</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setGreen</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">){</span><span class="hl-identifier">col</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Color</span><span class="hl-brackets">(</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getRed</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-identifier">g</span><span class="hl-code">,</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getBlue</span><span class="hl-brackets">())</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAttribute</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">getBlue</span><span class="hl-brackets">(){</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getBlue</span><span class="hl-brackets">()</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setBlue</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-brackets">){</span><span class="hl-identifier">col</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Color</span><span class="hl-brackets">(</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getRed</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-identifier">col</span><span class="hl-code">.</span><span class="hl-identifier">getGreen</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">throws</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">JAXBContext</span><span class="hl-code"> </span><span class="hl-identifier">jc</span><span class="hl-code"> = </span><span class="hl-identifier">JAXBContext</span><span class="hl-code">.</span><span class="hl-identifier">newInstance</span><span class="hl-brackets">(</span><span class="hl-identifier">Pack</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">jc</span><span class="hl-code">.</span><span class="hl-identifier">createMarshaller</span><span class="hl-brackets">()</span><span class="hl-code">.</span><span class="hl-identifier">marshal</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Pack</span><span class="hl-brackets">()</span><span class="hl-code">,</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FileOutputStream</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">dog.xml</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> <p>Note that saving a color in terms of its components requires creating &quot;get&quot; and &quot;set&quot; methods for the color components (making it a &quot;<a href="http://en.wikipedia.org/wiki/JavaBeans" >Java bean</a>&quot;). A better approach is to use the <tt>@XmlJavaTypeAdapter</tt> (discussed later). Here is the resulting <tt>XML</tt> file, with the pack initialized to contain two of the default dogs:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;</span><span class="hl-reserved">pack</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> </span><span class="hl-var">width</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">10.0</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">height</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">10.0</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Spot</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">coat</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">solid</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">thickness</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">2.0</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">color</span><span class="hl-code"> </span><span class="hl-var">red</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">33</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">green</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">33</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">blue</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">33</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">coat</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">dog</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> </span><span class="hl-var">width</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">10.0</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">height</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">10.0</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Spot</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">coat</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">solid</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">thickness</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">2.0</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">color</span><span class="hl-code"> </span><span class="hl-var">red</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">33</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">green</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">33</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">blue</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">33</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">coat</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">dog</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">pack</span><span class="hl-brackets">&gt;</span></pre></div> </div> <h1><span>Unmarshalling</span></h1> <p>The process of returning the XML code into objects is called &quot;unmarshalling&quot;. If the XML file <tt>doggin.xml</tt> is edited, maintaining the same structure, then one can read the data back in just as easily. Here is the code to read the dogs back into a <tt>Pack</tt> object:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-code"> </span><span class="hl-identifier">Pack</span><span class="hl-code"> </span><span class="hl-identifier">inputPack</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">Pack</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-identifier">jc</span><span class="hl-code">.</span><span class="hl-identifier">createUnmarshaller</span><span class="hl-brackets">()</span><span class="hl-code">.</span><span class="hl-identifier">unmarshal</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FileInputStream</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">doggin.xml</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">))</span><span class="hl-code">;</span></pre></div> </div> <br /> It seems easy, but one must be design the Java class carefully to ensure that things end up back in the proper places. Here is the order in which the unmarshal command calls Java methods: <ol> <li>Call to no-argument constructor to create the class</li> <li>Call to get and set methods in the order they appear within the XML</li> </ol> <p>It is a good idea to design constructors with arguments to work the same way within your code (this avoids bugs).</p> <h2><span>Source Code</span></h2> <p>Here is a sample input file: <a href="http://elishapeterson.wdfiles.com/local--files/technotes:xml-output-with-jaxp/doggin.xml">doggin.xml</a>. You can modify it directly, and see the resulting change in the <tt>inputPack</tt> object. The complete source code with <tt>String</tt> outputs to test the process is: <a href="http://elishapeterson.wdfiles.com/local--files/technotes:xml-output-with-jaxp/Main.java">Main.java</a>.</p> <h1><span>Working with Inheritance</span></h1> <h2><span>First Technique</span></h2> <div class="wiki-note"> <p><span style="color:red">This section is partially incomplete/inaccurate. I am still trying to figure out how this works!</span></p> </div> <p>XML binding will usually take place with a declared class rather than the &quot;runtime&quot; class. One approach that works is annotating with a list of possible concrete types. Suppose <tt>Animal</tt> is an abstract class that might represent a <tt>Cat</tt> or <tt>Dog</tt> or other animal? One can annotate the corresponding property with <tt>@XmlElements</tt> as follows:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-code">@</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Zoo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlElements</span><span class="hl-brackets">({</span><span class="hl-code"> @</span><span class="hl-identifier">XmlElement</span><span class="hl-brackets">(</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">dog</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-identifier">type</span><span class="hl-code">=</span><span class="hl-identifier">Dog</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code">, @</span><span class="hl-identifier">XmlElement</span><span class="hl-brackets">(</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">cat</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-identifier">type</span><span class="hl-code">=</span><span class="hl-identifier">Cat</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">})</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Animal</span><span class="hl-code"> </span><span class="hl-identifier">animal</span><span class="hl-code">; ... </span><span class="hl-brackets">}</span></pre></div> </div> <br /> This will allow for multiple types of XML tags within the output's <tt>&lt;zoo&gt;</tt> tag: <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> ...</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span></pre></div> </div> <br /> or <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">cat</span><span class="hl-code"> ...</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span></pre></div> </div> <br /> are both acceptable. <p>The use of the <tt>@XmlElements</tt> tag makes the following differences in the XML:</p> <ul> <li>tag names are specific to the type of subclass of the <tt>Animal</tt> class</li> <li>any additional attributes or elements specified as part of the subclass are included in the XML</li> </ul> <p>Thus, one generally needs to use this technique to ensure that inheritance works properly.</p> <h3><span>Collections</span></h3> <p>How do you deal with structures such as <tt>Vector&lt;Animal&gt;</tt>? The process is similar:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-code">@</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Zoo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlElements</span><span class="hl-brackets">({</span><span class="hl-code"> @</span><span class="hl-identifier">XmlElement</span><span class="hl-brackets">(</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">dog</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-identifier">type</span><span class="hl-code">=</span><span class="hl-identifier">Dog</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code">, @</span><span class="hl-identifier">XmlElement</span><span class="hl-brackets">(</span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">cat</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-identifier">type</span><span class="hl-code">=</span><span class="hl-identifier">Cat</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">})</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Vector</span><span class="hl-code">&lt;</span><span class="hl-identifier">Animal</span><span class="hl-code">&gt; </span><span class="hl-identifier">animals</span><span class="hl-code">; ... </span><span class="hl-brackets">}</span></pre></div> </div> <br /> This will allow for multiple types of XML tags within the output's <tt>&lt;zoo&gt;</tt> tag: <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> ...</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog</span><span class="hl-code"> ...</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">cat</span><span class="hl-code"> ...</span><span class="hl-brackets">/&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">zoo</span><span class="hl-brackets">&gt;</span></pre></div> </div> <h2><span>Second Technique</span></h2> <p>Another approach is to register the descendant classes into the <tt>JAXBContext</tt> and to use the <tt>@XmlAnyElement</tt> annotation. Here's how. First, you'll need to annotate the subclasses as follows:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-code">@</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Zoo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAnyElement</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">Animal</span><span class="hl-code">&gt; </span><span class="hl-identifier">animal</span><span class="hl-code">; ... </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">interface</span><span class="hl-code"> </span><span class="hl-identifier">Animal</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> ... </span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Lion</span><span class="hl-code"> </span><span class="hl-reserved">implements</span><span class="hl-code"> </span><span class="hl-identifier">Animal</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">Simba</span><span class="hl-quotes">&quot;</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">// other subclasses of animal</span></pre></div> </div> <br /> Now when initializing the <tt>JAXBContext</tt> use: <div class="code"> <div class="hl-main"> <pre><span class="hl-identifier">JAXBContext</span><span class="hl-code"> </span><span class="hl-identifier">jc</span><span class="hl-code"> = </span><span class="hl-identifier">JAXBContext</span><span class="hl-code">.</span><span class="hl-identifier">newInstance</span><span class="hl-brackets">(</span><span class="hl-identifier">Zoo</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-code">, </span><span class="hl-identifier">Lion</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-code">, ...</span><span class="hl-brackets">)</span><span class="hl-code">;</span></pre></div> </div> <br /> This registers the subclasses with the context so it recognizes them during the marshalling process. <h1><span>Customizing the XML</span></h1> <p>The <tt>@XmlJavaTypeAdapter</tt> annotation allows one to &quot;intercept&quot; the marshalling process and so specify exactly what the <tt>XML</tt> will look like. This requires writing two additional classes: one for the adapted version of the class, and an adapter class that handles the conversion process. Here is an example that stores dogs at half their height:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-comment">// our original class</span><span class="hl-code"> @</span><span class="hl-identifier">XmlJavaTypeAdapter</span><span class="hl-brackets">(</span><span class="hl-identifier">DogAdapter</span><span class="hl-code">.</span><span class="hl-reserved">class</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">height</span><span class="hl-code"> = </span><span class="hl-identifier">height</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">// our adapted class</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">LittleDog</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-code">; </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">LittleDog</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{}</span><span class="hl-code"> </span><span class="hl-comment">// no-arg default constructor required for marshalling</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">LittleDog</span><span class="hl-brackets">(</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">height</span><span class="hl-code"> = </span><span class="hl-identifier">height</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">// this performs the class conversion</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DogAdapter</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">XmlAdapter</span><span class="hl-code">&lt;</span><span class="hl-identifier">LittleDog</span><span class="hl-code">, </span><span class="hl-identifier">Dog</span><span class="hl-code">&gt; </span><span class="hl-brackets">{</span><span class="hl-code"> @</span><span class="hl-identifier">Override</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">unmarshal</span><span class="hl-brackets">(</span><span class="hl-identifier">LittleDog</span><span class="hl-code"> </span><span class="hl-identifier">v</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">throws</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-identifier">v</span><span class="hl-code">.</span><span class="hl-identifier">height</span><span class="hl-code">*</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> @</span><span class="hl-identifier">Override</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">LittleDog</span><span class="hl-code"> </span><span class="hl-identifier">marshal</span><span class="hl-brackets">(</span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">v</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">throws</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">LittleDog</span><span class="hl-brackets">(</span><span class="hl-identifier">v</span><span class="hl-code">.</span><span class="hl-identifier">height</span><span class="hl-code">/</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">// the enclosing instance</span><span class="hl-code"> @</span><span class="hl-identifier">XmlRootElement</span><span class="hl-code"> @</span><span class="hl-identifier">XmlAccessorType</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlAccessType</span><span class="hl-code">.</span><span class="hl-identifier">FIELD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Pack</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">dog1</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-number">5.0</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">dog2</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-number">3.0</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> <p>Now when we perform the marshalling process, we get the following XML:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;</span><span class="hl-reserved">pack</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog1</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code">2.5</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">dog1</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">dog2</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code">1.5</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">height</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">dog2</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">pack</span><span class="hl-brackets">&gt;</span></pre></div> </div> <p>by <span class="printuser avatarhover"><a href="http://www.wikidot.com/user:info/elishapeterson" ><img class="small" src="http://www.wikidot.com/avatar.php?userid=59229&amp;amp;size=small&amp;amp;timestamp=1780994287" alt="elishapeterson" style="background-image:url(http://www.wikidot.com/userkarma.php?u=59229)" /></a><a href="http://www.wikidot.com/user:info/elishapeterson" >elishapeterson</a></span></p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://elishapeterson.wikidot.com/technotes:geogebra-cobwebs</guid>
				<title>Creating a Dynamic Cobwebbing Demonstration with Geogebra</title>
				<link>http://elishapeterson.wikidot.com/technotes:geogebra-cobwebs</link>
				<description>

&lt;p&gt;Elisha Peterson&lt;br /&gt;
United States Military Academy&lt;/p&gt;
&lt;p&gt;by &lt;span class=&quot;printuser avatarhover&quot;&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;&lt;img class=&quot;small&quot; src=&quot;http://www.wikidot.com/avatar.php?userid=59229&amp;amp;amp;size=small&amp;amp;amp;timestamp=1780994287&quot; alt=&quot;elishapeterson&quot; style=&quot;background-image:url(http://www.wikidot.com/userkarma.php?u=59229)&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;http://www.wikidot.com/user:info/elishapeterson&quot;  &gt;elishapeterson&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
				<pubDate>Thu, 03 Jul 2008 20:40:46 +0000</pubDate>
												<content:encoded>
					<![CDATA[
						 <p>Elisha Peterson<br /> United States Military Academy</p> <div class="content-separator" style="display: none:"></div> <p>This is a tutorial describing how to use Geogebra to implement a cobwebbing demonstration. The techniques demonstrated can also be applied to many situations involving numerical and recursive techniques.</p> <div class="content-separator" style="display: none:"></div> <table style="margin:0; padding:0"> <tr> <td style="margin:0; padding:0"> <div id="toc"> <div id="toc-action-bar"><a href="javascript:;" >Fold</a><a style="display: none" href="javascript:;" >Unfold</a></div> <div class="title">Table of Contents</div> <div id="toc-list"> <div style="margin-left: 1em;"><a href="#toc0">Introduction</a></div> <div style="margin-left: 2em;"><a href="#toc1">How Cobwebbing Works</a></div> <div style="margin-left: 1em;"><a href="#toc2">Designing the First Iteration</a></div> <div style="margin-left: 2em;"><a href="#toc3">Method 1: Using Perpendicular Lines and Points of Intersection</a></div> <div style="margin-left: 2em;"><a href="#toc4">Method 2: Using Manual Input</a></div> <div style="margin-left: 1em;"><a href="#toc5">Subsequent Iterations</a></div> <div style="margin-left: 2em;"><a href="#toc6">Method 1: Tools</a></div> <div style="margin-left: 2em;"><a href="#toc7">Method 2: Sequence</a></div> <div style="margin-left: 1em;"><a href="#toc8">Exporting the Applet to the Web</a></div> <div style="margin-left: 2em;"><a href="#toc9">Exporting to the Web</a></div> <div style="margin-left: 2em;"><a href="#toc10">Adjusting the HTML and Implementing Javascript</a></div> <div style="margin-left: 1em;"><a href="#toc11">References</a></div> </div> </div> </td> </tr> </table> <div class="content-separator" style="display: none:"></div> <p><em><strong>NOTE:</strong> Readers unfamiliar with Geogebra may wish to refer to [<a href="javascript:;" class="bibcite" id="bibcite-490691-2-11388a" >2</a>] for a basic introduction. You can leave comments or questions at the bottom of the page.</em></p> <h1><span>Introduction</span></h1> <p><strong>Cobwebbing</strong> is a basic technique useful for visualizing solutions to simple recurrence equations. Given an initial value <span class="math-inline">$x_0$</span>, the recurrence <span class="math-inline">$x_n=f(x_{n-1})$</span> determines a sequence of points</p> <span class="equation-number">(1)</span> <div class="math-equation" id="equation-310085-1">\begin{align} x_0, x_1=f(x_0), x_2=f(x_1)=f(f(x_0)), \ldots, x_n=f(x_{n-1})=f(f(\cdots f(x_0)\cdots)), \ldots. \end{align}</div> <p>Typically, one is interested in the long-term behavior of this sequence. Does it converge? Does it approach a limit cycle? What else can you say about the sequence? Cobwebbing is a successful technique in large part because it provides a visual approach to determining the long-term behavior of the system. In particular, it can be used to show directly how the <em>slope</em> of the function <span class="math-inline">$f$</span> determines much of that behavior.</p> <p><strong>Geogebra</strong> is a free open-source mathematics tool designed for the interactive exploration of mathematics[<a href="javascript:;" class="bibcite" id="bibcite-490691-1-3334a" >1</a>]. Partly a dynamic <strong>geo</strong>metry platform, partly a computer al<strong>gebra</strong> system, it enables the creation of workbooks that beg for user interaction. This tutorial describes how to create a cobwebbing workbook that allows the user to change the initial point <span class="math-inline">$x_0$</span>, the recurrence function <span class="math-inline">$f$</span>, and the number of iterations displayed on the cobwebbing plot. It covers some advanced techniques such as using the Iteration command, designing customized functions with Geogebra's <em>Tool</em> feature, and creating webpages that support Javascript interactions.</p> <h2><span>How Cobwebbing Works</span></h2> <p>How does cobwebbing work? First, plot the function <span class="math-inline">$y=f(x)$</span> along with the diagonal line <span class="math-inline">$y=x$</span>. The resulting figure makes it immediately obvious whether any fixed points exist; <em>the number of intersections is precisely the number fixed points</em>. Second, mark the point <span class="math-inline">$(x_0,0)$</span> and draw a <em>vertical</em> line from the point to the function (whether above or below the point). Third, draw a <em>horizontal</em> line from that point to the diagonal line. This results in a line segment from <span class="math-inline">$(x_0,0)$</span> to <span class="math-inline">$(x_0,f(x_0))$</span>, and another from <span class="math-inline">$(x_0,f(x_0))$</span> to <span class="math-inline">$(f(x_0),f(x_0))$</span>. Fourth, repeat this process starting with the point on the diagonal. Continuing in this manner, the <span class="math-inline">$x$</span>-values of the vertical segments correspond to the values <span class="math-inline">$\{x_i\}$</span> in the sequence. Figure 1 shows cobwebbing in action.</p> <div style="margin:auto; width:400px; font-size:80%"> <div class="image-container aligncenter"><a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/cobwebbing.svg"><img src="http://elishapeterson.wdfiles.com/local--resized-images/technotes:geogebra-cobwebs/cobwebbing.svg/medium.jpg" alt="cobwebbing.svg" class="image" /></a></div> <p><strong>Figure 1.</strong> Cobwebbing demonstrates that iterating the sequence <span class="math-inline">$x_n=x_{n-1}(3-x_{n-1})$</span> with initial condition <span class="math-inline">$x_0=0.25$</span> slowly approaches the fixed point <span class="math-inline">$x^*=2$</span>.</p> </div> <h1><span>Designing the First Iteration</span></h1> <div style="float:right; width:500px; margin-left:20px; font-size:80%"> <div style="text-align: center;"><a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/cobweb1.jpg"><img src="http://elishapeterson.wdfiles.com/local--resized-images/technotes:geogebra-cobwebs/cobweb1.jpg/medium.jpg" alt="cobweb1.jpg" class="image" /></a><br /> <strong>Figure 2.</strong> The function, an initial point, and the <span class="math-inline">$y=x$</span> line.</div> </div> <br /> We'll start by inputting a function and the initial point <span class="math-inline">$(x_0,0)$</span>. Type <div class="code"> <pre><code>f(x)=3x-x^2</code></pre></div> <br /> into the input line. Next, type in <tt>y=x</tt> to draw the diagonal line. Finally, select the <img src="http://www.geogebra.org/help/docuen/topics/doc-Dateien/image032.gif" alt="image032.gif" class="image" /> <strong>Point</strong> tool and click somewhere on the x-axis (not too close to any intersections with other objects). This ensures the point is constrained to move on the axis. Check the constraint by hitting <tt>Esc</tt> and moving the point around. <p>At this point, you can change the appearances or names of any of the objects, by right-clicking on the object and selecting &quot;Properties&quot;. You should end up with something similar to Figure 2.</p> <div style="clear:right; height: 0px; font-size: 1px"></div> <p>We can now implement the <em>cobwebbing</em> technique. There are a few ways to generate additional points that move when the initial point is shifted. We demonstrate two different methods. In what follows, we assume the initial point is named &quot;<tt>A</tt>&quot;, the function is named &quot;<tt>f</tt>&quot;, and the line <span class="math-inline">$y=x$</span> is named &quot;<tt>a</tt>&quot;.</p> <h2><span>Method 1: Using Perpendicular Lines and Points of Intersection</span></h2> <div style="float:right; width:500px; margin-left:20px; font-size:80%"> <div style="text-align: center;"><a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/cobweb3.jpg"><img src="http://elishapeterson.wdfiles.com/local--resized-images/technotes:geogebra-cobwebs/cobweb3.jpg/medium.jpg" alt="cobweb3.jpg" class="image" /></a><br /> <strong>Figure 3.</strong> Creation of horizontal and vertical lines for the &quot;up and over&quot; cobweb construction.</div> </div> <br /> The first method uses standard toolbar features. <ol> <li>Select the <strong>Perpendicular line</strong> <img src="http://www.geogebra.org/help/docuen/topics/doc-Dateien/image044.gif" alt="image044.gif" class="image" /> tool, click on the point <tt>A</tt>, and then click on the x-axis. This creates a vertical line.</li> <li>With the same tool selected, move your mouse to the intersection of the vertical line and the function <span class="math-inline">$f(x)$</span>. Once both pieces are highlighted (indicated by an increase in line thickness), click to select the point of intersection. Then, click on the y-axis. This creates a horizontal line through the point <span class="math-inline">$(x_0,f(x_0))$</span>.</li> <li>Create a point at the intersection of the horizontal line and the diagonal line <span class="math-inline">$y=x$</span> by selecting the <strong>Point</strong> tool and clicking the point of intersection.</li> </ol> <p>Check that the dynamic structure is working properly by adjusting the point <tt>A</tt>. The additional lines should also move. The points that are <em>free</em> appear colored, while points that are not free appear black. The workbook should look like Figure 3.</p> <div style="clear:right; height: 0px; font-size: 1px"></div> You could leave the lines as is, but it's probably better to visualize segments than lines. My trick for fixing this problem follows. First, hide the lines by right-clicking on each and deselecting the &quot;Show object&quot; option. Second, use the <strong>Segment</strong> <img src="http://www.geogebra.org/help/docuen/topics/doc-Dateien/image037.gif" alt="image037.gif" class="image" /> tool to add segments between each pair of points. Alternately, one could use the <strong>Vector</strong> <img src="http://www.geogebra.org/help/docuen/topics/doc-Dateien/image035.gif" alt="image035.gif" class="image" /> tool to display segments with arrows. <p>I finish by cleaning up the workbook. Right-click on the objects whose labels are unnecessary and deselect the &quot;Show label&quot; option. This is also a good time to change the visual appearance of the figure. If you want to go with a minimalist approach, you may even wish to hide the displayed points and just display the lines. One useful trick is the <strong>Copy visual style</strong> tool, which allows you to quickly duplicate a visual style that you've created. See Figure 4 for my result.</p> <h2><span>Method 2: Using Manual Input</span></h2> <p>The second method uses direct input, and minimizes the creation of unnecessary features like the horizontal and vertical lines in the above. Here is the process:</p> <ol> <li>Type <tt>Segment[A,(x(A),f(x(A)))]</tt> into the input bar. This creates the desired vertical segment. Note that &quot;<tt>x(A)</tt>&quot; indicates the value of the x-coordinate of the point <tt>A</tt>.</li> <li>Create the horizontal segment by typing in <tt>Segment[(x(A),f(x(A))),(f(x(A)),f(x(A)))]</tt>.</li> <li>Turn off the labels on the segments, and change the appearance as desired.</li> </ol> <p>Visually, this provides exactly the same output as the first result, as seen in Figure 4.</p> <div style="float:left; width:50%; font-size:80%"> <div style="text-align: center;"><a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/cobweb4.jpg"><img src="http://elishapeterson.wdfiles.com/local--resized-images/technotes:geogebra-cobwebs/cobweb4.jpg/small.jpg" alt="cobweb4.jpg" class="image" /></a><br /> <strong>Figure 4.</strong> Clean version of the perpendicular-line construction.</div> </div> <div style="float:right; width:50%; font-size:80%"> <div style="text-align: center;"><a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/cobweb5.jpg"><img src="http://elishapeterson.wdfiles.com/local--resized-images/technotes:geogebra-cobwebs/cobweb5.jpg/small.jpg" alt="cobweb5.jpg" class="image" /></a><br /> <strong>Figure 5.</strong> Cobweb construction using direct algebraic input.</div> </div> <div style="clear:both; height: 0px; font-size: 1px"></div> <h1><span>Subsequent Iterations</span></h1> <p>You could repeat either process to generate several steps of the cobweb diagram, but it's easier to automate the process. We describe two methods for iterating the process, which illustrate some of the most useful features of Geogebra.</p> <h2><span>Method 1: Tools</span></h2> <p>The first technique is to create a <strong>Tool</strong>. A tool is a customized function (or macro) in which several output objects are dependent on several input objects. Most of the functions we have used thus far are tools. Geogebra permits users to design their own tools as well.</p> <p>The tool created here will have as inputs the function and initial point, and as outputs the vertical and horizontal cobweb lines. You must have completed one of the methods above before beginning this section. See Figure 6 for a visual of this process.</p> <ol> <li>Select &quot;Tools&#187;Create New Tool&#8230;&quot; from the menu. You select the outputs first. Select the vertical and horizontal segments corresponding to the first iteration.</li> <li>For the inputs, select the function and the point <tt>A</tt>. The order in which these are listed corresponds to the order in which they must be selected to use the tool later, so be sure they are ordered properly. I prefer to select the point first, so I moved the point to the top of the list.</li> <li>Give the function a name (like &quot;CobwebIteration&quot;), and for help type in &quot;<tt>Point, Function</tt>&quot;. This provides a useful reminder of the order of inputs when the tool is selected.</li> </ol> <div style="width:750px; margin:auto; font-size:80%"><a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/tool1.jpg"><img src="http://elishapeterson.wdfiles.com/local--resized-images/technotes:geogebra-cobwebs/tool1.jpg/small.jpg" alt="tool1.jpg" class="image" /></a> <a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/tool2.jpg"><img src="http://elishapeterson.wdfiles.com/local--resized-images/technotes:geogebra-cobwebs/tool2.jpg/small.jpg" alt="tool2.jpg" class="image" /></a> <a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/tool3.jpg"><img src="http://elishapeterson.wdfiles.com/local--resized-images/technotes:geogebra-cobwebs/tool3.jpg/small.jpg" alt="tool3.jpg" class="image" /></a><br /> <div style="text-align: center;"> <p><strong>Figure 6.</strong> Creating the <strong>CobwebIteration</strong> tool.</p> </div> </div> <div style="clear:both; height: 0px; font-size: 1px"></div> <p>After clicking &quot;Finish&quot;, the tool shows up in the normal toolbar. You can use the tool to perform further cobweb iterations by selecting the point of intersection of the last segment with the <span class="math-inline">$y=x$</span> line, and then the function. Figure 7 demonstrates the result after several iterations. Note the new button on the right side of the toolbar. You can also save the tool just created, for use in other workbooks, by selecting &quot;Tools&#187;Manage Tools&#8230;&quot; from the menu.</p> <div style="margin:auto; width:500px; font-size:80%"><a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/cobweb6.jpg"><img src="http://elishapeterson.wdfiles.com/local--resized-images/technotes:geogebra-cobwebs/cobweb6.jpg/medium.jpg" alt="cobweb6.jpg" class="image" /></a><br /> <div style="text-align: center;"> <p><strong>Figure 7.</strong> Iterated cobweb construction with a user-created tool.</p> </div> </div> <p>The workbook is now fully functional as a demonstration of the cobwebbing technique. The author's versions are attached to this page<sup class="footnoteref"><a id="footnoteref-998597-1" href="javascript:;" class="footnoteref" >1</a></sup>. In the next section, we'll make the demo a little more powerful by allowing the user to change the number of iterations dynamically.</p> <h2><span>Method 2: Sequence</span></h2> <p>Geogebra also has the functionality to construct a sequence of objects which depend on some input parameter. This requires knowing a little more of the command line structure, but offers the added functionality of being able to choose the desired number of iterations.</p> <p>There are two types of iteration processes available: <tt>Sequence</tt>, which produces a list of objects indexed by a particular value, and <tt>Iteration</tt>, which produces a list of numbers formed by iterating a single function.</p> Be sure the function <tt>f</tt>, the point <tt>A</tt>, and the line <span class="math-inline">$y=x$</span> have all been defined. We first create a slider that can be used to dynamically change the number of iterations displayed on the window. Begin by selecting the <strong>Slider</strong> <img src="http://www.geogebra.org/help/docuen/topics/doc-Dateien/image061.gif" alt="image061.gif" class="image" /> tool and clicking somewhere in the window. Rename the slider &quot;<tt>numIterations</tt>&quot;. Then set the minimum value to <tt>1</tt>, the maximum value to <tt>50</tt>, and the increment to <tt>1</tt>. Next, type <div class="code"> <pre><code>values = IterationList[f,x(A),numIterations]</code></pre></div> <br /> into the input line. This should create a list of several values with the name &quot;<tt>values</tt>&quot;. To create the horizontal and vertical segments, use the <tt>Sequence</tt> command. Type the following three lines into the input: <div class="code"> <pre><code>Segment[A,(x(A),f(x(A)))] cobwebHorizontal = Sequence[ Segment[(Element[values, i], Element[values, i+1]), (Element[values, i+1], Element[values, i+1])], i, 1, numIterations-1] cobwebVertical = Sequence[ Segment[(Element[values, i], Element[values, i]), (Element[values, i], Element[values, i+1])], i, 2, numIterations]</code></pre></div> <br /> Now you should be able to adjust the number of cobweb iterations by moving the slider. Figure 8 shows the resulting figure. The Geogebra workbook generated by the author using the <tt>Sequence</tt> and <tt>Iteration</tt> commands is attached below (<a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/CobwebIteration-Sequence.ggb">CobwebIteration-Sequence.ggb</a>). <div style="margin:auto; width:500px; font-size:80%"><a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/cobweb7.jpg"><img src="http://elishapeterson.wdfiles.com/local--resized-images/technotes:geogebra-cobwebs/cobweb7.jpg/medium.jpg" alt="cobweb7.jpg" class="image" /></a><br /> <div style="text-align: center;"> <p><strong>Figure 8.</strong> Cobweb construction with slider, allowing the user to change the number of iterations.</p> </div> </div> <p>At this point, the user could export the displayed figure as an image file, creating beautiful images of cobwebbing. Figure 1 was exported from Geogebra as an <em>SVG</em>, or &quot;Scalable Vector Graphics&quot; file. This type of file can be displayed at any resolution without loss of image quality.</p> <h1><span>Exporting the Applet to the Web</span></h1> <p>Geogebra allows you to easily post workbooks that you create to a website. This section describes how to do this, and how to use Javascript to provide the user with a textbox to input the function directly.</p> <h2><span>Exporting to the Web</span></h2> <div style="float:right; width:500px; margin-left:20px; font-size:80%"> <div style="text-align: center;"><a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/export1.jpg"><img src="http://elishapeterson.wdfiles.com/local--resized-images/technotes:geogebra-cobwebs/export1.jpg/medium.jpg" alt="export1.jpg" class="image" /></a><br /> <strong>Figure 9.</strong> Exporting a Worksheet to HTML.</div> </div> <br /> Exporting to the web using Geogebra is a simple process. Go to &quot;<tt>File&#187;Export&#187;Dynamic Worksheet as Webpage (html)</tt>&quot; or press <tt>Ctrl+Shift+W</tt>. This loads a template, shown in Figure 9, which allows you to fill in text before and after the worksheet. Under the &quot;<tt>Advanced</tt>&quot; tab, you can change the displayed size of the worksheet, control whether or not the menu and toolbars are displayed, and so on. <p><em><strong>Warning</strong>: It is best to resize the window to your desired size</em> before <em>exporting. Otherwise, the change in size may not display all the objects in your worksheet properly, or may not display some of them at all.</em></p> <p>Once you click &quot;<tt>Export</tt>&quot;, an <tt>html</tt> file will be created along with a new worksheet file <tt>.ggb</tt> and two <tt>.jar</tt> files that are required for Java to run the program: <tt>geogebra.jar</tt> and <tt>geogebra_properties.jar</tt>. In order for the applet to work, you will need to upload all four files to a web folder, and link to the <tt>html</tt> file. Note that in some cases the <tt>.ggb</tt> files will not load properly because the file type is not &quot;enabled&quot; on the server. If this comes up, you can ask IT to permit you to post <tt>.ggb</tt> files. The author's version is posted <a href="http://www.dean.usma.edu/math/people/Peterson/geogebra/CobwebIteration_Sequence.html">here</a>.</p> <div style="clear:right; height: 0px; font-size: 1px"></div> <h2><span>Adjusting the HTML and Implementing Javascript</span></h2> <p>After the <tt>html</tt> file has been created, it can be modified directly or with an <tt>html</tt> editor. One can add additional titles, links, and text, or update the appearance.</p> <p>One particularly nice feature of <em>Geogebra</em> is that it can interface with <em>Javascript</em>. We'll create a simple form that the user can use to input a new function, similar to that shown in Figure 10. The Javascript code required is minimal. For further details on the Geogebra/Javascript interface, see [<a href="javascript:;" class="bibcite" id="bibcite-490691-5-48373a" >5</a>, <a href="javascript:;" class="bibcite" id="bibcite-490691-6-64563a" >6</a>].</p> <div style="margin:auto; width:600px; font-size:80%"> <div class="image-container aligncenter"><img src="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/formInput.jpg" alt="formInput.jpg" class="image" /></div> <div style="text-align: center;"> <p><strong>Figure 10.</strong> Javascript form input used to interface with Geogebra cobwebbing worksheet.</p> </div> </div> <p>To implement the form, I inserted the following <tt>html</tt> code block immediately before the applet:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-brackets">&lt;</span><span class="hl-reserved">form</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">p</span><span class="hl-code"> </span><span class="hl-var">align</span><span class="hl-code">=</span><span class="hl-var">center</span><span class="hl-brackets">&gt;</span><span class="hl-code"> Change function: </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">b</span><span class="hl-brackets">&gt;</span><span class="hl-code">f(x)=</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">b</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">input</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">text</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">T1</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">size</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">20</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">value</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">3x-x^2</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">input</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">button</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">value</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Submit</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">B1</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">onclick</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">document.ggbApplet.evalCommand('f(x)='+T1.value);</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">input</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">button</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">value</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Reset</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">B2</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">onclick</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">T1.value='3x-x^2';document.ggbApplet.reset();</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">p</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">form</span><span class="hl-brackets">&gt;</span><span class="hl-code"> </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">applet</span><span class="hl-code"> </span><span class="hl-var">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">ggbApplet</span><span class="hl-quotes">&quot;</span><span class="hl-code"> ....</span></pre></div> </div> <br /> The two commands used to interface with Geogebra are <tt>evalComand()</tt>, which takes as argument a <tt>String</tt> and is equivalent to typing a command at the Input line, and <tt>reset()</tt>, which restores the applet to its initial state. The author's applet with user-input form is posted <a href="http://www.dean.usma.edu/math/people/Peterson/geogebra/CobwebIteration_Sequence_2.html">here</a>. <h1><span>References</span></h1> <p>Much more is possible using Geogebra. An outstanding resource is the online wiki [<a href="javascript:;" class="bibcite" id="bibcite-490691-4-46760a" >4</a>]. My collection of worksheets and webpages created with Geogebra is available at [<a href="javascript:;" class="bibcite" id="bibcite-490691-7-86402a" >7</a>].</p> <div class="bibitems"> <div class="bibitem" id="bibitem-490691-1">1. <strong>Geogebra</strong> (application), available by download or Java WebStart at <a href="http://www.geogebra.org">http://www.geogebra.org</a>.</div> <div class="bibitem" id="bibitem-490691-2">2. <strong>Geogebra Documentation</strong>, available at <a href="http://www.geogebra.org/help/docuen/index.html">http://www.geogebra.org/help/docuen/index.html</a></div> <div class="bibitem" id="bibitem-490691-3">3. <strong>Creating Mathlets with Open-Source Tools</strong>, Markus Hohenwarter and Judith Preiner, available at <a href="http://www.maa.org/joma/Volume7/Hohenwarter2/index.html">http://www.maa.org/joma/Volume7/Hohenwarter2/index.html</a></div> <div class="bibitem" id="bibitem-490691-4">4. <strong>Geogebra Wiki</strong>, <a href="http://www.geogebra.org/en/wiki/index.php/Main_Page">http://www.geogebra.org/en/wiki/index.php/Main_Page</a></div> <div class="bibitem" id="bibitem-490691-5">5. <strong>Geogebra Applets and Javascript</strong>, available at <a href="http://www.geogebra.org/source/program/applet/geogebra_applet_javascript.html">http://www.geogebra.org/source/program/applet/geogebra_applet_javascript.html</a></div> <div class="bibitem" id="bibitem-490691-6">6. <em>Creating Interactive Exercises using JavaScript</em>, Markus Hohenwarter and Judith Preiner, available at <a href="http://www.maa.org/joma/Volume7/Hohenwarter2/javascript.html">http://www.maa.org/joma/Volume7/Hohenwarter2/javascript.html</a></div> <div class="bibitem" id="bibitem-490691-7">7. Collection of Geogebra webpages and worksheets, available at <a href="http://www.dean.usma.edu/math/people/Peterson/geogebra/mathlets.html">http://www.dean.usma.edu/math/people/Peterson/geogebra/mathlets.html</a></div> </div> <p>by <span class="printuser avatarhover"><a href="http://www.wikidot.com/user:info/elishapeterson" ><img class="small" src="http://www.wikidot.com/avatar.php?userid=59229&amp;amp;size=small&amp;amp;timestamp=1780994287" alt="elishapeterson" style="background-image:url(http://www.wikidot.com/userkarma.php?u=59229)" /></a><a href="http://www.wikidot.com/user:info/elishapeterson" >elishapeterson</a></span></p> <div class="footnotes-footer"> <div class="title">Footnotes</div> <div class="footnote-footer" id="footnote-998597-1"><a href="javascript:;" >1</a>. The Geogebra workbook generated by the author using this method is attached to this page (<a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/CobwebIteration-Tool.ggb">CobwebIteration-Tool.ggb</a>). The custom tool is also attached (<a href="http://elishapeterson.wdfiles.com/local--files/technotes:geogebra-cobwebs/CobwebIterationTool.ggt">CobwebIterationTool.ggt</a>).</div> </div> 
				 	]]>
				</content:encoded>							</item>
				</channel>
</rss>