001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.dtd;
019:
020: /**
021: * ContentSpec really exists to aid the parser classes in implementing
022: * access to the grammar.
023: * <p>
024: * This class is used by the DTD scanner and the validator classes,
025: * allowing them to be used separately or together. This "struct"
026: * class is used to build content models for validation, where it
027: * is more efficient to fetch all of the information for each of
028: * these content model "fragments" than to fetch each field one at
029: * a time. Since configurations are allowed to have validators
030: * without a DTD scanner (i.e. a schema validator) and a DTD scanner
031: * without a validator (non-validating processor), this class can be
032: * used by each without requiring the presence of the other.
033: * <p>
034: * When processing element declarations, the DTD scanner will build
035: * up a representation of the content model using the node types that
036: * are defined here. Since a non-validating processor only needs to
037: * remember the type of content model declared (i.e. ANY, EMPTY, MIXED,
038: * or CHILDREN), it is free to discard the specific details of the
039: * MIXED and CHILDREN content models described using this class.
040: * <p>
041: * In the typical case of a validating processor reading the grammar
042: * of the document from a DTD, the information about the content model
043: * declared will be preserved and later "compiled" into an efficient
044: * form for use during element validation. Each content spec node
045: * that is saved is assigned a unique index that is used as a handle
046: * for the "value" or "otherValue" fields of other content spec nodes.
047: * A leaf node has a "value" that is either an index in the string
048: * pool of the element type of that leaf, or a value of -1 to indicate
049: * the special "#PCDATA" leaf type used in a mixed content model.
050: * <p>
051: * For a mixed content model, the content spec will be made up of
052: * leaf and choice content spec nodes, with an optional "zero or more"
053: * node. For example, the mixed content declaration "(#PCDATA)" would
054: * contain a single leaf node with a node value of -1. A mixed content
055: * declaration of "(#PCDATA|foo)*" would have a content spec consisting
056: * of two leaf nodes, for the "#PCDATA" and "foo" choices, a choice node
057: * with the "value" set to the index of the "#PCDATA" leaf node and the
058: * "otherValue" set to the index of the "foo" leaf node, and a "zero or
059: * more" node with the "value" set to the index of the choice node. If
060: * the content model has more choices, for example "(#PCDATA|a|b)*", then
061: * there will be more corresponding choice and leaf nodes, the choice
062: * nodes will be chained together through the "value" field with each
063: * leaf node referenced by the "otherValue" field.
064: * <p>
065: * For element content models, there are sequence nodes and also "zero or
066: * one" and "one or more" nodes. The leaf nodes would always have a valid
067: * string pool index, as the "#PCDATA" leaf is not used in the declarations
068: * for element content models.
069: *
070: * @xerces.internal
071: *
072: * @version $Id: XMLContentSpec.java 446755 2006-09-15 21:56:27Z mrglavas $
073: */
074: public class XMLContentSpec {
075:
076: //
077: // Constants
078: //
079:
080: /**
081: * Name or #PCDATA. Leaf nodes that represent parsed character
082: * data (#PCDATA) have values of -1.
083: */
084: public static final short CONTENTSPECNODE_LEAF = 0;
085:
086: /** Represents a zero or one occurence count, '?'. */
087: public static final short CONTENTSPECNODE_ZERO_OR_ONE = 1;
088:
089: /** Represents a zero or more occurence count, '*'. */
090: public static final short CONTENTSPECNODE_ZERO_OR_MORE = 2;
091:
092: /** Represents a one or more occurence count, '+'. */
093: public static final short CONTENTSPECNODE_ONE_OR_MORE = 3;
094:
095: /** Represents choice, '|'. */
096: public static final short CONTENTSPECNODE_CHOICE = 4;
097:
098: /** Represents sequence, ','. */
099: public static final short CONTENTSPECNODE_SEQ = 5;
100:
101: /**
102: * Represents any namespace specified namespace. When the element
103: * found in the document must belong to a specific namespace,
104: * <code>otherValue</code> will contain the name of the namespace.
105: * If <code>otherValue</code> is <code>-1</code> then the element
106: * can be from any namespace.
107: * <p>
108: * Lists of valid namespaces are created from choice content spec
109: * nodes that have any content spec nodes as children.
110: */
111: public static final short CONTENTSPECNODE_ANY = 6;
112:
113: /**
114: * Represents any other namespace (XML Schema: ##other).
115: * <p>
116: * When the content spec node type is set to CONTENTSPECNODE_ANY_OTHER,
117: * <code>value</code> will contain the namespace that <em>cannot</em>
118: * occur.
119: */
120: public static final short CONTENTSPECNODE_ANY_OTHER = 7;
121:
122: /** Represents any local element (XML Schema: ##local). */
123: public static final short CONTENTSPECNODE_ANY_LOCAL = 8;
124:
125: /** prcessContent is 'lax' **/
126: public static final short CONTENTSPECNODE_ANY_LAX = 22;
127:
128: public static final short CONTENTSPECNODE_ANY_OTHER_LAX = 23;
129:
130: public static final short CONTENTSPECNODE_ANY_LOCAL_LAX = 24;
131:
132: /** processContent is 'skip' **/
133:
134: public static final short CONTENTSPECNODE_ANY_SKIP = 38;
135:
136: public static final short CONTENTSPECNODE_ANY_OTHER_SKIP = 39;
137:
138: public static final short CONTENTSPECNODE_ANY_LOCAL_SKIP = 40;
139: //
140: // Data
141: //
142:
143: /**
144: * The content spec node type.
145: *
146: * @see #CONTENTSPECNODE_LEAF
147: * @see #CONTENTSPECNODE_ZERO_OR_ONE
148: * @see #CONTENTSPECNODE_ZERO_OR_MORE
149: * @see #CONTENTSPECNODE_ONE_OR_MORE
150: * @see #CONTENTSPECNODE_CHOICE
151: * @see #CONTENTSPECNODE_SEQ
152: */
153: public short type;
154:
155: /**
156: * The "left hand" value object of the content spec node.
157: * leaf name.localpart, single child for unary ops, left child for binary ops.
158: */
159: public Object value;
160:
161: /**
162: * The "right hand" value of the content spec node.
163: * leaf name.uri, right child for binary ops
164: */
165: public Object otherValue;
166:
167: //
168: // Constructors
169: //
170:
171: /** Default constructor. */
172: public XMLContentSpec() {
173: clear();
174: }
175:
176: /** Constructs a content spec with the specified values. */
177: public XMLContentSpec(short type, Object value, Object otherValue) {
178: setValues(type, value, otherValue);
179: }
180:
181: /**
182: * Constructs a content spec from the values in the specified content spec.
183: */
184: public XMLContentSpec(XMLContentSpec contentSpec) {
185: setValues(contentSpec);
186: }
187:
188: /**
189: * Constructs a content spec from the values specified by the given
190: * content spec provider and identifier.
191: */
192: public XMLContentSpec(XMLContentSpec.Provider provider,
193: int contentSpecIndex) {
194: setValues(provider, contentSpecIndex);
195: }
196:
197: //
198: // Public methods
199: //
200:
201: /** Clears the values. */
202: public void clear() {
203: type = -1;
204: value = null;
205: otherValue = null;
206: }
207:
208: /** Sets the values. */
209: public void setValues(short type, Object value, Object otherValue) {
210: this .type = type;
211: this .value = value;
212: this .otherValue = otherValue;
213: }
214:
215: /** Sets the values of the specified content spec. */
216: public void setValues(XMLContentSpec contentSpec) {
217: type = contentSpec.type;
218: value = contentSpec.value;
219: otherValue = contentSpec.otherValue;
220: }
221:
222: /**
223: * Sets the values from the values specified by the given content spec
224: * provider and identifier. If the specified content spec cannot be
225: * provided, the values of this content spec are cleared.
226: */
227: public void setValues(XMLContentSpec.Provider provider,
228: int contentSpecIndex) {
229: if (!provider.getContentSpec(contentSpecIndex, this )) {
230: clear();
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 | value.hashCode() << 8
241: | otherValue.hashCode();
242: }
243:
244: /** Returns true if the two objects are equal. */
245: public boolean equals(Object object) {
246: if (object != null && object instanceof XMLContentSpec) {
247: XMLContentSpec contentSpec = (XMLContentSpec) object;
248: return type == contentSpec.type
249: && value == contentSpec.value
250: && otherValue == contentSpec.otherValue;
251: }
252: return false;
253: }
254:
255: //
256: // Interfaces
257: //
258:
259: /**
260: * Provides a means for walking the structure built out of
261: * content spec "nodes". The user of this provider interface is
262: * responsible for knowing what the content spec node values
263: * "mean". If those values refer to content spec identifiers,
264: * then the user can call back into the provider to get the
265: * next content spec node in the structure.
266: *
267: * @xerces.internal
268: */
269: public interface Provider {
270:
271: //
272: // XMLContentSpec.Provider methods
273: //
274:
275: /**
276: * Fills in the provided content spec structure with content spec
277: * information for a unique identifier.
278: *
279: * @param contentSpecIndex The content spec identifier. All content
280: * spec "nodes" have a unique identifier.
281: * @param contentSpec The content spec struct to fill in with
282: * the information.
283: *
284: * @return Returns true if the contentSpecIndex was found.
285: */
286: public boolean getContentSpec(int contentSpecIndex,
287: XMLContentSpec contentSpec);
288:
289: } // interface Provider
290:
291: } // class XMLContentSpec
|