Imported GNU Classpath 0.20
[official-gcc.git] / libjava / classpath / gnu / java / beans / encoder / elements / Element.java
blob5681d2b76a0350611c48061d2fc0365d23b56f7b
1 /* Element.java -- Base class for object tree elements.
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.elements;
41 import java.util.Iterator;
42 import java.util.LinkedList;
44 import gnu.java.beans.encoder.ObjectId;
45 import gnu.java.beans.encoder.Writer;
47 /** <code>Element</code> is the base class for the object tree elements.
49 * <p>It provides the neccessary infrastructure every element subclass
50 * needs in order to interact with the {@link gnu.java.beans.encoder.Root}
51 * class.</p>
53 * @author Robert Schuster (robertschuster@fsfe.org)
55 public abstract class Element
57 /**
58 * Stores the child elements.
60 private LinkedList children = new LinkedList();
62 /**
63 * An optional ObjectId instance which is needed for certain subclasses
64 * only.
66 private ObjectId objectId;
68 /** Sets an {@link gnu.java.beans.encoder.ObjectId} instance in this
69 * <code>Element</code>.
71 * <p>This can only be done once.</p>
73 * @param objectId An ObjectId instance.
75 public final void initId(ObjectId objectId)
77 assert (this.objectId == null);
78 assert (objectId != null);
80 this.objectId = objectId;
83 /** Adds a child element to this <code>Element</code>.
85 * @param elem The new child.
87 public final void addChild(Element elem)
89 children.add(elem);
92 /** Removes the child element added last.
94 public final void removeLast()
96 children.removeLast();
99 /** Provides access to the child elements via an iterator.
101 * @return An iterator for the child elements.
103 public final Iterator iterator(){
104 return children.iterator();
107 /** Clears all the stored child elements.
110 public final void clear()
112 children.clear();
115 /** Returns whether this element contains child elements.
117 * <p>This method is useful to decide which formatting variant
118 * for the XML element can be chosen.</p>
120 * @return Whether the element has child elements.
122 public final boolean isEmpty()
124 return children.isEmpty();
127 /** Retrieves the element's {@link gnu.java.beans.encoder.ObjectId} instance
128 * if it has one.
130 * @return The ObjectId instance or <code>null</code>.
132 public final ObjectId getId()
134 return objectId;
137 /** Writes the opening XML tag.
139 * @param writer The writer to be used for XML writing.
141 public abstract void writeStart(Writer writer);
143 /** Writes the closing XML tag.
145 * <p>By default this does <code>writer.writeEnd(children.isEmpty())</code>.
146 * Override if neccessary, for example when using the
147 * {@link gnu.java.beans.encoder.Writer#writeNoChildren}</code> method
148 * variants.
150 * @param writer The writer to be used for XML writing.
152 public void writeEnd(Writer writer)
154 writer.writeEnd(children.isEmpty());