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 @SuppressWarnings("serial") // Same-version serialization only
48 public final
49 class AttributeList implements DTDConstants, Serializable {
50 
51     /**
52      * The attribute name
53      */
54     public String name;
55 
56     /**
57      * The attribute type
58      */
59     public int type;
60 
61     /**
62      * The possible attribute values
63      */
64     public Vector<?> values;
65 
66     /**
67      * The attribute modifier
68      */
69     public int modifier;
70 
71     /**
72      * The default attribute value
73      */
74     public String value;
75 
76     /**
77      * The next attribute in the list
78      */
79     public AttributeList next;
80 
AttributeList()81     AttributeList() {
82     }
83 
84     /**
85      * Create an attribute list element.
86      *
87      * @param name  the attribute name
88      */
AttributeList(String name)89     public AttributeList(String name) {
90         this.name = name;
91     }
92 
93     /**
94      * Create an attribute list element.
95      *
96      * @param name      the attribute name
97      * @param type      the attribute type
98      * @param modifier  the attribute modifier
99      * @param value     the default attribute value
100      * @param values    the possible attribute values
101      * @param next      the next attribute in the list
102      */
AttributeList(String name, int type, int modifier, String value, Vector<?> values, AttributeList next)103     public AttributeList(String name, int type, int modifier, String value, Vector<?> values, AttributeList next) {
104         this.name = name;
105         this.type = type;
106         this.modifier = modifier;
107         this.value = value;
108         this.values = values;
109         this.next = next;
110     }
111 
112     /**
113      * @return attribute name
114      */
getName()115     public String getName() {
116         return name;
117     }
118 
119     /**
120      * @return attribute type
121      * @see DTDConstants
122      */
getType()123     public int getType() {
124         return type;
125     }
126 
127     /**
128      * @return attribute modifier
129      * @see DTDConstants
130      */
getModifier()131     public int getModifier() {
132         return modifier;
133     }
134 
135     /**
136      * @return possible attribute values
137      */
getValues()138     public Enumeration<?> getValues() {
139         return (values != null) ? values.elements() : null;
140     }
141 
142     /**
143      * @return default attribute value
144      */
getValue()145     public String getValue() {
146         return value;
147     }
148 
149     /**
150      * @return the next attribute in the list
151      */
getNext()152     public AttributeList getNext() {
153         return next;
154     }
155 
156     /**
157      * @return string representation
158      */
toString()159     public String toString() {
160         return name;
161     }
162 
163     /**
164      * Create a hashtable of attribute types.
165      */
166     static Hashtable<Object, Object> attributeTypes = new Hashtable<Object, Object>();
167 
defineAttributeType(String nm, int val)168     static void defineAttributeType(String nm, int val) {
169         Integer num = Integer.valueOf(val);
170         attributeTypes.put(nm, num);
171         attributeTypes.put(num, nm);
172     }
173 
174     static {
175         defineAttributeType("CDATA", CDATA);
176         defineAttributeType("ENTITY", ENTITY);
177         defineAttributeType("ENTITIES", ENTITIES);
178         defineAttributeType("ID", ID);
179         defineAttributeType("IDREF", IDREF);
180         defineAttributeType("IDREFS", IDREFS);
181         defineAttributeType("NAME", NAME);
182         defineAttributeType("NAMES", NAMES);
183         defineAttributeType("NMTOKEN", NMTOKEN);
184         defineAttributeType("NMTOKENS", NMTOKENS);
185         defineAttributeType("NOTATION", NOTATION);
186         defineAttributeType("NUMBER", NUMBER);
187         defineAttributeType("NUMBERS", NUMBERS);
188         defineAttributeType("NUTOKEN", NUTOKEN);
189         defineAttributeType("NUTOKENS", NUTOKENS);
190 
191         attributeTypes.put("fixed", Integer.valueOf(FIXED));
192         attributeTypes.put("required", Integer.valueOf(REQUIRED));
193         attributeTypes.put("current", Integer.valueOf(CURRENT));
194         attributeTypes.put("conref", Integer.valueOf(CONREF));
195         attributeTypes.put("implied", Integer.valueOf(IMPLIED));
196     }
197 
198     /**
199      * Converts an attribute name to the type
200      *
201      * @param nm an attribute name
202      * @return the type
203      */
name2type(String nm)204     public static int name2type(String nm) {
205         Integer i = (Integer)attributeTypes.get(nm);
206         return (i == null) ? CDATA : i.intValue();
207     }
208 
209     /**
210      * Converts a type to the attribute name
211      *
212      * @param tp a type
213      * @return the attribute name
214      */
type2name(int tp)215     public static String type2name(int tp) {
216         return (String)attributeTypes.get(Integer.valueOf(tp));
217     }
218 }
219