View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2000 (C) Intalio Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  
46  package org.exolab.castor.xml.dtd;
47  
48  import java.util.HashSet;
49  import java.util.Iterator;
50  
51  /**
52   * Implementation of DTD Attribute declaration specification.
53   * @author <a href="mailto:totok@intalio.com">Alexander Totok</a>
54   * @version $Revision$ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
55   */
56  public class Attribute {
57  
58     private static final short CDATA = 0;
59     private static final short ID = 1;
60     private static final short IDREF = 2;
61     private static final short IDREFS = 3;
62     private static final short ENTITY = 4;
63     private static final short ENTITIES = 5;
64     private static final short NMTOKEN = 6;
65     private static final short NMTOKENS = 7;
66     private static final short NOTATION = 8;
67     private static final short Enumeration = 9;
68  
69     private static final short DEFAULT = 10;
70     private static final short REQUIRED = 11;
71     private static final short IMPLIED = 12;
72     private static final short FIXED = 13;
73  
74     /**
75      * Name of the attribute.
76      */
77     private String name;
78  
79     /**
80      * Element owning this attribute.
81      */
82     private Element element;
83  
84     /**
85      * Type of the attribute. Value may be
86      * {@link #CDATA CDATA}, {@link #ID ID}, {@link #IDREF IDREF},
87      * {@link #IDREFS IDREFS}, {@link #ENTITY ENTITY}, {@link #ENTITIES ENTITIES},
88      * {@link #NMTOKEN NMTOKEN}, {@link #NMTOKENS NMTOKENS},
89      * {@link #NOTATION NOTATION}, {@link #Enumeration Enumeration} or -1,
90      * if unspecified.
91      */
92     private short type = -1;
93  
94     /**
95      * Specifies occurance of the attribute. Value may be {@link #DEFAULT DEFAULT}
96      * - default attribute value is specified, presence of the attribute
97      * is not required, {@link #REQUIRED REQUIRED} - the presence ot
98      * the attribute is required and no default value is specified,
99      * {@link #IMPLIED IMPLIED} - default value is not specified and presence
100     * of the attribute is not required, or {@link #FIXED FIXED} -
101     * the attribute must have fixed value, which is specified, however
102     * attribute may be present, but its value must match the default value.
103     */
104    private short occuranceType;
105 
106    /**
107     * Default value of the attribute.
108     */
109    private String defaultValue = null;
110 
111    /**
112     * Possible values of the attribute (if the attribute is of <tt>NOTATION</tt>
113     * or <tt>Enumeration</tt> type).
114     */
115    private HashSet values;
116 
117    /**
118     * Constructor, setting name, owning element of the attribute and
119     * occurance specification to <tt>DEFAULT</tt>.
120     * @param element must not be <tt>null</tt>.
121     * @param name must not be <tt>null</tt> or equal to empty String.
122     */
123    public Attribute(Element element, String name) {
124 
125       if (name == null || name.equals("")) {
126          String err = "Attribute constructor: name must not be empty.";
127          throw new IllegalArgumentException(err);
128       }
129 
130       if (element == null) {
131          String err = "Attribute constructor: element must not be null.";
132          throw new IllegalArgumentException(err);
133       }
134 
135       this.name = name;
136       this.element = element;
137       values = new HashSet();
138       occuranceType = DEFAULT;
139    } //-- Attribute
140 
141    /**
142     * Returns the name of the attribute.
143     */
144    public String getName() {
145       return name;
146    } //-- getName
147 
148    /**
149     * Returns Element owning this attribute.
150     */
151    public Element getElement() {
152       return element;
153    } //-- getElement
154 
155    /**
156     * Returns {@link java.util.Iterator iterator} of the set of possible values,
157     * if of <tt>NOTATION</tt> or <tt>Enumeration</tt> type, <tt>null</tt> otherwise.
158     */
159    public Iterator getValues() {
160       if (isNOTATIONType() || isEnumerationType()) return values.iterator();
161       return null;
162    } //-- getValues
163 
164    /**
165     * Sets the type of the attribute to <tt>CDATA</tt>.
166     */
167    public void setStringType() {
168       type = CDATA;
169    } //-- setStringType
170 
171    /**
172     * <b>True</b> if the attribute is of <tt>CDATA</tt> type,
173     * <b>false</b> otherwise.
174     */
175    public boolean isStringType() {
176       return type == CDATA;
177    } //-- isStringType
178 
179    /**
180     * Sets the type of the attribute to <tt>ID</tt>.
181     */
182    public void setIDType() {
183       type = ID;
184    } //-- setIDType
185 
186    /**
187     * <b>True</b> if the attribute is of <tt>ID</tt> type, <b>false</b> otherwise.
188     */
189    public boolean isIDType() {
190       return type == ID;
191    } //-- isIDType
192 
193    /**
194     * Sets the type of the attribute to <tt>IDREF</tt>.
195     */
196    public void setIDREFType() {
197       type = IDREF;
198    } //-- setIDREFType
199 
200    /**
201     * <b>True</b> if the attribute is of <tt>IDREF</tt> type,
202     * <b>false</b> otherwise.
203     */
204    public boolean isIDREFType() {
205       return type == IDREF;
206    } //-- isIDREFType
207 
208    /**
209     * Sets the type of the attribute to <tt>IDREFS</tt>.
210     */
211    public void setIDREFSType() {
212       type = IDREFS;
213    } //-- setIDREFSType
214 
215    /**
216     * <b>True</b> if the attribute is of <tt>IDREFS</tt> type,
217     * <b>false</b> otherwise.
218     */
219    public boolean isIDREFSType() {
220       return type == IDREFS;
221    } //-- isIDREFSType
222 
223    /**
224     * Sets the type of the attribute to <tt>ENTITY</tt>.
225     */
226    public void setENTITYType() {
227       type = ENTITY;
228    } //-- setENTITYType
229 
230    /**
231     * <b>True</b> if the attribute is of <tt>ENTITY</tt> type,
232     * <b>false</b> otherwise.
233     */
234    public boolean isENTITYType() {
235       return type == ENTITY;
236    } //-- isENTITYType
237 
238    /**
239     * Sets the type of the attribute to <tt>ENTITIES</tt>.
240     */
241    public void setENTITIESType() {
242       type = ENTITIES;
243    } //-- setENTITIESType
244 
245    /**
246     * <b>True</b> if the attribute is of <tt>ENTITIES</tt> type,
247     * <b>false</b> otherwise.
248     */
249    public boolean isENTITIESType() {
250       return type == ENTITIES;
251    } //-- isENTITIESType
252 
253    /**
254     * Sets the type of the attribute to <tt>NMTOKEN</tt>.
255     */
256    public void setNMTOKENType() {
257       type = NMTOKEN;
258    } //-- setNMTOKENType
259 
260    /**
261     * <b>True</b> if the attribute is of <tt>NMTOKEN</tt> type,
262     * <b>false</b> otherwise.
263     */
264    public boolean isNMTOKENType() {
265       return type == NMTOKEN;
266    } //-- isNMTOKENType
267 
268    /**
269     * Sets the type of the attribute to <tt>NMTOKENS</tt>.
270     */
271    public void setNMTOKENSType() {
272       type = NMTOKENS;
273    } //-- setNMTOKENSType
274 
275    /**
276     * <b>True</b> if the attribute is of <tt>NMTOKENS</tt> type,
277     * <b>false</b> otherwise.
278     */
279    public boolean isNMTOKENSType() {
280       return type == NMTOKENS;
281    } //-- isNMTOKENSType
282 
283    /**
284     * Sets the type of the attribute to <tt>NOTATION</tt>.
285     */
286    public void setNOTATIONType() {
287       type = NOTATION;
288    } //-- setNOTATIONType
289 
290    /**
291     * <b>True</b> if the attribute is of <tt>NOTATION</tt> type,
292     * <b>false</b> otherwise.
293     */
294    public boolean isNOTATIONType() {
295       return type == NOTATION;
296    } //-- isNOTATIONType
297 
298    /**
299     * Sets the type of the attribute to <tt>Enumeration</tt>.
300     */
301    public void setEnumerationType() {
302       type = Enumeration;
303    } //-- setEnumerationType
304 
305    /**
306     * <b>True</b> if the attribute is of <tt>Enumeration</tt> type, <b>false</b> otherwise.
307     */
308    public boolean isEnumerationType() {
309       return type == Enumeration;
310    } //-- isEnumerationType
311 
312    /**
313     * Sets occurance specification to <tt>DEFAULT</tt>.
314     */
315    public void setDEFAULT() {
316       occuranceType = DEFAULT;
317    } //-- setDEFAULT
318 
319    /**
320     * <b>True</b> if attribute's default value is specified, <b>false</b> otherwise.
321     */
322    public boolean isDEFAULT() {
323       return occuranceType == DEFAULT;
324    } //-- isDEFAULT
325 
326    /**
327     * Sets occurance specification to <tt>REQUIRED</tt>.
328     */
329    public void setREQUIRED() {
330       occuranceType = REQUIRED;
331    } //-- setREQUIRED
332 
333    /**
334     * <b>True</b> if the attribute is required, <b>false</b> otherwise.
335     */
336    public boolean isREQUIRED() {
337       return occuranceType == REQUIRED;
338    } //-- isREQUIRED
339 
340    /**
341     * Sets occurance specification to <tt>IMPLIED</tt>.
342     */
343    public void setIMPLIED() {
344       occuranceType = IMPLIED;
345    } //-- setIMOLIED
346 
347    /**
348     * <b>True</b> if no default value for the attribute is provided
349     * ("IMPLIED" specification), <b>false</b> otherwise.
350     */
351    public boolean isIMPLIED() {
352       return occuranceType == IMPLIED;
353    } //-- isIMPLIED
354 
355    /**
356     * Sets occurance specification to <tt>FIXED</tt>.
357     */
358    public void setFIXED() {
359       occuranceType = FIXED;
360    } //-- setFIXED
361 
362    /**
363     * <b>True</b> if the attribute has fixed value, <b>false</b> otherwise.
364     */
365    public boolean isFIXED() {
366       return occuranceType == FIXED;
367    } //-- isFIXED
368 
369    /**
370     * Sets default value.
371     */
372    public void setDefaultValue(String value) {
373       defaultValue = value;
374    } //-- setDefaultValue
375 
376    /**
377     * Returns default value.
378     */
379    public String getDefaultValue() {
380       return defaultValue;
381    } //-- getDefaultValue
382 
383    /**
384     * Adds the <tt>value</tt> to the set of possible values.
385     * @throws DTDException if the <tt>value</tt> is already contained
386     * in the set of possible values.
387     */
388    public synchronized void addValue(String value) throws DTDException {
389       if (values.contains(value)) {
390          String err = "The value \"" + value + "\" is already contained in the set";
391          err += " of possible values of \"" + name + "\" attribute.";
392          throw new DTDException(err);
393       }
394       values.add(value);
395    } //-- addvalue
396 
397 } //-- Attribute