Source for gnu.java.beans.encoder.elements.Element

   1: /* Element.java -- Base class for object tree elements.
   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.elements;
  40: 
  41: import java.util.Iterator;
  42: import java.util.LinkedList;
  43: 
  44: import gnu.java.beans.encoder.ObjectId;
  45: import gnu.java.beans.encoder.Writer;
  46: 
  47: /** <code>Element</code> is the base class for the object tree elements.
  48:  *
  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>
  52:  *
  53:  * @author Robert Schuster (robertschuster@fsfe.org)
  54:  */
  55: public abstract class Element
  56: {
  57:   /**
  58:    * Stores the child elements.
  59:    */
  60:   private LinkedList children = new LinkedList();
  61: 
  62:   /**
  63:    * An optional ObjectId instance which is needed for certain subclasses
  64:    * only.
  65:    */
  66:   private ObjectId objectId;
  67: 
  68:   /** Sets an {@link gnu.java.beans.encoder.ObjectId} instance in this
  69:    * <code>Element</code>.
  70:    *
  71:    * <p>This can only be done once.</p>
  72:    *
  73:    * @param objectId An ObjectId instance.
  74:    */
  75:   public final void initId(ObjectId objectId)
  76:   {
  77:     assert (this.objectId == null);
  78:     assert (objectId != null);
  79: 
  80:     this.objectId = objectId;
  81:   }
  82: 
  83:   /** Adds a child element to this <code>Element</code>.
  84:    *
  85:    * @param elem The new child.
  86:    */
  87:   public final void addChild(Element elem)
  88:   {
  89:     children.add(elem);
  90:   }
  91: 
  92:   /** Removes the child element added last.
  93:    */
  94:   public final void removeLast()
  95:   {
  96:     children.removeLast();
  97:   }
  98: 
  99:   /** Provides access to the child elements via an iterator.
 100:    *
 101:    * @return An iterator for the child elements.
 102:    */
 103:   public final Iterator iterator(){
 104:     return children.iterator();
 105:   }
 106: 
 107:   /** Clears all the stored child elements.
 108:    *
 109:    */
 110:   public final void clear()
 111:   {
 112:    children.clear();
 113:   }
 114: 
 115:   /** Returns whether this element contains child elements.
 116:    *
 117:    * <p>This method is useful to decide which formatting variant
 118:    * for the XML element can be chosen.</p>
 119:    *
 120:    * @return Whether the element has child elements.
 121:    */
 122:   public final boolean isEmpty()
 123:   {
 124:     return children.isEmpty();
 125:   }
 126: 
 127:   /** Retrieves the element's {@link gnu.java.beans.encoder.ObjectId} instance
 128:    * if it has one.
 129:    *
 130:    * @return The ObjectId instance or <code>null</code>.
 131:    */
 132:   public final ObjectId getId()
 133:   {
 134:     return objectId;
 135:   }
 136: 
 137:   /** Writes the opening XML tag.
 138:    *
 139:    * @param writer The writer to be used for XML writing.
 140:    */
 141:   public abstract void writeStart(Writer writer);
 142: 
 143:   /** Writes the closing XML tag.
 144:    *
 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.
 149:    *
 150:    * @param writer The writer to be used for XML writing.
 151:    */
 152:   public void writeEnd(Writer writer)
 153:   {
 154:     writer.writeEnd(children.isEmpty());
 155:   }
 156: 
 157: }