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.Vector;
49  import java.util.Enumeration;
50  
51  /**
52   * Implementation of DTD Content Particle specification, used to define the content
53   * of an element.
54   * @author <a href="mailto:totok@intalio.com">Alexander Totok</a>
55   * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
56   */
57  public class ContentParticle {
58  
59     private static final short REFERENCE = 0;
60     private static final short SEQ = 1;
61     private static final short CHOICE = 2;
62  
63     private static final short ONE = 3;
64     private static final short ZERO_OR_ONE = 4;
65     private static final short ZERO_OR_MORE = 5;
66     private static final short ONE_OR_MORE = 6;
67  
68     /**
69      * Type of the content particle. Value may be {@link #REFERENCE REFERENCE}
70      * - content is exactly one child-element, {@link #SEQ SEQ} - content
71      * is a choice list of content particles, {@link #CHOICE CHOICE}
72      * - content is a sequence list of content particles, or -1 if unspecified.
73      */
74     private short type = -1;
75  
76     /**
77      * Specifies occurance of the content particle. Value may be
78      * {@link #ONE ONE} - one occurance, {@link #ZERO_OR_ONE ZERO_OR_ONE}
79      * - zero or one occurance, {@link #ONE_OR_MORE ONE_OR_MORE} - one or more
80      * occurances, {@link #ZERO_OR_MORE ZERO_OR_MORE} - zero or more
81      * occurances.
82      */
83     private short occuranceSpec;
84  
85     /**
86      * Name of the child, if the content is exactly one child-element.
87      */
88     private String reference = null;
89  
90     /**
91      * Content particles - children of this content particle, if the type is
92      * a sequence list or choice list of content particles.
93      */
94     private Vector children = null;
95  
96     /**
97      * Constructor, setting occurance specification to <tt>ONE</tt>, by default.
98      */
99     public ContentParticle() {
100       occuranceSpec = ONE;
101       children = new Vector();
102    } //-- ContentParticle
103 
104    /**
105     * Creates content particle and sets its type to <tt>REFERENCE</tt>, that is
106     * the content is exactly one child-element.
107     * @param reference name of this child-element.
108     */
109    public ContentParticle(String reference) {
110       this();
111       setReferenceType(reference);
112    } //-- ContentParticle
113 
114    /**
115     * Makes the content particle represent the content with exactly one
116     * child-element.
117     * @param reference the name of this child-element - must not be
118     * <tt>null</tt> or equal to empty String.
119     */
120    public void setReferenceType(String reference) {
121 
122       if (reference == null || reference.equals("")) {
123          String err = "ContentParticle: name of the reference child must not be empty.";
124          throw new IllegalArgumentException(err);
125       }
126 
127       type = REFERENCE;
128       this.reference = reference;
129    } //-- setReferenceType
130 
131    /**
132     * <b>True</b> if the content is exactly on child-element,
133     * <b>false</b> otherwise.
134     */
135    public boolean isReferenceType() {
136       return type == REFERENCE;
137    } //-- isReferenceType
138 
139    /**
140     * Sets the content to sequence list of content particles.
141     */
142    public void setSeqType() {
143       type = SEQ;
144    } //-- setSeqType
145 
146    /**
147     * <b>True</b> if the content is a sequence list of content particles,
148     * <b>false</b> otherwise.
149     */
150    public boolean isSeqType() {
151       return type == SEQ;
152    } //-- isSeqType
153 
154    /**
155     * Sets the content to choice list of content particles.
156     */
157    public void setChoiceType() {
158       type = CHOICE;
159    } //-- setChoiceType
160 
161    /**
162     * <b>True</b> if the content is a choice list of content particles,
163     * <b>false</b> otherwise.
164     */
165    public boolean isChoiceType() {
166       return type == CHOICE;
167    } //-- isChoiceType
168 
169    /**
170     * Returns the name of the child element, if content is exactly one child.
171     */
172    public String getReference() {
173       return reference;
174    } //-- getReference
175 
176    /**
177     * Returns enumeration of the children - content particles, that form
178     * the content of this Content Particle, if has
179     * <tt>SEQ</tt> or <tt>CHOICE</tt> type (sequence or choice list),
180     * <tt>null</tt> otherwise.
181     */
182    public Enumeration getChildren() {
183       if (isSeqType() || isChoiceType()) return children.elements();
184       return null;
185    } //-- getChildren
186 
187    /**
188     * Sets occurance specification of the content particle to <tt>ONE</tt>.
189     */
190    public void setOneOccurance() {
191       occuranceSpec = ONE;
192    } //-- setOneOccurance
193 
194    /**
195     * <b>True</b> if ocurence specification of the content particle is <tt>ONE</tt>,
196     * <b>false</b> otherwise.
197     */
198    public boolean isOneOccurance() {
199       return occuranceSpec == ONE;
200    } //-- isOneOccurance
201 
202    /**
203     * Sets occurance specification of the content particle to
204     * <tt>ZERO_OR_ONE</tt>.
205     */
206    public void setZeroOrOneOccurance() {
207       occuranceSpec = ZERO_OR_ONE;
208    } //-- setZeroOrOneOccurance
209 
210    /**
211     * <b>True</b> if occurance specification of the content particle
212     * is <tt>ZERO_OR_ONE</tt>, <b>false</b> otherwise.
213     */
214    public boolean isZeroOrOneOccurance() {
215       return occuranceSpec == ZERO_OR_ONE;
216    } //-- isZeroOrOneOccurance
217 
218    /**
219     * Sets occurance specification of the content particle to
220     * <tt>ONE_OR_MORE</tt>.
221     */
222    public void setOneOrMoreOccurances() {
223       occuranceSpec = ONE_OR_MORE;
224    } //-- setOneOrMoreOccurances
225 
226    /**
227     * <b>True</b> if occurance specification of the content particle
228     * is <tt>ONE_OR_MORE</tt>, <b>false</b> otherwise.
229     */
230    public boolean isOneOrMoreOccurances() {
231       return occuranceSpec == ONE_OR_MORE;
232    } //-- isOneOrMoreOccurances
233 
234    /**
235     * Sets occurance specification of the content particle to
236     * <tt>ZERO_OR_MORE</tt>.
237     */
238    public void setZeroOrMoreOccurances() {
239       occuranceSpec = ZERO_OR_MORE;
240    } //-- setZeroOrMoreOccurances
241 
242    /**
243     * <b>True</b> if occurance specification of the content particle
244     * is <tt>ZERO_OR_MORE</tt>, <b>false</b> otherwise.
245     */
246    public boolean isZeroOrMoreOccurances() {
247       return occuranceSpec == ZERO_OR_MORE;
248    } //-- isZeroOrMoreOccurances
249 
250    /**
251     * Adds child to the vector of child elements (content particles).
252     * @param cp content particle to add to the vector of children.
253     */
254    public synchronized void addChild(ContentParticle cp) {
255       children.add(cp);
256    } //-- addChild
257 
258 } //-- ContentParticle