Source for javax.swing.text.html.parser.Element

   1: /* Element.java --
   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 javax.swing.text.html.parser;
  40: 
  41: import gnu.javax.swing.text.html.parser.support.gnuStringIntMapper;
  42: 
  43: import java.io.Serializable;
  44: 
  45: import java.util.BitSet;
  46: 
  47: /**
  48:  * <p>
  49:  * Stores the element information, obtained by parsing SGML DTD
  50:  * tag <code>&lt;!ELEMENT .. &gt;</code>. This class has no public
  51:  * constructor and can only be instantiated using the
  52:  * {@link javax.swing.text.html.parser.DTD } methods</p>
  53:  *
  54:  * <p>SGML defines elements that represent structures or
  55:  * behavior. An element typically consists of a start tag, content, and an
  56:  * end tag. Hence the elements are not tags. The HTML 4.0 definition specifies
  57:  * that some elements are not required to have the end tags. Also, some
  58:  * HTML elements (like <code>&lt;hr&gt;</code>) have no content. Element names
  59:  * are case sensitive.</p>
  60:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  61:  */
  62: public final class Element
  63:   implements DTDConstants, Serializable
  64: {
  65:   /**
  66:    * Package level mapper between type names and they string values.
  67:    */
  68:   static final gnuStringIntMapper mapper =
  69:     new gnuStringIntMapper()
  70:     {
  71:       protected void create()
  72:       {
  73:         add("CDATA", DTDConstants.CDATA);
  74:         add("RCDATA", DTDConstants.RCDATA);
  75:         add("EMPTY", DTDConstants.EMPTY);
  76:         add("ANY", DTDConstants.ANY);
  77:       }
  78:     };
  79: 
  80:   /** Use serialVersionUID for interoperability. */
  81:   private static final long serialVersionUID = -6717939384601675586L;
  82: 
  83:   /**
  84:    * The element attributes.
  85:    */
  86:   public AttributeList atts;
  87: 
  88:   /**
  89:    * Contains refernces to elements that must NOT occur inside this element,
  90:    * at any level of hierarchy.
  91:    */
  92:   public BitSet exclusions;
  93: 
  94:   /**
  95:    * Contains refernces to elements that must CAN occur inside this element,
  96:    * at any level of hierarchy.
  97:    */
  98:   public BitSet inclusions;
  99: 
 100:   /**
 101:    * The content model, defining elements, entities and DTD text
 102:    * that may/may not occur inside this element.
 103:    */
 104:   public ContentModel content;
 105: 
 106:   /**
 107:    * A field to store additional user data for this Element.
 108:    */
 109:   public Object data;
 110: 
 111:   /**
 112:    * The element name.
 113:    */
 114:   public String name;
 115: 
 116:   /**
 117:    * True is this element need not to have the closing tag, false
 118:    * otherwise. The HTML 4.0 definition specifies
 119:    * that some elements (like <code>&lt;hr&gt;</code>are
 120:    * not required to have the end tags.
 121:    */
 122:   public boolean oEnd;
 123: 
 124:   /**
 125:    * True is this element need not to have the starting tag, false
 126:    * otherwise. The HTML 4.0 definition specifies
 127:    * that some elements (like <code>&lt;head&gt;</code> or
 128:    * <code>&lt;body&gt;</code>) are
 129:    * not required to have the start tags.
 130: 
 131:    */
 132:   public boolean oStart;
 133: 
 134:   /**
 135:    * This field contains the unique integer identifier of this Element,
 136:    * used to refer the element (more exactly, the element flag)
 137:    * in <code>inclusions</code> and <code>exclusions</code> bit set.
 138:    */
 139:   public int index;
 140: 
 141:   /**
 142:    * The element type, containing value, defined in DTDConstants.
 143:    * In this implementation, the element type can be
 144:    * CDATA, RCDATA, EMPTY or ANY.
 145:    */
 146:   public int type;
 147: 
 148:   /**
 149:    * The default constructor must have package level access in this
 150:    * class. Use DTD.defineElement(..) to create an element when required.
 151:    */
 152:   Element()
 153:   {
 154:     // Nothing to do here.
 155:   }
 156: 
 157:   /**
 158:    * Converts the string representation of the element type
 159:    * into its unique integer identifier, defined in DTDConstants.
 160:    * @param a_type A name of the type
 161:    * @return DTDConstants.CDATA, DTDConstants.RCDATA, DTDConstants.EMPTY,
 162:    * DTDConstants.ANY or null if the type name is not
 163:    * "CDATA", "RCDATA", "EMPTY" or "ANY". This function is case sensitive.
 164:    * @throws NullPointerException if <code>a_type</code> is null.
 165:    */
 166:   public static int name2type(String a_type)
 167:   {
 168:     return mapper.get(a_type);
 169:   }
 170: 
 171:   /**
 172:    * Get the element attribute by name.
 173:    * @param attribute the attribute name, case insensitive.
 174:    * @return the correspoding attribute of this element. The class,
 175:    * for storing as attribute list, as a single attribute, is used to
 176:    * store a single attribute in this case.
 177:    * @throws NullPointerException if the attribute name is null.
 178:    */
 179:   public AttributeList getAttribute(String attribute)
 180:   {
 181:     AttributeList a = atts;
 182: 
 183:     while (a != null && !attribute.equalsIgnoreCase(a.name))
 184:       a = a.next;
 185: 
 186:     return a;
 187:   }
 188: 
 189:   /**
 190:    * Get the element attribute by its value.
 191:    * @param a_value the attribute value, case insensitive.
 192:    * @return the correspoding attribute of this element. The class,
 193:    * for storing as attribute list, as a single attribute, is used to
 194:    * store a single attribute in this case. If there are several
 195:    * attributes with the same value, there is no garranty, which one
 196:    * is returned.
 197:    */
 198:   public AttributeList getAttributeByValue(String a_value)
 199:   {
 200:     AttributeList a = atts;
 201: 
 202:     if (a_value == null)
 203:       {
 204:         while (a != null)
 205:           {
 206:             if (a.value == null)
 207:               return a;
 208: 
 209:             a = a.next;
 210:           }
 211:       }
 212:     else
 213:       {
 214:         while (a != null)
 215:           {
 216:             if (a.value != null && a_value.equalsIgnoreCase(a.value))
 217:               return a;
 218: 
 219:             a = a.next;
 220:           }
 221:       }
 222: 
 223:     return null;
 224:   }
 225: 
 226:   /**
 227:    * Get all attributes of this document as an attribute list.
 228:    * @return The attribute list.
 229:    */
 230:   public AttributeList getAttributes()
 231:   {
 232:     return atts;
 233:   }
 234: 
 235:   /**
 236:    * Get the content model, defining elements, entities and DTD text
 237:    * that may/may not occur inside this element.
 238:    */
 239:   public ContentModel getContent()
 240:   {
 241:     return content;
 242:   }
 243: 
 244:   /**
 245:    * Returns true for the element with no content.
 246:    * Empty elements are defined with the SGML DTD keyword "EMPTY".
 247:    * @return true if content model field (content) method is equal to
 248:    * null or its method empty() returns true.
 249:    */
 250:   public boolean isEmpty()
 251:   {
 252:     return content == null || content.empty();
 253:   }
 254: 
 255:   /**
 256:    * Get the unique integer identifier of this Element,
 257:    * used to refer the element (more exactly, the element flag)
 258:    * in <code>inclusions</code> and <code>exclusions</code> bit set.
 259:    * WARNING: This value may not be the same between different
 260:    * implementations.
 261:    */
 262:   public int getIndex()
 263:   {
 264:     return index;
 265:   }
 266: 
 267:   /**
 268:    * Get the element name.
 269:    */
 270:   public String getName()
 271:   {
 272:     return name;
 273:   }
 274: 
 275:   /**
 276:    * Get the element type.
 277:    * @return one of the values, defined DTDConstants.
 278:    * In this implementation, the element type can be
 279:    * CDATA, RCDATA, EMPTY or ANY.
 280:    */
 281:   public int getType()
 282:   {
 283:     return type;
 284:   }
 285: 
 286:   /**
 287:    * True is this element need not to have the starting tag, false
 288:    * otherwise.s element need not to have the closing tag, false
 289:    * otherwise. The HTML 4.0 definition specifies
 290:    * that some elements (like <code>&lt;hr&gt;</code>are
 291:    * not required to have the end tags.
 292:    */
 293:   public boolean omitEnd()
 294:   {
 295:     return oEnd;
 296:   }
 297: 
 298:   /**
 299:    * True is this element need not to have the closing tag, false
 300:    * otherwise. The HTML 4.0 definition specifies
 301:    * that some elements (like <code>&lt;head&gt;</code> or
 302:    * <code>&lt;body&gt;</code>) are
 303:    * not required to have the start tags.
 304:    */
 305:   public boolean omitStart()
 306:   {
 307:     return oStart;
 308:   }
 309: 
 310:   /**
 311:    * Returns the name of this element.
 312:    */
 313:   public String toString()
 314:   {
 315:     return name;
 316:   }
 317: }