001    /* Element.java --
002       Copyright (C) 2005 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.swing.text.html.parser;
040    
041    import gnu.javax.swing.text.html.parser.support.gnuStringIntMapper;
042    
043    import java.io.Serializable;
044    
045    import java.util.BitSet;
046    
047    /**
048     * <p>
049     * Stores the element information, obtained by parsing SGML DTD
050     * tag <code>&lt;!ELEMENT .. &gt;</code>. This class has no public
051     * constructor and can only be instantiated using the
052     * {@link javax.swing.text.html.parser.DTD } methods</p>
053     *
054     * <p>SGML defines elements that represent structures or
055     * behavior. An element typically consists of a start tag, content, and an
056     * end tag. Hence the elements are not tags. The HTML 4.0 definition specifies
057     * that some elements are not required to have the end tags. Also, some
058     * HTML elements (like <code>&lt;hr&gt;</code>) have no content. Element names
059     * are case sensitive.</p>
060     * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
061     */
062    public final class Element
063      implements DTDConstants, Serializable
064    {
065      /**
066       * Package level mapper between type names and they string values.
067       */
068      static final gnuStringIntMapper mapper =
069        new gnuStringIntMapper()
070        {
071          protected void create()
072          {
073            add("CDATA", DTDConstants.CDATA);
074            add("RCDATA", DTDConstants.RCDATA);
075            add("EMPTY", DTDConstants.EMPTY);
076            add("ANY", DTDConstants.ANY);
077          }
078        };
079    
080      /** Use serialVersionUID for interoperability. */
081      private static final long serialVersionUID = -6717939384601675586L;
082    
083      /**
084       * The element attributes.
085       */
086      public AttributeList atts;
087    
088      /**
089       * Contains refernces to elements that must NOT occur inside this element,
090       * at any level of hierarchy.
091       */
092      public BitSet exclusions;
093    
094      /**
095       * Contains refernces to elements that must CAN occur inside this element,
096       * at any level of hierarchy.
097       */
098      public BitSet inclusions;
099    
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    }