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 1999-2002 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  package org.exolab.castor.xml.schema.reader;
37  
38  import org.exolab.castor.xml.AttributeSet;
39  import org.exolab.castor.xml.Namespaces;
40  import org.exolab.castor.xml.XMLException;
41  import org.exolab.castor.xml.schema.Annotation;
42  import org.exolab.castor.xml.schema.AttributeGroup;
43  import org.exolab.castor.xml.schema.ComplexType;
44  import org.exolab.castor.xml.schema.SchemaContext;
45  import org.exolab.castor.xml.schema.Group;
46  import org.exolab.castor.xml.schema.Schema;
47  import org.exolab.castor.xml.schema.SchemaException;
48  import org.exolab.castor.xml.schema.SchemaNames;
49  import org.exolab.castor.xml.schema.Wildcard;
50  
51  import java.util.StringTokenizer;
52  
53  /**
54   * A class for Unmarshalling WildCard
55   * 
56   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
57   * @version $Revision$ $Date: 2003-07-03 15:49:44 -0600 (Thu, 03 Jul 2003) $
58   **/
59  public class WildcardUnmarshaller extends ComponentReader {
60  
61  
62    /**
63     * The value of the maximum occurance wild card
64     */
65    private static final String MAX_OCCURS_WILDCARD = "unbounded";
66  
67  
68    // --------------------/
69    // - Member Variables -/
70    // --------------------/
71  
72    /**
73     * The current ComponentReader
74     **/
75    private ComponentReader unmarshaller;
76  
77    /**
78     * The current branch depth
79     **/
80    private int depth = 0;
81  
82    /**
83     * The wildcard we are constructing
84     */
85    private Wildcard _wildcard = null;
86    /**
87     * The Schema being "unmarshalled"
88     **/
89    private Schema _schema = null;
90  
91    /**
92     * The element name of the Group to be unmarshalled.
93     */
94    private String _element = SchemaNames.ANY;
95  
96  
97    // ----------------/
98    // - Constructors -/
99    // ----------------/
100   public WildcardUnmarshaller(final SchemaContext schemaContext, final ComplexType complexType,
101       final Schema schema, final String element, final AttributeSet atts) {
102     this(schemaContext, schema, element, atts, new Wildcard(complexType));
103   }
104 
105   public WildcardUnmarshaller(final SchemaContext schemaContext, final Group group,
106       final Schema schema, final String element, final AttributeSet atts) {
107     this(schemaContext, schema, element, atts, new Wildcard(group));
108   }
109 
110   public WildcardUnmarshaller(final SchemaContext schemaContext, final AttributeGroup attGroup,
111       final Schema schema, final String element, final AttributeSet atts) {
112     this(schemaContext, schema, element, atts, new Wildcard(attGroup));
113   }
114 
115 
116   /**
117    * Creates a new WildcardUnmarshaller.
118    * 
119    * @param schemaContext the {@link SchemaContext} to get some configuration settings from
120    * @param schema the Schema to which the {@link Wildcard} belongs
121    * @param element the name of the element
122    * @param atts the AttributeList
123    **/
124   private WildcardUnmarshaller(final SchemaContext schemaContext, final Schema schema,
125       final String element, final AttributeSet atts, final Wildcard wildcard) {
126     super(schemaContext);
127     _wildcard = wildcard;
128     this._schema = schema;
129     this._element = element;
130 
131     // -- handle attributes
132     String attValue = null;
133 
134     if (SchemaNames.ANY_ATTRIBUTE.equals(element))
135       _wildcard.setAttributeWildcard();
136     _element = element;
137 
138     // --namespace
139     attValue = atts.getValue(SchemaNames.NAMESPACE);
140 
141     if (attValue != null) {
142       // check if there is more than one namespace
143       StringTokenizer tokenizer = new StringTokenizer(attValue);
144       while (tokenizer.hasMoreTokens()) {
145         // need to retrieve all the namespaces
146         String temp = tokenizer.nextToken();
147         // if there is more than one namespace ##any or ##other should not
148         // appear
149         /** @todo optimize the following? */
150         if (tokenizer.countTokens() > 1)
151           if ((SchemaNames.NAMESPACE_ANY.equals(temp))
152               || (SchemaNames.NAMESPACE_OTHER.equals(temp)))
153             throw new IllegalArgumentException(
154                 temp + " is not valid when multiple namespaces are listed.");
155 
156         /**
157          * @todo validation on the value of the attribute we need a way to check the validity of an
158          *       URI. A temporary solution if to assume that the URI are URL.
159          * @see SchemaNames#isNamespaceName()
160          */
161         if (SchemaNames.isNamespaceName(temp))
162           _wildcard.addNamespace(temp);
163         else {
164           String err = "Invalid 'namespace' value: " + temp;
165           throw new IllegalArgumentException(err);
166         }
167       }
168     } // if
169     else
170       _wildcard.addNamespace(SchemaNames.NAMESPACE_ANY);
171 
172     /*
173      * @maxOccurs If maxOccurs is present, the value is either unbounded or the int value of the
174      * attribute, otherwise maxOccurs equals the minOccurs value.
175      */
176     attValue = atts.getValue(SchemaNames.MAX_OCCURS_ATTR);
177     if (attValue != null) {
178       if (_wildcard.isAttributeWildcard())
179         throw new IllegalStateException("'maxOccurs' is prohibited on a <anyAttribute> element.");
180       if (MAX_OCCURS_WILDCARD.equals(attValue))
181         attValue = "-1";
182       int maxOccurs = toInt(attValue);
183       _wildcard.setMaxOccurs(maxOccurs);
184     }
185     // -- minOccurs
186     attValue = atts.getValue("minOccurs");
187     if (attValue != null) {
188       if (_wildcard.isAttributeWildcard())
189         throw new IllegalStateException("'minOccurs' is prohibited on a <anyAttribute> element.");
190       _wildcard.setMinOccurs(toInt(attValue));
191     }
192     // -- processContents
193     attValue = atts.getValue("processContents");
194 
195     if (attValue != null) {
196       try {
197         _wildcard.setProcessContents(attValue);
198       } catch (SchemaException e) {
199         throw new IllegalArgumentException(e.getMessage());
200       }
201     }
202 
203 
204     // -- id
205     _wildcard.setId(atts.getValue("id"));
206 
207   } // -- WildCardUnmarshaller
208 
209   // -----------/
210   // - Methods -/
211   // -----------/
212 
213   /**
214    * Returns the name of the element that this ComponentReader handles
215    * 
216    * @return the name of the element that this ComponentReader handles
217    **/
218   public String elementName() {
219     return _element;
220   } // -- elementName
221 
222 
223   /**
224    * Returns the Wildcard unmarshalled by this Unmarshaller.
225    * 
226    * @return the unmarshalled Wildcard
227    */
228   public Wildcard getWildcard() {
229     return _wildcard;
230   }
231 
232   /**
233    * Returns the Object created by this ComponentReader
234    * 
235    * @return the Object created by this ComponentReader
236    **/
237   public Object getObject() {
238     return getWildcard();
239   } // -- getObject
240 
241   /**
242    * Signals the start of an element with the given name.
243    *
244    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
245    *        prefix).
246    * @param namespace the namespace of the element. This may be null. Note: A null namespace is not
247    *        the same as the default namespace unless the default namespace is also null.
248    * @param atts the AttributeSet containing the attributes associated with the element.
249    * @param nsDecls the namespace declarations being declared for this element. This may be null.
250    **/
251   public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
252       throws XMLException {
253     // -- Do delagation if necessary
254     if (unmarshaller != null) {
255       unmarshaller.startElement(name, namespace, atts, nsDecls);
256       ++depth;
257       return;
258     }
259     // -- <annotation>
260     if (SchemaNames.ANNOTATION.equals(name)) {
261       unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
262     }
263 
264     else {
265       String err = new StringBuffer("illegal element <").append(name).append("> found in <")
266           .append(_element).append('>').toString();
267       throw new SchemaException(err);
268     }
269 
270   } // -- startElement
271 
272   /**
273    * Signals to end of the element with the given name.
274    *
275    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
276    *        prefix).
277    * @param namespace the namespace of the element.
278    **/
279   public void endElement(String name, String namespace) throws XMLException {
280 
281     // -- Do delagation if necessary
282     if ((unmarshaller != null) && (depth > 0)) {
283       unmarshaller.endElement(name, namespace);
284       --depth;
285       return;
286     }
287 
288     // -- check for name mismatches
289     if (unmarshaller != null) {
290       if (!name.equals(unmarshaller.elementName())) {
291         String err = "missing end element for ";
292         err += unmarshaller.elementName();
293         throw new SchemaException(err);
294       }
295     }
296 
297     // -- have unmarshaller perform any necessary clean up
298     unmarshaller.finish();
299 
300     // -- <annotation>
301     if (name == SchemaNames.ANNOTATION) {
302       _schema.addAnnotation((Annotation) unmarshaller.getObject());
303     }
304     unmarshaller = null;
305   } // -- endElement
306 
307   public void characters(char[] ch, int start, int length) throws XMLException {
308     // -- Do delagation if necessary
309     if (unmarshaller != null) {
310       unmarshaller.characters(ch, start, length);
311     }
312   } // -- characters
313 
314 }