KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dtd > XMLContentSpec


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.dtd;
18
19 /**
20  * ContentSpec really exists to aid the parser classes in implementing
21  * access to the grammar.
22  * <p>
23  * This class is used by the DTD scanner and the validator classes,
24  * allowing them to be used separately or together. This "struct"
25  * class is used to build content models for validation, where it
26  * is more efficient to fetch all of the information for each of
27  * these content model "fragments" than to fetch each field one at
28  * a time. Since configurations are allowed to have validators
29  * without a DTD scanner (i.e. a schema validator) and a DTD scanner
30  * without a validator (non-validating processor), this class can be
31  * used by each without requiring the presence of the other.
32  * <p>
33  * When processing element declarations, the DTD scanner will build
34  * up a representation of the content model using the node types that
35  * are defined here. Since a non-validating processor only needs to
36  * remember the type of content model declared (i.e. ANY, EMPTY, MIXED,
37  * or CHILDREN), it is free to discard the specific details of the
38  * MIXED and CHILDREN content models described using this class.
39  * <p>
40  * In the typical case of a validating processor reading the grammar
41  * of the document from a DTD, the information about the content model
42  * declared will be preserved and later "compiled" into an efficient
43  * form for use during element validation. Each content spec node
44  * that is saved is assigned a unique index that is used as a handle
45  * for the "value" or "otherValue" fields of other content spec nodes.
46  * A leaf node has a "value" that is either an index in the string
47  * pool of the element type of that leaf, or a value of -1 to indicate
48  * the special "#PCDATA" leaf type used in a mixed content model.
49  * <p>
50  * For a mixed content model, the content spec will be made up of
51  * leaf and choice content spec nodes, with an optional "zero or more"
52  * node. For example, the mixed content declaration "(#PCDATA)" would
53  * contain a single leaf node with a node value of -1. A mixed content
54  * declaration of "(#PCDATA|foo)*" would have a content spec consisting
55  * of two leaf nodes, for the "#PCDATA" and "foo" choices, a choice node
56  * with the "value" set to the index of the "#PCDATA" leaf node and the
57  * "otherValue" set to the index of the "foo" leaf node, and a "zero or
58  * more" node with the "value" set to the index of the choice node. If
59  * the content model has more choices, for example "(#PCDATA|a|b)*", then
60  * there will be more corresponding choice and leaf nodes, the choice
61  * nodes will be chained together through the "value" field with each
62  * leaf node referenced by the "otherValue" field.
63  * <p>
64  * For element content models, there are sequence nodes and also "zero or
65  * one" and "one or more" nodes. The leaf nodes would always have a valid
66  * string pool index, as the "#PCDATA" leaf is not used in the declarations
67  * for element content models.
68  *
69  * @xerces.internal
70  *
71  * @version $Id: XMLContentSpec.java,v 1.5 2004/10/04 21:57:30 mrglavas Exp $
72  */

73 public class XMLContentSpec {
74
75     //
76
// Constants
77
//
78

79     /**
80      * Name or #PCDATA. Leaf nodes that represent parsed character
81      * data (#PCDATA) have values of -1.
82      */

83     public static final short CONTENTSPECNODE_LEAF = 0;
84
85     /** Represents a zero or one occurence count, '?'. */
86     public static final short CONTENTSPECNODE_ZERO_OR_ONE = 1;
87
88     /** Represents a zero or more occurence count, '*'. */
89     public static final short CONTENTSPECNODE_ZERO_OR_MORE = 2;
90     
91     /** Represents a one or more occurence count, '+'. */
92     public static final short CONTENTSPECNODE_ONE_OR_MORE = 3;
93     
94     /** Represents choice, '|'. */
95     public static final short CONTENTSPECNODE_CHOICE = 4;
96     
97     /** Represents sequence, ','. */
98     public static final short CONTENTSPECNODE_SEQ = 5;
99
100     /**
101      * Represents any namespace specified namespace. When the element
102      * found in the document must belong to a specific namespace,
103      * <code>otherValue</code> will contain the name of the namespace.
104      * If <code>otherValue</code> is <code>-1</code> then the element
105      * can be from any namespace.
106      * <p>
107      * Lists of valid namespaces are created from choice content spec
108      * nodes that have any content spec nodes as children.
109      */

110     public static final short CONTENTSPECNODE_ANY = 6;
111
112     /**
113      * Represents any other namespace (XML Schema: ##other).
114      * <p>
115      * When the content spec node type is set to CONTENTSPECNODE_ANY_OTHER,
116      * <code>value</code> will contain the namespace that <em>cannot</em>
117      * occur.
118      */

119     public static final short CONTENTSPECNODE_ANY_OTHER = 7;
120
121     /** Represents any local element (XML Schema: ##local). */
122     public static final short CONTENTSPECNODE_ANY_LOCAL = 8;
123
124     /** prcessContent is 'lax' **/
125     public static final short CONTENTSPECNODE_ANY_LAX = 22;
126
127     public static final short CONTENTSPECNODE_ANY_OTHER_LAX = 23;
128
129     public static final short CONTENTSPECNODE_ANY_LOCAL_LAX = 24;
130
131     /** processContent is 'skip' **/
132     
133     public static final short CONTENTSPECNODE_ANY_SKIP = 38;
134
135     public static final short CONTENTSPECNODE_ANY_OTHER_SKIP = 39;
136
137     public static final short CONTENTSPECNODE_ANY_LOCAL_SKIP = 40;
138     //
139
// Data
140
//
141

142     /**
143      * The content spec node type.
144      *
145      * @see #CONTENTSPECNODE_LEAF
146      * @see #CONTENTSPECNODE_ZERO_OR_ONE
147      * @see #CONTENTSPECNODE_ZERO_OR_MORE
148      * @see #CONTENTSPECNODE_ONE_OR_MORE
149      * @see #CONTENTSPECNODE_CHOICE
150      * @see #CONTENTSPECNODE_SEQ
151      */

152     public short type;
153
154     /**
155      * The "left hand" value object of the content spec node.
156      * leaf name.localpart, single child for unary ops, left child for binary ops.
157      */

158     public Object JavaDoc value;
159
160     /**
161      * The "right hand" value of the content spec node.
162      * leaf name.uri, right child for binary ops
163      */

164     public Object JavaDoc otherValue;
165
166     //
167
// Constructors
168
//
169

170     /** Default constructor. */
171     public XMLContentSpec() {
172         clear();
173     }
174
175     /** Constructs a content spec with the specified values. */
176     public XMLContentSpec(short type, Object JavaDoc value, Object JavaDoc otherValue) {
177         setValues(type, value, otherValue);
178     }
179
180     /**
181      * Constructs a content spec from the values in the specified content spec.
182      */

183     public XMLContentSpec(XMLContentSpec contentSpec) {
184         setValues(contentSpec);
185     }
186
187     /**
188      * Constructs a content spec from the values specified by the given
189      * content spec provider and identifier.
190      */

191     public XMLContentSpec(XMLContentSpec.Provider provider,
192                           int contentSpecIndex) {
193         setValues(provider, contentSpecIndex);
194     }
195
196     //
197
// Public methods
198
//
199

200     /** Clears the values. */
201     public void clear() {
202         type = -1;
203         value = null;
204         otherValue = null;
205     }
206
207     /** Sets the values. */
208     public void setValues(short type, Object JavaDoc value, Object JavaDoc otherValue) {
209         this.type = type;
210         this.value = value;
211         this.otherValue = otherValue;
212     }
213     
214     /** Sets the values of the specified content spec. */
215     public void setValues(XMLContentSpec contentSpec) {
216         type = contentSpec.type;
217         value = contentSpec.value;
218         otherValue = contentSpec.otherValue;
219     }
220
221     /**
222      * Sets the values from the values specified by the given content spec
223      * provider and identifier. If the specified content spec cannot be
224      * provided, the values of this content spec are cleared.
225      */

226     public void setValues(XMLContentSpec.Provider provider,
227                           int contentSpecIndex) {
228         if (!provider.getContentSpec(contentSpecIndex, this)) {
229             clear();
230         }
231     }
232
233
234     //
235
// Object methods
236
//
237

238     /** Returns a hash code for this node. */
239     public int hashCode() {
240         return type << 16 |
241                value.hashCode() << 8 |
242                otherValue.hashCode();
243     }
244
245     /** Returns true if the two objects are equal. */
246     public boolean equals(Object JavaDoc object) {
247         if (object != null && object instanceof XMLContentSpec) {
248             XMLContentSpec contentSpec = (XMLContentSpec)object;
249             return type == contentSpec.type &&
250                    value == contentSpec.value &&
251                    otherValue == contentSpec.otherValue;
252         }
253         return false;
254     }
255
256
257     //
258
// Interfaces
259
//
260

261     /**
262      * Provides a means for walking the structure built out of
263      * content spec "nodes". The user of this provider interface is
264      * responsible for knowing what the content spec node values
265      * "mean". If those values refer to content spec identifiers,
266      * then the user can call back into the provider to get the
267      * next content spec node in the structure.
268      *
269      * @xerces.internal
270      */

271     public interface Provider {
272
273         //
274
// XMLContentSpec.Provider methods
275
//
276

277         /**
278          * Fills in the provided content spec structure with content spec
279          * information for a unique identifier.
280          *
281          * @param contentSpecIndex The content spec identifier. All content
282          * spec "nodes" have a unique identifier.
283          * @param contentSpec The content spec struct to fill in with
284          * the information.
285          *
286          * @return Returns true if the contentSpecIndex was found.
287          */

288         public boolean getContentSpec(int contentSpecIndex, XMLContentSpec contentSpec);
289
290     } // interface Provider
291

292 } // class XMLContentSpec
293

294
Popular Tags