KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > dtd > XMLContentSpec


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.impl.dtd;
59
60 /**
61  * ContentSpec really exists to aid the parser classes in implementing
62  * access to the grammar.
63  * <p>
64  * This class is used by the DTD scanner and the validator classes,
65  * allowing them to be used separately or together. This "struct"
66  * class is used to build content models for validation, where it
67  * is more efficient to fetch all of the information for each of
68  * these content model "fragments" than to fetch each field one at
69  * a time. Since configurations are allowed to have validators
70  * without a DTD scanner (i.e. a schema validator) and a DTD scanner
71  * without a validator (non-validating processor), this class can be
72  * used by each without requiring the presence of the other.
73  * <p>
74  * When processing element declarations, the DTD scanner will build
75  * up a representation of the content model using the node types that
76  * are defined here. Since a non-validating processor only needs to
77  * remember the type of content model declared (i.e. ANY, EMPTY, MIXED,
78  * or CHILDREN), it is free to discard the specific details of the
79  * MIXED and CHILDREN content models described using this class.
80  * <p>
81  * In the typical case of a validating processor reading the grammar
82  * of the document from a DTD, the information about the content model
83  * declared will be preserved and later "compiled" into an efficient
84  * form for use during element validation. Each content spec node
85  * that is saved is assigned a unique index that is used as a handle
86  * for the "value" or "otherValue" fields of other content spec nodes.
87  * A leaf node has a "value" that is either an index in the string
88  * pool of the element type of that leaf, or a value of -1 to indicate
89  * the special "#PCDATA" leaf type used in a mixed content model.
90  * <p>
91  * For a mixed content model, the content spec will be made up of
92  * leaf and choice content spec nodes, with an optional "zero or more"
93  * node. For example, the mixed content declaration "(#PCDATA)" would
94  * contain a single leaf node with a node value of -1. A mixed content
95  * declaration of "(#PCDATA|foo)*" would have a content spec consisting
96  * of two leaf nodes, for the "#PCDATA" and "foo" choices, a choice node
97  * with the "value" set to the index of the "#PCDATA" leaf node and the
98  * "otherValue" set to the index of the "foo" leaf node, and a "zero or
99  * more" node with the "value" set to the index of the choice node. If
100  * the content model has more choices, for example "(#PCDATA|a|b)*", then
101  * there will be more corresponding choice and leaf nodes, the choice
102  * nodes will be chained together through the "value" field with each
103  * leaf node referenced by the "otherValue" field.
104  * <p>
105  * For element content models, there are sequence nodes and also "zero or
106  * one" and "one or more" nodes. The leaf nodes would always have a valid
107  * string pool index, as the "#PCDATA" leaf is not used in the declarations
108  * for element content models.
109  *
110  * @version $Id: XMLContentSpec.java,v 1.3 2002/01/29 01:15:10 lehors Exp $
111  */

112 public class XMLContentSpec {
113
114     //
115
// Constants
116
//
117

118     /**
119      * Name or #PCDATA. Leaf nodes that represent parsed character
120      * data (#PCDATA) have values of -1.
121      */

122     public static final short CONTENTSPECNODE_LEAF = 0;
123
124     /** Represents a zero or one occurence count, '?'. */
125     public static final short CONTENTSPECNODE_ZERO_OR_ONE = 1;
126
127     /** Represents a zero or more occurence count, '*'. */
128     public static final short CONTENTSPECNODE_ZERO_OR_MORE = 2;
129     
130     /** Represents a one or more occurence count, '+'. */
131     public static final short CONTENTSPECNODE_ONE_OR_MORE = 3;
132     
133     /** Represents choice, '|'. */
134     public static final short CONTENTSPECNODE_CHOICE = 4;
135     
136     /** Represents sequence, ','. */
137     public static final short CONTENTSPECNODE_SEQ = 5;
138
139     /**
140      * Represents any namespace specified namespace. When the element
141      * found in the document must belong to a specific namespace,
142      * <code>otherValue</code> will contain the name of the namespace.
143      * If <code>otherValue</code> is <code>-1</code> then the element
144      * can be from any namespace.
145      * <p>
146      * Lists of valid namespaces are created from choice content spec
147      * nodes that have any content spec nodes as children.
148      */

149     public static final short CONTENTSPECNODE_ANY = 6;
150
151     /**
152      * Represents any other namespace (XML Schema: ##other).
153      * <p>
154      * When the content spec node type is set to CONTENTSPECNODE_ANY_OTHER,
155      * <code>value</code> will contain the namespace that <em>cannot</em>
156      * occur.
157      */

158     public static final short CONTENTSPECNODE_ANY_OTHER = 7;
159
160     /** Represents any local element (XML Schema: ##local). */
161     public static final short CONTENTSPECNODE_ANY_LOCAL = 8;
162
163     /** prcessContent is 'lax' **/
164     public static final short CONTENTSPECNODE_ANY_LAX = 22;
165
166     public static final short CONTENTSPECNODE_ANY_OTHER_LAX = 23;
167
168     public static final short CONTENTSPECNODE_ANY_LOCAL_LAX = 24;
169
170     /** processContent is 'skip' **/
171     
172     public static final short CONTENTSPECNODE_ANY_SKIP = 38;
173
174     public static final short CONTENTSPECNODE_ANY_OTHER_SKIP = 39;
175
176     public static final short CONTENTSPECNODE_ANY_LOCAL_SKIP = 40;
177     //
178
// Data
179
//
180

181     /**
182      * The content spec node type.
183      *
184      * @see #CONTENTSPECNODE_LEAF
185      * @see #CONTENTSPECNODE_ZERO_OR_ONE
186      * @see #CONTENTSPECNODE_ZERO_OR_MORE
187      * @see #CONTENTSPECNODE_ONE_OR_MORE
188      * @see #CONTENTSPECNODE_CHOICE
189      * @see #CONTENTSPECNODE_SEQ
190      */

191     public short type;
192
193     /**
194      * The "left hand" value object of the content spec node.
195      * leaf name.localpart, single child for unary ops, left child for binary ops.
196      */

197     public Object JavaDoc value;
198
199     /**
200      * The "right hand" value of the content spec node.
201      * leaf name.uri, right child for binary ops
202      */

203     public Object JavaDoc otherValue;
204
205     //
206
// Constructors
207
//
208

209     /** Default constructor. */
210     public XMLContentSpec() {
211         clear();
212     }
213
214     /** Constructs a content spec with the specified values. */
215     public XMLContentSpec(short type, Object JavaDoc value, Object JavaDoc otherValue) {
216         setValues(type, value, otherValue);
217     }
218
219     /**
220      * Constructs a content spec from the values in the specified content spec.
221      */

222     public XMLContentSpec(XMLContentSpec contentSpec) {
223         setValues(contentSpec);
224     }
225
226     /**
227      * Constructs a content spec from the values specified by the given
228      * content spec provider and identifier.
229      */

230     public XMLContentSpec(XMLContentSpec.Provider provider,
231                           int contentSpecIndex) {
232         setValues(provider, contentSpecIndex);
233     }
234
235     //
236
// Public methods
237
//
238

239     /** Clears the values. */
240     public void clear() {
241         type = -1;
242         value = null;
243         otherValue = null;
244     }
245
246     /** Sets the values. */
247     public void setValues(short type, Object JavaDoc value, Object JavaDoc otherValue) {
248         this.type = type;
249         this.value = value;
250         this.otherValue = otherValue;
251     }
252     
253     /** Sets the values of the specified content spec. */
254     public void setValues(XMLContentSpec contentSpec) {
255         type = contentSpec.type;
256         value = contentSpec.value;
257         otherValue = contentSpec.otherValue;
258     }
259
260     /**
261      * Sets the values from the values specified by the given content spec
262      * provider and identifier. If the specified content spec cannot be
263      * provided, the values of this content spec are cleared.
264      */

265     public void setValues(XMLContentSpec.Provider provider,
266                           int contentSpecIndex) {
267         if (!provider.getContentSpec(contentSpecIndex, this)) {
268             clear();
269         }
270     }
271
272
273     //
274
// Object methods
275
//
276

277     /** Returns a hash code for this node. */
278     public int hashCode() {
279         return type << 16 |
280                value.hashCode() << 8 |
281                otherValue.hashCode();
282     }
283
284     /** Returns true if the two objects are equal. */
285     public boolean equals(Object JavaDoc object) {
286         if (object != null && object instanceof XMLContentSpec) {
287             XMLContentSpec contentSpec = (XMLContentSpec)object;
288             return type == contentSpec.type &&
289                    value == contentSpec.value &&
290                    otherValue == contentSpec.otherValue;
291         }
292         return false;
293     }
294
295
296     //
297
// Interfaces
298
//
299

300     /**
301      * Provides a means for walking the structure built out of
302      * content spec "nodes". The user of this provider interface is
303      * responsible for knowing what the content spec node values
304      * "mean". If those values refer to content spec identifiers,
305      * then the user can call back into the provider to get the
306      * next content spec node in the structure.
307      */

308     public interface Provider {
309
310         //
311
// XMLContentSpec.Provider methods
312
//
313

314         /**
315          * Fills in the provided content spec structure with content spec
316          * information for a unique identifier.
317          *
318          * @param contentSpecIndex The content spec identifier. All content
319          * spec "nodes" have a unique identifier.
320          * @param contentSpec The content spec struct to fill in with
321          * the information.
322          *
323          * @return Returns true if the contentSpecIndex was found.
324          */

325         public boolean getContentSpec(int contentSpecIndex, XMLContentSpec contentSpec);
326
327     } // interface Provider
328

329 } // class XMLContentSpec
330

331
Popular Tags