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