001/* AttributeList.java --
002   Copyright (C) 2005 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package javax.swing.text.html.parser;
040
041import gnu.javax.swing.text.html.parser.support.gnuStringIntMapper;
042
043import java.io.Serializable;
044
045import java.util.Enumeration;
046import java.util.Vector;
047
048/**
049 * <p>
050 * Stores the attribute information, obtained by parsing SGML (DTD) tag
051 * <code>&lt;!ATTLIST .. &gt;</code></p>
052 * <p>
053 * Elements can have a associated named properties (attributes) having the
054 * assigned values. The element start tag can have any number of attribute
055 * value pairs, separated by spaces. They can appear in any order.
056 * SGML requires you to delimit the attribute values using either double (")
057 * or single (') quotation marks.  In HTML, it is possible
058 * (but not recommended) to specify the value of an attribute without
059 * quotation marks. Such attribute value may only contain
060 * letters, digits, hyphens (-) and periods (.) .
061 * </p>
062 * <p>
063 * The <code>AttributeList</code> defines a single attribute that additionally
064 * has a pointer referencing the possible subsequent attribute.
065 * The whole structure is just a simple linked list, storing all attributes of
066 * some <code>Element</code>.
067 * Use the <code>getNext()</code> method repeatedly to see all attributes in
068 * the list.
069 * </p>
070 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
071 */
072public final class AttributeList
073  implements DTDConstants, Serializable
074{
075  /** Maps between type names and they string values. */
076  private static final gnuStringIntMapper mapper =
077    new gnuStringIntMapper()
078    {
079      protected void create()
080      {
081        add("CDATA", DTDConstants.CDATA);
082        add("ENTITY", DTDConstants.ENTITY);
083        add("ENTITIES", DTDConstants.ENTITIES);
084        add("ID", DTDConstants.ID);
085        add("IDREF", DTDConstants.IDREF);
086        add("IDREFS", DTDConstants.IDREFS);
087        add("NAME", DTDConstants.NAME);
088        add("NAMES", DTDConstants.NAMES);
089        add("NMTOKEN", DTDConstants.NMTOKEN);
090        add("NMTOKENS", DTDConstants.NMTOKENS);
091        add("NOTATION", DTDConstants.NOTATION);
092        add("NUMBER", DTDConstants.NUMBER);
093        add("NUMBERS", DTDConstants.NUMBERS);
094        add("NUTOKEN", DTDConstants.NUTOKEN);
095        add("NUTOKENS", DTDConstants.NUTOKENS);
096      }
097    };
098
099  /** Use serialVersionUID for interoperability. */
100  private static final long serialVersionUID = -1361214058742015233L;
101
102  /**
103   * The value of ( = pointer to ) the next attribute in the linked list,
104   * storing all attributes of some Element. Contains null for the
105   * last attribute.
106   */
107  public AttributeList next;
108
109  /**
110   * The name of the attribute. The attribute names are case insensitive.
111   */
112  public String name;
113
114  /**
115   * The default value of this attribute. Equals to null if no default value
116   * is specified.
117   */
118  public String value;
119
120  /**
121   * The explicit set of the allowed values of this attribute. Equals to
122   * null, if this parameter was not specified.
123   * Values, defined in DTD, are case insensitive.
124   */
125  public Vector<?> values;
126
127  /**
128   * The modifier of this attribute. This field contains one of the
129   * following DTD constants:
130   * <ul>
131   * <li> REQUIRED if the attribute value is always required,</li>
132   * <li> IMPLIED if the user agent must supply the default value itself,</li>
133   * <li> FIXED if the attribute value is fixed to some value and cannot
134   * be changed.</li>
135   * <li> DEFAULT if the attribute default value has been supplied.</li>
136   * <li> CURRENT the value that at any point in the document is
137   * the last value supplied for that element. A value is required to be
138   * supplied for the first* occurrence of an element</li>
139   * <li> CONREF specifies the IDREF value of
140   * the reference to content in another location of the document.
141   * The element with this attribute is empty, the content from
142   * that another location must be used instead.</li>
143   * </ul>
144   */
145  public int modifier;
146
147  /**
148   * The type of the attribute. The possible values of this field
149   * (NUMBER, NAME, ID, CDATA and so on) are defined in DTDConstants.
150   */
151  public int type;
152
153  /**
154   * Creates the attribute with the given name, initializing other fields
155   * to the default values ( 0 and null ).
156   *
157   * @param a_name The name of the attribute.
158   */
159  public AttributeList(String a_name)
160  {
161    name = a_name;
162  }
163
164  /**
165   * Creates the attribute with the given properties.
166   * @param a_name The name of the attribute
167   * @param a_type The type of the attribute. The possible values are defined
168   * in <code> DTDConstants</code>.
169   * @param a_modifier The modifier of this attribute. The possible values
170   * are defined in <code> DTDConstants</code>.
171   * @param a_default The default value of this attribute
172   * @param allowed_values The explicit set of the allowed values of
173   * this attribute
174   * @param a_next The value of the subsequent instance of the AttributeList,
175   * representing the next attribute definition for the same element.
176   * Equals to null for the last attribute definition.
177   */
178  public AttributeList(String a_name, int a_type, int a_modifier,
179                       String a_default, Vector<?> allowed_values,
180                       AttributeList a_next
181                      )
182  {
183    this(a_name);
184    type = a_type;
185    modifier = a_modifier;
186    value = a_default;
187    values = allowed_values;
188    next = a_next;
189  }
190
191  /**
192   * Get the modifier of this attribute. This field contains one of the
193   * following DTD constants:
194   * <ul>
195   * <li> REQUIRED if the attribute value is always required,</li>
196   * <li> IMPLIED if the user agent must supply the default value itself,</li>
197   * <li> FIXED if the attribute value is fixed to some value and cannot
198   * be changed.</li>
199   * <li> DEFAULT if the attribute default value has been supplied.</li>
200   * <li> CURRENT the value that at any point in the document is
201   * the last value supplied for that element. A value is required to be
202   * supplied for the first* occurrence of an element</li>
203   * <li> CONREF specifies the IDREF value of
204   * the reference to content in another location of the document.
205   * The element with this attribute is empty, the content from
206   * that another location must be used instead.</li>
207   * </ul>
208   */
209  public int getModifier()
210  {
211    return modifier;
212  }
213
214  /**
215   * Get the name of the attribute.
216   * The value is returned as it was supplied to a
217   * constructor, preserving the character case.
218   */
219  public String getName()
220  {
221    return name;
222  }
223
224  /**
225   * Get the value of ( = pointer to ) the next attribute in the linked list,
226   * storing all attributes of some Element. Contains null for the
227   * last attribute.
228   */
229  public AttributeList getNext()
230  {
231    return next;
232  }
233
234  /**
235   * Get the type of the attribute. The possible values of this field
236   * (NUMBER, NAME, ID, CDATA and so on) are defined in DTDConstants.
237   */
238  public int getType()
239  {
240    return type;
241  }
242
243  /**
244   * Get the default value of this attribute.
245   */
246  public String getValue()
247  {
248    return value;
249  }
250
251  /**
252   * Get the allowed values of this attribute.
253   */
254  public Enumeration<?> getValues()
255  {
256    return (values != null) ? values.elements() : null;
257  }
258
259  /**
260   * Converts a string value, representing a valid SGLM attribute type,
261   * into the corresponding value, defined in DTDConstants.
262   * @param typeName the name of the type (character case is ignored).
263   * @return a value from DTDConstants or DTDConstants.ANY if the
264   * string is not representing a known type. The known attribute types
265   * in this implementation are CDATA, ENTITY, ENTITIES, ID, IDREF, IDREFS,
266   *  NAME, NAMES, NMTOKEN, NMTOKENS, NOTATION, NUMBER, NUMBERS, NUTOKEN and
267   *  NUTOKENS.
268   * @throws NullPointerException if the passed parameter is null.
269   */
270  public static int name2type(String typeName)
271  {
272    return mapper.get(typeName.toUpperCase());
273  }
274
275  /**
276   * Returns the attribute name.
277   */
278  public String toString()
279  {
280    return name;
281  }
282
283  /**
284   * Converts a value from DTDConstants into the string representation.
285   * @param type - an integer value of the public static integer field,
286   * defined in the DTDConstants class.
287   * @return a corresponding SGML DTD keyword (UPPERCASE) or null if there
288   * are no attribute type constant having the given value.
289   */
290  public static String type2name(int type)
291  {
292    return mapper.get(type);
293  }
294}