View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2001 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   * Date         Author          Changes
45   * 04/02/2001   Arnaud Blandin  Created
46   */
47  
48  package org.exolab.castor.xml.schema;
49  
50  import java.util.Enumeration;
51  import java.util.Vector;
52  import org.exolab.castor.xml.ValidationException;
53  
54  /**
55   * A class that represents an XML Schema Wildcard.
56   * A wilcard is represented by the XML elements {@literal <any>} and
57   * {@literal <anyAttribute>} and can be hold in a complexType or in
58   * a ModelGroup (<group>).
59   *
60   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
61   */
62  public class Wildcard extends Particle {
63      /** SerialVersionUID */
64      private static final long serialVersionUID = -2747251897459951684L;
65  
66      /**
67       * The vector where we store the list of namespaces
68       */
69      //don't use ArrayList to keep compatibility with jdk1.1
70      private Vector _namespaces;
71  
72      /**
73       * A boolean that indicates if this wildcard represents
74       * {@literal <anyAttribute>}.
75       * By default a wildcard represents {@literal <any>}
76       */
77      private boolean _attribute = false;
78  
79      /**
80       * The complexType that holds this wildcard.
81       */
82       private ComplexType _complexType;
83  
84      /**
85       * The Group (<sequence> or <choice>) that holds this wildcard.
86       */
87       private Group _group;
88  
89       /**
90        * The Attribute Group that holds the wildcard
91        */
92        private AttributeGroup  _attGroup;
93  
94      /**
95       * the processContent of this wildcard.
96       * (strict by default)
97       */
98       private String _processContents;
99  
100     /**
101      * The wildcard is embedded in a complexType
102      * @param complexType the complexType that contains this wildcard
103      */
104     public Wildcard(ComplexType complexType) {
105         _complexType = complexType;
106         init();
107     }
108 
109     /**
110      * The wildcard is embedded in a ModelGroup (<group>)
111      * @param group the ModelGoup that contains this wildcard
112      */
113     public Wildcard(Group group) {
114         _group = group;
115         init();
116     }
117 
118     /**
119      * The wildcard is embedded in an AttributeGroup.
120      * @param attGroup the AttributeGroup that contains this wildcard
121      */
122     public Wildcard(AttributeGroup attGroup) {
123         _attGroup = attGroup;
124         init();
125     }
126 
127     private void init() {
128         //in general not more than one namespace
129         _namespaces = new Vector(1);
130         setMaxOccurs(1);
131         setMinOccurs(1);
132         try {
133            setProcessContents(SchemaNames.STRICT);
134         } catch (SchemaException e) {
135            //nothing to do since we are
136            //not 'out of bounds' by using an hard coded value
137         }
138     }
139 
140     /**
141      * add a namespace
142      * @param Namespace the namespace to add
143      */
144      public void addNamespace(String Namespace) {
145          _namespaces.addElement(Namespace);
146      }
147 
148    /**
149     * Removes the given namespace from the namespace collection
150     * @param namespace the namespace to remove.
151     */
152     public boolean removeNamespace(String namespace) {
153        if (namespace == null)
154            return false;
155        int position = _namespaces.indexOf(namespace);
156 	   if (position >= 0) {
157 	        _namespaces.removeElementAt(position);
158 	         return true;
159 	   }
160        return false;
161     }
162 
163     /**
164      * Returns the complexType that contains this wildcard, can return null.
165      * @return the complexType that contains this wildcard (can be null).
166      */
167     public ComplexType getComplexType() {
168         return _complexType;
169     }
170 
171     /**
172      * Returns the model group that contains this wildcard, can return null.
173      * @return the model group that contains this wildcard (can be null).
174      */
175     public Group getModelGroup() {
176         return _group;
177     }
178 
179     /**
180      * Returns the AttributeGroup that contains this wilcard (can return null)
181      * @return the AttributeGroup that contains this wilcard (can return null)
182      */
183     public AttributeGroup getAttributeGroup() {
184         return _attGroup;
185     }
186 
187     /**
188      * Returns the parent schema in which this wildcard is located.
189      *
190      * @return the schema that contains the parent structure of this wildcard.
191      */
192     public Schema getSchema() {
193         if (_complexType != null)
194             return _complexType.getSchema();
195         else if (_attGroup != null) {
196             if (_attGroup instanceof AttributeGroupDecl)
197                 return ((AttributeGroupDecl)_attGroup).getSchema();
198             else if (_attGroup instanceof AttributeGroupReference) {
199                 AttributeGroup tempRef = ((AttributeGroupReference)_attGroup).resolveReference();
200                 if (tempRef instanceof AttributeGroupDecl)
201                     return ((AttributeGroupDecl)tempRef).getSchema();
202                 tempRef = null;
203                 return null;
204             }
205         }
206         if (_group == null) return null;
207         
208         Structure parent = _group.getParent();
209         if (parent == null) return null;
210         
211         Schema result = null;
212         while (result == null) {
213             switch (parent.getStructureType()) {
214                 case Structure.COMPLEX_TYPE:
215                     result = ((ComplexType)parent).getSchema();
216                     break;
217                 case Structure.MODELGROUP:
218                     result = ((ModelGroup)parent).getSchema();
219                     break;
220                 case Structure.GROUP:
221                     parent = ((Group)parent).getParent();
222                     break;
223                 default:
224                     String err = "A group can only be child of a complexType";
225                     err += " or a ModelGroup or a group.";
226                     throw new IllegalStateException(err);
227             }
228         }
229         return result;
230     }
231     /**
232      * Returns an enumeration that contains the different namespaces
233      * of this wildcard
234      * @return an enumeration that contains the different namespaces
235      * of this wildcard
236      */
237      public Enumeration getNamespaces() {
238          return _namespaces.elements();
239      }
240 
241     /**
242      * Returns the processContent of this wildcard
243      * @return the processContent of this wildcard
244      */
245      public String getProcessContent() {
246          return _processContents;
247      }
248 
249 
250     /**
251      * Returns true if this wildcard represents {@literal <anyAttribute>} otherwise false
252      * @return true if this wildcard represents {@literal <anyAttribute>} otherwise false
253      */
254      public boolean isAttributeWildcard() {
255          return _attribute;
256      }
257 
258     /**
259      * Sets this wildcard to represent {@literal <anyAttribute>}
260      */
261     public void setAttributeWildcard() {
262          _attribute = true;
263     }
264 
265     /**
266      * Sets the ID for this Group
267      * @param id the ID for this Group
268      */
269     public void setId(final String id) { }
270 
271 
272     /**
273      * Sets the processContent of the wildCard
274      * @param process the process content to set
275      * @exception SchemaException thrown when the processContent is not valid
276      */
277     public void setProcessContents(String process)
278         throws SchemaException
279     {
280         if (!SchemaNames.isProcessName(process))
281            throw new SchemaException("processContents attribute not valid:" +process);
282         _processContents = process;
283     }
284     public void validate() throws ValidationException {
285     //only do the validation on the namespace
286     }
287 
288     /**
289      * Returns the type of this Schema Structure
290      * @return the type of this Schema Structure
291     **/
292     public short getStructureType() {
293         return Structure.WILDCARD;
294     }
295 }