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