1 /*
   2  * Copyright (c) 1998, 2014, 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     private static final long serialVersionUID = -1361214058742015233L;
  50 
  51     public String name;
  52     public int type;
  53     public Vector<?> values;
  54     public int modifier;
  55     public String value;
  56     public AttributeList next;
  57 
  58     AttributeList() {
  59     }
  60 
  61     /**
  62      * Create an attribute list element.
  63      */
  64     public AttributeList(String name) {
  65         this.name = name;
  66     }
  67 
  68     /**
  69      * Create an attribute list element.
  70      */
  71     public AttributeList(String name, int type, int modifier, String value, Vector<?> values, AttributeList next) {
  72         this.name = name;
  73         this.type = type;
  74         this.modifier = modifier;
  75         this.value = value;
  76         this.values = values;
  77         this.next = next;
  78     }
  79 
  80     /**
  81      * @return attribute name
  82      */
  83     public String getName() {
  84         return name;
  85     }
  86 
  87     /**
  88      * @return attribute type
  89      * @see DTDConstants
  90      */
  91     public int getType() {
  92         return type;
  93     }
  94 
  95     /**
  96      * @return attribute modifier
  97      * @see DTDConstants
  98      */
  99     public int getModifier() {
 100         return modifier;
 101     }
 102 
 103     /**
 104      * @return possible attribute values
 105      */
 106     public Enumeration<?> getValues() {
 107         return (values != null) ? values.elements() : null;
 108     }
 109 
 110     /**
 111      * @return default attribute value
 112      */
 113     public String getValue() {
 114         return value;
 115     }
 116 
 117     /**
 118      * @return the next attribute in the list
 119      */
 120     public AttributeList getNext() {
 121         return next;
 122     }
 123 
 124     /**
 125      * @return string representation
 126      */
 127     public String toString() {
 128         return name;
 129     }
 130 
 131     /**
 132      * Create a hashtable of attribute types.
 133      */
 134     static Hashtable<Object, Object> attributeTypes = new Hashtable<Object, Object>();
 135 
 136     static void defineAttributeType(String nm, int val) {
 137         Integer num = Integer.valueOf(val);
 138         attributeTypes.put(nm, num);
 139         attributeTypes.put(num, nm);
 140     }
 141 
 142     static {
 143         defineAttributeType("CDATA", CDATA);
 144         defineAttributeType("ENTITY", ENTITY);
 145         defineAttributeType("ENTITIES", ENTITIES);
 146         defineAttributeType("ID", ID);
 147         defineAttributeType("IDREF", IDREF);
 148         defineAttributeType("IDREFS", IDREFS);
 149         defineAttributeType("NAME", NAME);
 150         defineAttributeType("NAMES", NAMES);
 151         defineAttributeType("NMTOKEN", NMTOKEN);
 152         defineAttributeType("NMTOKENS", NMTOKENS);
 153         defineAttributeType("NOTATION", NOTATION);
 154         defineAttributeType("NUMBER", NUMBER);
 155         defineAttributeType("NUMBERS", NUMBERS);
 156         defineAttributeType("NUTOKEN", NUTOKEN);
 157         defineAttributeType("NUTOKENS", NUTOKENS);
 158 
 159         attributeTypes.put("fixed", Integer.valueOf(FIXED));
 160         attributeTypes.put("required", Integer.valueOf(REQUIRED));
 161         attributeTypes.put("current", Integer.valueOf(CURRENT));
 162         attributeTypes.put("conref", Integer.valueOf(CONREF));
 163         attributeTypes.put("implied", Integer.valueOf(IMPLIED));
 164     }
 165 
 166     public static int name2type(String nm) {
 167         Integer i = (Integer)attributeTypes.get(nm);
 168         return (i == null) ? CDATA : i.intValue();
 169     }
 170 
 171     public static String type2name(int tp) {
 172         return (String)attributeTypes.get(Integer.valueOf(tp));
 173     }
 174 }