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