001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 2001 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: Wildcard.java 6230 2006-09-19 07:56:07Z wguttmn $
044: * Date Author Changes
045: * 04/02/2001 Arnaud Blandin Created
046: */package org.exolab.castor.xml.schema;
047:
048: import java.util.Enumeration;
049: import java.util.Vector;
050: import org.exolab.castor.xml.ValidationException;
051:
052: /**
053: * A class that represents an XML Schema Wildcard.
054: * A wilcard is represented by the XML elements {@literal <any>} and
055: * {@literal <anyAttribute>} and can be hold in a complexType or in
056: * a ModelGroup (<group>).
057: *
058: * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
059: */
060: public class Wildcard extends Particle {
061: /** SerialVersionUID */
062: private static final long serialVersionUID = -2747251897459951684L;
063:
064: /**
065: * The vector where we store the list of namespaces
066: */
067: //don't use ArrayList to keep compatibility with jdk1.1
068: private Vector _namespaces;
069:
070: /**
071: * A boolean that indicates if this wildcard represents
072: * {@literal <anyAttribute>}.
073: * By default a wildcard represents {@literal <any>}
074: */
075: private boolean _attribute = false;
076:
077: /**
078: * The complexType that holds this wildcard.
079: */
080: private ComplexType _complexType;
081:
082: /**
083: * The Group (<sequence> or <choice>) that holds this wildcard.
084: */
085: private Group _group;
086:
087: /**
088: * The Attribute Group that holds the wildcard
089: */
090: private AttributeGroup _attGroup;
091:
092: /**
093: * the processContent of this wildcard.
094: * (strict by default)
095: */
096: private String _processContents;
097:
098: /**
099: * The wildcard is embedded in a complexType
100: * @param complexType the complexType that contains this wildcard
101: */
102: public Wildcard(ComplexType complexType) {
103: _complexType = complexType;
104: init();
105: }
106:
107: /**
108: * The wildcard is embedded in a ModelGroup (<group>)
109: * @param group the ModelGoup that contains this wildcard
110: */
111: public Wildcard(Group group) {
112: _group = group;
113: init();
114: }
115:
116: /**
117: * The wildcard is embedded in an AttributeGroup.
118: * @param attGroup the AttributeGroup that contains this wildcard
119: */
120: public Wildcard(AttributeGroup attGroup) {
121: _attGroup = attGroup;
122: init();
123: }
124:
125: private void init() {
126: //in general not more than one namespace
127: _namespaces = new Vector(1);
128: setMaxOccurs(1);
129: setMinOccurs(1);
130: try {
131: setProcessContents(SchemaNames.STRICT);
132: } catch (SchemaException e) {
133: //nothing to do since we are
134: //not 'out of bounds' by using an hard coded value
135: }
136: }
137:
138: /**
139: * add a namespace
140: * @param Namespace the namespace to add
141: */
142: public void addNamespace(String Namespace) {
143: _namespaces.addElement(Namespace);
144: }
145:
146: /**
147: * Removes the given namespace from the namespace collection
148: * @param namespace the namespace to remove.
149: */
150: public boolean removeNamespace(String namespace) {
151: if (namespace == null)
152: return false;
153: int position = _namespaces.indexOf(namespace);
154: if (position >= 0) {
155: _namespaces.removeElementAt(position);
156: return true;
157: }
158: return false;
159: }
160:
161: /**
162: * Returns the complexType that contains this wildcard, can return null.
163: * @return the complexType that contains this wildcard (can be null).
164: */
165: public ComplexType getComplexType() {
166: return _complexType;
167: }
168:
169: /**
170: * Returns the model group that contains this wildcard, can return null.
171: * @return the model group that contains this wildcard (can be null).
172: */
173: public Group getModelGroup() {
174: return _group;
175: }
176:
177: /**
178: * Returns the AttributeGroup that contains this wilcard (can return null)
179: * @return the AttributeGroup that contains this wilcard (can return null)
180: */
181: public AttributeGroup getAttributeGroup() {
182: return _attGroup;
183: }
184:
185: /**
186: * Returns the parent schema in which this wildcard is located.
187: *
188: * @return the schema that contains the parent structure of this wildcard.
189: */
190: public Schema getSchema() {
191: if (_complexType != null)
192: return _complexType.getSchema();
193: else if (_attGroup != null) {
194: if (_attGroup instanceof AttributeGroupDecl)
195: return ((AttributeGroupDecl) _attGroup).getSchema();
196: else if (_attGroup instanceof AttributeGroupReference) {
197: AttributeGroup tempRef = ((AttributeGroupReference) _attGroup)
198: .resolveReference();
199: if (tempRef instanceof AttributeGroupDecl)
200: return ((AttributeGroupDecl) tempRef).getSchema();
201: tempRef = null;
202: return null;
203: }
204: }
205: if (_group == null)
206: return null;
207:
208: Structure parent = _group.getParent();
209: if (parent == null)
210: return null;
211:
212: Schema result = null;
213: while (result == null) {
214: switch (parent.getStructureType()) {
215: case Structure.COMPLEX_TYPE:
216: result = ((ComplexType) parent).getSchema();
217: break;
218: case Structure.MODELGROUP:
219: result = ((ModelGroup) parent).getSchema();
220: break;
221: case Structure.GROUP:
222: parent = ((Group) parent).getParent();
223: break;
224: default:
225: String err = "A group can only be child of a complexType";
226: err += " or a ModelGroup or a group.";
227: throw new IllegalStateException(err);
228: }
229: }
230: return result;
231: }
232:
233: /**
234: * Returns an enumeration that contains the different namespaces
235: * of this wildcard
236: * @return an enumeration that contains the different namespaces
237: * of this wildcard
238: */
239: public Enumeration getNamespaces() {
240: return _namespaces.elements();
241: }
242:
243: /**
244: * Returns the processContent of this wildcard
245: * @return the processContent of this wildcard
246: */
247: public String getProcessContent() {
248: return _processContents;
249: }
250:
251: /**
252: * Returns true if this wildcard represents {@literal <anyAttribute>} otherwise false
253: * @return true if this wildcard represents {@literal <anyAttribute>} otherwise false
254: */
255: public boolean isAttributeWildcard() {
256: return _attribute;
257: }
258:
259: /**
260: * Sets this wildcard to represent {@literal <anyAttribute>}
261: */
262: public void setAttributeWildcard() {
263: _attribute = true;
264: }
265:
266: /**
267: * Sets the ID for this Group
268: * @param id the ID for this Group
269: */
270: public void setId(final String id) {
271: }
272:
273: /**
274: * Sets the processContent of the wildCard
275: * @param process the process content to set
276: * @exception SchemaException thrown when the processContent is not valid
277: */
278: public void setProcessContents(String process)
279: throws SchemaException {
280: if (!SchemaNames.isProcessName(process))
281: throw new SchemaException(
282: "processContents attribute not valid:" + process);
283: _processContents = process;
284: }
285:
286: public void validate() throws ValidationException {
287: //only do the validation on the namespace
288: }
289:
290: /**
291: * Returns the type of this Schema Structure
292: * @return the type of this Schema Structure
293: **/
294: public short getStructureType() {
295: return Structure.WILDCARD;
296: }
297: }
|