Merge from mainline
[official-gcc.git] / libjava / classpath / gnu / java / beans / encoder / Root.java
blobf4eade1939d11fd6d39e0394213579327096cbce
1 /* Root.java -- The root of an object tree.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.beans.encoder;
41 import java.beans.XMLEncoder;
42 import java.util.Iterator;
43 import java.util.Stack;
45 import gnu.java.beans.encoder.elements.Element;
47 /** <p><code>Root</code> provides a simple interface to a tree of
48 * objects.</p>
50 * <p>Using an instance of this class a logical representation of
51 * the real object tree that is serialized can be built. When the
52 * actual data should be written as XML <code>Root</code> and
53 * {@link gnu.java.beans.encoder.elements.Element} class can provide
54 * context information which is used to write the best fitting
55 * XML representation.</p>
57 * @author Robert Schuster (robertschuster@fsfe.org)
59 public class Root
61 private Stack parents = new Stack();
63 private Element rootElement, current;
65 private boolean started;
67 public Root()
69 rootElement = current = new RootElement();
72 /** <p>Adds another child element to the tree.</p>
74 * <p>The new element automatically becomes the current
75 * element.</p>
77 * @param elem The new child element.
79 public void addChild(Element elem)
81 current.addChild(elem);
83 parents.push(current);
84 current = elem;
87 /**
88 * <p>Marks that the end of the current element
89 * is reached and that no more childs are added to
90 * it.</p>
92 * <p>The behavior is to return to the nearest parent
93 * element.</p>
95 public void end()
97 current = (Element) parents.pop();
101 * <p>Goes back to the nearest parent element but
102 * deletes the just created child.</p>
104 * <p>This is used if something went wrong while
105 * processing the child element's {@link java.beans.Expression}
106 * or {@link java.beans.Statement}.</p>
109 public void deleteLast()
111 current = (Element) parents.pop();
113 current.removeLast();
117 * <p>Traverses the elements in the object tree
118 * and creates their XML representation in the output
119 * stream of the given {@link Writer}.</p>
121 * <p>Finally the <code>Writer</code> is flushed.</p>
123 * @param writer The Writer instance that generates the XML representation.
125 public void traverse(Writer writer)
127 if (!started)
129 writer.writePreamble();
130 rootElement.writeStart(writer);
132 started = true;
134 traverse(writer, rootElement.iterator());
136 rootElement.clear();
138 writer.flush();
141 /** Writes the closing element and closes the {@link Writer}
143 * @param writer The Writer instance that generates the XML representation.
145 public void close(Writer writer)
147 rootElement.writeEnd(writer);
148 writer.close();
151 /** Recursively traverses the object tree.
153 * @param writer The Writer instance that generates the XML representation.
154 * @param ite An Iterator returning Element instances.
156 private void traverse(Writer writer, Iterator ite)
158 while (ite.hasNext())
160 Element e = (Element) ite.next();
161 e.writeStart(writer);
163 traverse(writer, e.iterator());
165 e.writeEnd(writer);
167 e.clear();
171 /** <p>A special Element implementation that represents the
172 * encoder's context.</p>
174 * <p>This element is written only once per Writer.</p>
176 * <p>It is assumed that this element is never empty to simplify
177 * the implementation.</p>
179 * @author Robert Schuster (robertschuster@fsfe.org);
182 static class RootElement extends Element
184 public void writeStart(Writer writer)
186 writer.write("java", new String[] { "version", "class" },
187 new String[] { System.getProperty("java.version"),
188 XMLEncoder.class.getName() }, false);
191 public void writeEnd(Writer writer)
193 writer.writeEnd(false);