1 /*
   2  * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing.text.html.parser;
  27 
  28 import java.util.Vector;
  29 import java.util.Hashtable;
  30 import java.util.Enumeration;
  31 import java.io.*;
  32 
  33 /**
  34  * This class defines the attributes of an SGML element
  35  * as described in a DTD using the ATTLIST construct.
  36  * An AttributeList can be obtained from the Element
  37  * class using the getAttributes() method.
  38  * <p>
  39  * It is actually an element in a linked list. Use the
  40  * getNext() method repeatedly to enumerate all the attributes
  41  * of an element.
  42  *
  43  * @see         Element
  44  * @author      Arthur Van Hoff
  45  *
  46  */
  47 public final
  48 class AttributeList implements DTDConstants, Serializable {
  49     public String name;
  50     public int type;
  51     public Vector<?> values;
  52     public int modifier;
  53     public String value;
  54     public AttributeList next;
  55 
  56     AttributeList() {
  57     }
  58 
  59     /**
  60      * Create an attribute list element.
  61      */
  62     public AttributeList(String name) {
  63         this.name = name;
  64     }
  65 
  66     /**
  67      * Create an attribute list element.
  68      */
  69     public AttributeList(String name, int type, int modifier, String value, Vector<?> values, AttributeList next) {
  70         this.name = name;
  71         this.type = type;
  72         this.modifier = modifier;
  73         this.value = value;
  74         this.values = values;
  75         this.next = next;
  76     }
  77 
  78     /**
  79      * @return attribute name
  80      */
  81     public String getName() {
  82         return name;
  83     }
  84 
  85     /**
  86      * @return attribute type
  87      * @see DTDConstants
  88      */
  89     public int getType() {
  90         return type;
  91     }
  92 
  93     /**
  94      * @return attribute modifier
  95      * @see DTDConstants
  96      */
  97     public int getModifier() {
  98         return modifier;
  99     }
 100 
 101     /**
 102      * @return possible attribute values
 103      */
 104     public Enumeration<?> getValues() {
 105         return (values != null) ? values.elements() : null;
 106     }
 107 
 108     /**
 109      * @return default attribute value
 110      */
 111     public String getValue() {
 112         return value;
 113     }
 114 
 115     /**
 116      * @return the next attribute in the list
 117      */
 118     public AttributeList getNext() {
 119         return next;
 120     }
 121 
 122     /**
 123      * @return string representation
 124      */
 125     public String toString() {
 126         return name;
 127     }
 128 
 129     /**
 130      * Create a hashtable of attribute types.
 131      */
 132     static Hashtable<Object, Object> attributeTypes = new Hashtable<Object, Object>();
 133 
 134     static void defineAttributeType(String nm, int val) {
 135         Integer num = Integer.valueOf(val);
 136         attributeTypes.put(nm, num);
 137         attributeTypes.put(num, nm);
 138     }
 139 
 140     static {
 141         defineAttributeType("CDATA", CDATA);
 142         defineAttributeType("ENTITY", ENTITY);
 143         defineAttributeType("ENTITIES", ENTITIES);
 144         defineAttributeType("ID", ID);
 145         defineAttributeType("IDREF", IDREF);
 146         defineAttributeType("IDREFS", IDREFS);
 147         defineAttributeType("NAME", NAME);
 148         defineAttributeType("NAMES", NAMES);
 149         defineAttributeType("NMTOKEN", NMTOKEN);
 150         defineAttributeType("NMTOKENS", NMTOKENS);
 151         defineAttributeType("NOTATION", NOTATION);
 152         defineAttributeType("NUMBER", NUMBER);
 153         defineAttributeType("NUMBERS", NUMBERS);
 154         defineAttributeType("NUTOKEN", NUTOKEN);
 155         defineAttributeType("NUTOKENS", NUTOKENS);
 156 
 157         attributeTypes.put("fixed", Integer.valueOf(FIXED));
 158         attributeTypes.put("required", Integer.valueOf(REQUIRED));
 159         attributeTypes.put("current", Integer.valueOf(CURRENT));
 160         attributeTypes.put("conref", Integer.valueOf(CONREF));
 161         attributeTypes.put("implied", Integer.valueOf(IMPLIED));
 162     }
 163 
 164     public static int name2type(String nm) {
 165         Integer i = (Integer)attributeTypes.get(nm);
 166         return (i == null) ? CDATA : i.intValue();
 167     }
 168 
 169     public static String type2name(int tp) {
 170         return (String)attributeTypes.get(Integer.valueOf(tp));
 171     }
 172 }