OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [beans/] [encoder/] [Root.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* Root.java -- The root of an object tree.
2
 Copyright (C) 2005 Free Software Foundation, Inc.
3
 
4
 This file is part of GNU Classpath.
5
 
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.
10
 
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.
15
 
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.
20
 
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.
25
 
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. */
37
 
38
 
39
package gnu.java.beans.encoder;
40
 
41
import java.beans.XMLEncoder;
42
import java.util.Iterator;
43
import java.util.Stack;
44
 
45
import gnu.java.beans.encoder.elements.Element;
46
 
47
/** <p><code>Root</code> provides a simple interface to a tree of
48
 * objects.</p>
49
 *
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>
56
 *
57
 * @author Robert Schuster (robertschuster@fsfe.org)
58
 */
59
public class Root
60
{
61
  private Stack parents = new Stack();
62
 
63
  private Element rootElement, current;
64
 
65
  private boolean started;
66
 
67
  public Root()
68
  {
69
    rootElement = current = new RootElement();
70
  }
71
 
72
  /** <p>Adds another child element to the tree.</p>
73
   *
74
   * <p>The new element automatically becomes the current
75
   * element.</p>
76
   *
77
   * @param elem The new child element.
78
   */
79
  public void addChild(Element elem)
80
  {
81
    current.addChild(elem);
82
 
83
    parents.push(current);
84
    current = elem;
85
  }
86
 
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>
91
   *
92
   * <p>The behavior is to return to the nearest parent
93
   * element.</p>
94
   */
95
  public void end()
96
  {
97
    current = (Element) parents.pop();
98
  }
99
 
100
  /**
101
   * <p>Goes back to the nearest parent element but
102
   * deletes the just created child.</p>
103
   *
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>
107
   *
108
   */
109
  public void deleteLast()
110
  {
111
    current = (Element) parents.pop();
112
 
113
    current.removeLast();
114
  }
115
 
116
  /**
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>
120
   *
121
   * <p>Finally the <code>Writer</code> is flushed.</p>
122
   *
123
   * @param writer The Writer instance that generates the XML representation.
124
   */
125
  public void traverse(Writer writer)
126
  {
127
    if (!started)
128
      {
129
        writer.writePreamble();
130
        rootElement.writeStart(writer);
131
      }
132
    started = true;
133
 
134
    traverse(writer, rootElement.iterator());
135
 
136
    rootElement.clear();
137
 
138
    writer.flush();
139
  }
140
 
141
  /** Writes the closing element and closes the {@link Writer}
142
   *
143
   * @param writer The Writer instance that generates the XML representation.
144
   */
145
  public void close(Writer writer)
146
  {
147
    rootElement.writeEnd(writer);
148
    writer.close();
149
  }
150
 
151
  /** Recursively traverses the object tree.
152
   *
153
   * @param writer The Writer instance that generates the XML representation.
154
   * @param ite An Iterator returning Element instances.
155
   */
156
  private void traverse(Writer writer, Iterator ite)
157
  {
158
    while (ite.hasNext())
159
      {
160
        Element e = (Element) ite.next();
161
        e.writeStart(writer);
162
 
163
        traverse(writer, e.iterator());
164
 
165
        e.writeEnd(writer);
166
 
167
        e.clear();
168
      }
169
  }
170
 
171
  /** <p>A special Element implementation that represents the
172
   * encoder's context.</p>
173
   *
174
   * <p>This element is written only once per Writer.</p>
175
   *
176
   * <p>It is assumed that this element is never empty to simplify
177
   * the implementation.</p>
178
   *
179
   * @author Robert Schuster (robertschuster@fsfe.org);
180
   *
181
   */
182
  static class RootElement extends Element
183
  {
184
    public void writeStart(Writer writer)
185
    {
186
      writer.write("java", new String[] { "version", "class" },
187
                   new String[] { System.getProperty("java.version"),
188
                                 XMLEncoder.class.getName() }, false);
189
    }
190
 
191
    public void writeEnd(Writer writer)
192
    {
193
      writer.writeEnd(false);
194
    }
195
 
196
  }
197
 
198
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.