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-2003 (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.AttributeDecl;
43  import org.exolab.castor.xml.schema.Form;
44  import org.exolab.castor.xml.schema.SchemaContext;
45  import org.exolab.castor.xml.schema.Schema;
46  import org.exolab.castor.xml.schema.SchemaNames;
47  import org.exolab.castor.xml.schema.SimpleType;
48  
49  /**
50   * The Unmarshaller for Attribute declarations
51   *
52   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
53   * @version $Revision$ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
54   **/
55  public class AttributeUnmarshaller extends ComponentReader {
56  
57  
58    // --------------------/
59    // - Member Variables -/
60    // --------------------/
61  
62    /**
63     * The current ComponentReader
64     **/
65    private ComponentReader unmarshaller;
66  
67    /**
68     * The current branch depth
69     **/
70    private int depth = 0;
71  
72    /**
73     * The Attribute reference for the Attribute we are constructing
74     **/
75    private AttributeDecl _attribute = null;
76  
77    private Schema _schema = null;
78  
79    private boolean foundAnnotation = false;
80    private boolean foundSimpleType = false;
81  
82  
83    // ----------------/
84    // - Constructors -/
85    // ----------------/
86  
87    public AttributeUnmarshaller(final SchemaContext schemaContext, final Schema schema,
88        final AttributeSet atts) {
89      super(schemaContext);
90      this._schema = schema;
91  
92      _attribute = new AttributeDecl(schema);
93  
94      // --@ref
95      String attValue = atts.getValue(SchemaNames.REF_ATTR);
96      if (attValue != null) {
97        _attribute.setReference(attValue);
98      }
99  
100     // -- @name
101     attValue = atts.getValue(SchemaNames.NAME_ATTR);
102     if (attValue != null) {
103       if (_attribute.isReference()) {
104         String err =
105             "An attribute cannot have a 'name' attribute and a 'ref' attribute at the same time.";
106         throw new IllegalStateException(err);
107       }
108       _attribute.setName(attValue);
109     }
110 
111     // -- @default
112     attValue = atts.getValue(SchemaNames.DEFAULT_ATTR);
113     if (attValue != null) {
114       _attribute.setDefaultValue(attValue);
115     }
116 
117     // -- @id
118     _attribute.setId(atts.getValue(SchemaNames.ID_ATTR));
119 
120     // -- @fixed
121     attValue = atts.getValue(SchemaNames.FIXED_ATTR);
122     if (attValue != null) {
123       _attribute.setFixedValue(attValue);
124     }
125 
126     // -- @form
127     attValue = atts.getValue(SchemaNames.FORM);
128     if (attValue != null) {
129       if (_attribute.isReference()) {
130         String err = "An attribute reference cannot have a 'form' attribute.";
131         throw new IllegalArgumentException(err);
132       }
133       _attribute.setForm(Form.valueOf(attValue));
134     }
135 
136     // -- @type
137     attValue = atts.getValue(SchemaNames.TYPE_ATTR);
138     if (attValue != null) {
139       if (_attribute.isReference()) {
140         String err = "An attribute reference cannot have a 'type' attribute.";
141         throw new IllegalArgumentException(err);
142       }
143       _attribute.setSimpleTypeReference(attValue);
144     }
145 
146     // -- @use
147     attValue = atts.getValue(SchemaNames.USE_ATTR);
148     if (attValue != null) {
149       if (_attribute.isDefault() && (!attValue.equals(AttributeDecl.USE_OPTIONAL)))
150         throw new IllegalArgumentException(
151             "When 'default' is present, the 'use' attribute must have the value 'optional'.");
152       _attribute.setUse(attValue);
153     }
154 
155   } // -- AttributeUnmarshaller
156 
157   // -----------/
158   // - Methods -/
159   // -----------/
160 
161 
162   /**
163    * Returns the name of the element that this ComponentReader handles
164    * 
165    * @return the name of the element that this ComponentReader handles
166    **/
167   public String elementName() {
168     return SchemaNames.ATTRIBUTE;
169   } // -- elementName
170 
171   /**
172    *
173   **/
174   public AttributeDecl getAttribute() {
175     return _attribute;
176   } // -- getAttribute
177 
178   /**
179    * Returns the Object created by this ComponentReader
180    * 
181    * @return the Object created by this ComponentReader
182    **/
183   public Object getObject() {
184     return getAttribute();
185   } // -- getObject
186 
187   /**
188    * Signals the start of an element with the given name.
189    *
190    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
191    *        prefix).
192    * @param namespace the namespace of the element. This may be null. Note: A null namespace is not
193    *        the same as the default namespace unless the default namespace is also null.
194    * @param atts the AttributeSet containing the attributes associated with the element.
195    * @param nsDecls the namespace declarations being declared for this element. This may be null.
196    **/
197   public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
198       throws XMLException {
199     // -- Do delagation if necessary
200     if (unmarshaller != null) {
201       unmarshaller.startElement(name, namespace, atts, nsDecls);
202       ++depth;
203       return;
204     }
205 
206     if (SchemaNames.ANNOTATION.equals(name)) {
207 
208       if (foundAnnotation)
209         error("Only one (1) annotation is allowed as a child of " + "an attribute declaration.");
210 
211       if (foundSimpleType)
212         error("An annotation may only appear as the first child of " + "an attribute declaration.");
213 
214       foundAnnotation = true;
215       unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
216     } else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
217       if (foundSimpleType)
218         error("Only one (1) simpleType is allowed as a child of " + "an attribute declaration.");
219 
220       foundSimpleType = true;
221       unmarshaller = new SimpleTypeUnmarshaller(getSchemaContext(), _schema, atts);
222     } else {
223       illegalElement(name);
224     }
225 
226   } // -- startElement
227 
228   /**
229    * Signals to end of the element with the given name.
230    *
231    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
232    *        prefix).
233    * @param namespace the namespace of the element.
234    **/
235   public void endElement(String name, String namespace) throws XMLException {
236 
237     // -- Do delagation if necessary
238     if ((unmarshaller != null) && (depth > 0)) {
239       unmarshaller.endElement(name, namespace);
240       --depth;
241       return;
242     }
243 
244     // -- call unmarshaller finish to perform any necessary cleanup
245     unmarshaller.finish();
246 
247     if (SchemaNames.ANNOTATION.equals(name)) {
248       Annotation ann = (Annotation) unmarshaller.getObject();
249       _attribute.addAnnotation(ann);
250     } else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
251       SimpleType simpleType = ((SimpleTypeUnmarshaller) unmarshaller).getSimpleType();
252       _attribute.setSimpleType(simpleType);
253     }
254 
255     unmarshaller = null;
256 
257   } // -- endElement
258 
259   public void characters(char[] ch, int start, int length) throws XMLException {
260     // -- Do delagation if necessary
261     if (unmarshaller != null) {
262       unmarshaller.characters(ch, start, length);
263     }
264   } // -- characters
265 
266 } // -- AttributeUnmarshaller