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.AppInfo;
43  import org.exolab.castor.xml.schema.Documentation;
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  
48  /**
49   * A class for Unmarshalling Annotations
50   * 
51   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
52   * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
53   **/
54  public class AnnotationUnmarshaller extends ComponentReader {
55  
56  
57    // --------------------/
58    // - Member Variables -/
59    // --------------------/
60  
61    /**
62     * The current ComponentReader
63     **/
64    private ComponentReader unmarshaller;
65  
66    /**
67     * The current branch depth
68     **/
69    private int depth = 0;
70  
71    /**
72     * The Annotation we are constructing
73     **/
74    private Annotation _annotation = null;
75  
76    // ----------------/
77    // - Constructors -/
78    // ----------------/
79  
80    /**
81     * Creates a new AnnotationUnmarshaller.
82     * 
83     * @param schemaContext the XMLContext to get some configuration settings from
84     * @param atts the AttributeList
85     **/
86    public AnnotationUnmarshaller(final SchemaContext schemaContext, final AttributeSet atts)
87        throws XMLException {
88      super(schemaContext);
89  
90      _annotation = new Annotation();
91  
92      // check for invalid declared attributes
93      if ((atts != null) && (atts.getSize() > 0)) {
94        for (int i = 0; i < atts.getSize(); i++) {
95          String namespace = atts.getNamespace(i);
96          // according to the XML schema specification, it must be possible
97          // to define any attributes with a non-schema namespace
98          if (namespace.equals(Schema.DEFAULT_SCHEMA_NS)) {
99            illegalAttribute(atts.getName(i));
100         }
101       }
102     }
103 
104   } // -- AnnotationUnmarshaller
105 
106   // -----------/
107   // - Methods -/
108   // -----------/
109 
110   /**
111    * Returns the name of the element that this ComponentReader handles
112    * 
113    * @return the name of the element that this ComponentReader handles
114    **/
115   public String elementName() {
116     return SchemaNames.ANNOTATION;
117   } // -- elementName
118 
119   /**
120    * 
121   **/
122   public Annotation getAnnotation() {
123     return _annotation;
124   } // -- getAnnotation
125 
126   /**
127    * Returns the Object created by this Unmarshaller
128    * 
129    * @return the object created by this Unmarshaller
130    **/
131   public Object getObject() {
132     return getAnnotation();
133   } // -- getObject
134 
135   /**
136    * Signals the start of an element with the given name.
137    *
138    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
139    *        prefix).
140    * @param namespace the namespace of the element. This may be null. Note: A null namespace is not
141    *        the same as the default namespace unless the default namespace is also null.
142    * @param atts the AttributeSet containing the attributes associated with the element.
143    * @param nsDecls the namespace declarations being declared for this element. This may be null.
144    **/
145   public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
146       throws XMLException {
147     // -- Do delagation if necessary
148     if (unmarshaller != null) {
149       unmarshaller.startElement(name, namespace, atts, nsDecls);
150       ++depth;
151       return;
152     }
153 
154     if (SchemaNames.APPINFO.equals(name)) {
155       unmarshaller = new AppInfoUnmarshaller(getSchemaContext(), atts);
156     } else if (SchemaNames.DOCUMENTATION.equals(name)) {
157       unmarshaller = new DocumentationUnmarshaller(getSchemaContext(), atts);
158     } else
159       illegalElement(name);
160 
161   } // -- startElement
162 
163   /**
164    * Signals to end of the element with the given name.
165    *
166    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
167    *        prefix).
168    * @param namespace the namespace of the element.
169    **/
170   public void endElement(String name, String namespace) throws XMLException {
171 
172     // -- Do delagation if necessary
173     if ((unmarshaller != null) && (depth > 0)) {
174       unmarshaller.endElement(name, namespace);
175       --depth;
176       return;
177     }
178 
179     // -- have unmarshaller perform any necessary clean up
180     unmarshaller.finish();
181 
182     // -- appinfo
183     if (SchemaNames.APPINFO.equals(name)) {
184       _annotation.addAppInfo((AppInfo) unmarshaller.getObject());
185     }
186     // -- info
187     else {
188       _annotation.addDocumentation((Documentation) unmarshaller.getObject());
189     }
190     unmarshaller = null;
191   } // -- endElement
192 
193   public void characters(char[] ch, int start, int length) throws XMLException {
194     // -- Do delagation if necessary
195     if (unmarshaller != null) {
196       unmarshaller.characters(ch, start, length);
197     }
198 
199   } // -- characters
200 
201 } // -- ArchetypeUnmarshaller