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 2000-2002 (C) Intalio Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  package org.exolab.castor.xml.schema.reader;
37  
38  // -- imported classes and packages
39  import org.exolab.castor.xml.AttributeSet;
40  import org.exolab.castor.xml.Namespaces;
41  import org.exolab.castor.xml.XMLException;
42  import org.exolab.castor.xml.schema.Annotation;
43  import org.exolab.castor.xml.schema.Facet;
44  import org.exolab.castor.xml.schema.Schema;
45  import org.exolab.castor.xml.schema.SchemaContext;
46  import org.exolab.castor.xml.schema.SchemaNames;
47  import org.exolab.castor.xml.schema.SimpleType;
48  import org.exolab.castor.xml.schema.Structure;
49  import org.exolab.castor.xml.schema.XMLType;
50  
51  /**
52   * A class for unmarshalling restriction elements of a simpleType
53   * 
54   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
55   * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
56   **/
57  public class SimpleTypeRestrictionUnmarshaller extends ComponentReader {
58  
59  
60    // --------------------/
61    // - Member Variables -/
62    // --------------------/
63  
64    /**
65     * The current ComponentReader
66     **/
67    private ComponentReader unmarshaller;
68  
69    /**
70     * The current branch depth
71     **/
72    private int depth = 0;
73  
74    /**
75     * The simpleType we are unmarshalling
76     **/
77    private SimpleTypeDefinition _typeDefinition = null;
78  
79    private Schema _schema = null;
80    private boolean foundAnnotation = false;
81    private boolean foundSimpleType = false;
82    private boolean foundFacets = false;
83  
84    /**
85     * The base simpleType (ie the one we are restricting)
86     **/
87    // private SimpleType _baseType = null;
88  
89    // ----------------/
90    // - Constructors -/
91    // ----------------/
92  
93    /**
94     * Creates a new RestrictionUnmarshaller
95     * 
96     * @param schemaContext the {@link SchemaContext} to get some configuration settings from
97     * @param typeDefinition the SimpleType being unmarshalled
98     * @param atts the AttributeList
99     **/
100   public SimpleTypeRestrictionUnmarshaller(final SchemaContext schemaContext,
101       final SimpleTypeDefinition typeDefinition, final AttributeSet atts) throws XMLException {
102     super(schemaContext);
103 
104     _typeDefinition = typeDefinition;
105     _schema = typeDefinition.getSchema();
106 
107     // -- base
108     String base = atts.getValue(SchemaNames.BASE_ATTR);
109     if ((base != null) && (base.length() > 0)) {
110 
111       XMLType baseType = _schema.getType(base);
112       if (baseType == null)
113         _typeDefinition.setBaseTypeName(base);
114       else if (baseType.getStructureType() == Structure.COMPLEX_TYPE) {
115         String err = "The base type of a simpleType cannot " + "be a complexType.";
116         throw new IllegalStateException(err);
117       } else
118         _typeDefinition.setBaseType((SimpleType) baseType);
119     }
120 
121 
122   } // -- RestrictionUnmarshaller
123 
124   // -----------/
125   // - Methods -/
126   // -----------/
127 
128   /**
129    * Returns the name of the element that this ComponentReader handles
130    * 
131    * @return the name of the element that this ComponentReader handles
132    **/
133   public String elementName() {
134     return SchemaNames.RESTRICTION;
135   } // -- elementName
136 
137   /**
138    * Returns the Object created by this ComponentReader
139    * 
140    * @return the Object created by this ComponentReader
141    **/
142   public Object getObject() {
143     return null;
144   } // -- getObject
145 
146   /**
147    * Signals the start of an element with the given name.
148    *
149    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
150    *        prefix).
151    * @param namespace the namespace of the element. This may be null. Note: A null namespace is not
152    *        the same as the default namespace unless the default namespace is also null.
153    * @param atts the AttributeSet containing the attributes associated with the element.
154    * @param nsDecls the namespace declarations being declared for this element. This may be null.
155    **/
156   public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
157       throws XMLException {
158     // -- Do delagation if necessary
159     if (unmarshaller != null) {
160       unmarshaller.startElement(name, namespace, atts, nsDecls);
161       ++depth;
162       return;
163     }
164 
165 
166     // -- annotation
167     if (name.equals(SchemaNames.ANNOTATION)) {
168 
169       if (foundFacets || foundSimpleType)
170         error("An annotation must appear as the first child " + "of 'restriction' elements.");
171 
172       if (foundAnnotation)
173         error("Only one (1) annotation may appear as a child of " + "'restriction' elements.");
174 
175       foundAnnotation = true;
176       unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
177     } else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
178       if (foundSimpleType)
179         error("Only one (1) 'simpleType' may appear as a child of " + "'restriction' elements.");
180 
181       if (foundFacets)
182         error("A 'simpleType', as a child of 'restriction' "
183             + "elements, must appear before any facets.");
184 
185       foundSimpleType = true;
186       unmarshaller = new SimpleTypeUnmarshaller(getSchemaContext(), _schema, atts);
187 
188     } else if (FacetUnmarshaller.isFacet(name)) {
189       foundFacets = true;
190       unmarshaller = new FacetUnmarshaller(getSchemaContext(), name, atts);
191     } else
192       illegalElement(name);
193 
194     unmarshaller.setDocumentLocator(getDocumentLocator());
195   } // -- startElement
196 
197   /**
198    * Signals to end of the element with the given name.
199    *
200    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
201    *        prefix).
202    * @param namespace the namespace of the element.
203    **/
204   public void endElement(String name, String namespace) throws XMLException {
205 
206     // -- Do delagation if necessary
207     if ((unmarshaller != null) && (depth > 0)) {
208       unmarshaller.endElement(name, namespace);
209       --depth;
210       return;
211     }
212 
213     // -- have unmarshaller perform any necessary clean up
214     unmarshaller.finish();
215 
216     // -- annotation
217     if (SchemaNames.ANNOTATION.equals(name)) {
218       Annotation ann = ((AnnotationUnmarshaller) unmarshaller).getAnnotation();
219       _typeDefinition.setAnnotation(ann);
220     } else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
221       SimpleType type = (SimpleType) unmarshaller.getObject();
222       _typeDefinition.setBaseType(type);
223     } else {
224       Facet facet = (Facet) unmarshaller.getObject();
225       facet.setOwningType((SimpleType) _typeDefinition.getBaseType());
226       _typeDefinition.addFacet(facet);
227     }
228 
229 
230     unmarshaller = null;
231   } // -- endElement
232 
233   public void characters(char[] ch, int start, int length) throws XMLException {
234     // -- Do delagation if necessary
235     if (unmarshaller != null) {
236       unmarshaller.characters(ch, start, length);
237     }
238   } // -- characters
239 
240 } // -- SimpleTypeRestrictionUnmarshaller