1 /**
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * $Id$
44 */
45
46 package org.exolab.castor.xml.schema.reader;
47
48 import org.exolab.castor.xml.AttributeSet;
49 import org.exolab.castor.xml.Namespaces;
50 import org.exolab.castor.xml.XMLException;
51 import org.exolab.castor.xml.schema.Annotation;
52 import org.exolab.castor.xml.schema.AppInfo;
53 import org.exolab.castor.xml.schema.Documentation;
54 import org.exolab.castor.xml.schema.SchemaContext;
55 import org.exolab.castor.xml.schema.Schema;
56 import org.exolab.castor.xml.schema.SchemaNames;
57
58 /**
59 * A class for Unmarshalling Annotations
60 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
61 * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
62 **/
63 public class AnnotationUnmarshaller extends ComponentReader {
64
65
66 //--------------------/
67 //- Member Variables -/
68 //--------------------/
69
70 /**
71 * The current ComponentReader
72 **/
73 private ComponentReader unmarshaller;
74
75 /**
76 * The current branch depth
77 **/
78 private int depth = 0;
79
80 /**
81 * The Annotation we are constructing
82 **/
83 private Annotation _annotation = null;
84
85 //----------------/
86 //- Constructors -/
87 //----------------/
88
89 /**
90 * Creates a new AnnotationUnmarshaller.
91 * @param schemaContext the XMLContext to get some configuration settings from
92 * @param atts the AttributeList
93 **/
94 public AnnotationUnmarshaller (final SchemaContext schemaContext, final AttributeSet atts)
95 throws XMLException
96 {
97 super(schemaContext);
98
99 _annotation = new Annotation();
100
101 // check for invalid declared attributes
102 if ((atts != null) && (atts.getSize() > 0)) {
103 for (int i = 0; i < atts.getSize(); i++) {
104 String namespace = atts.getNamespace(i);
105 // according to the XML schema specification, it must be possible
106 // to define any attributes with a non-schema namespace
107 if (namespace.equals(Schema.DEFAULT_SCHEMA_NS)) {
108 illegalAttribute(atts.getName(i));
109 }
110 }
111 }
112
113 } //-- AnnotationUnmarshaller
114
115 //-----------/
116 //- Methods -/
117 //-----------/
118
119 /**
120 * Returns the name of the element that this ComponentReader
121 * handles
122 * @return the name of the element that this ComponentReader
123 * handles
124 **/
125 public String elementName() {
126 return SchemaNames.ANNOTATION;
127 } //-- elementName
128
129 /**
130 *
131 **/
132 public Annotation getAnnotation() {
133 return _annotation;
134 } //-- getAnnotation
135
136 /**
137 * Returns the Object created by this Unmarshaller
138 * @return the object created by this Unmarshaller
139 **/
140 public Object getObject() {
141 return getAnnotation();
142 } //-- getObject
143
144 /**
145 * Signals the start of an element with the given name.
146 *
147 * @param name the NCName of the element. It is an error
148 * if the name is a QName (ie. contains a prefix).
149 * @param namespace the namespace of the element. This may be null.
150 * Note: A null namespace is not the same as the default namespace unless
151 * the default namespace is also null.
152 * @param atts the AttributeSet containing the attributes associated
153 * with the element.
154 * @param nsDecls the namespace declarations being declared for this
155 * element. This may be null.
156 **/
157 public void startElement(String name, String namespace, AttributeSet atts,
158 Namespaces nsDecls)
159 throws XMLException
160 {
161 //-- Do delagation if necessary
162 if (unmarshaller != null) {
163 unmarshaller.startElement(name, namespace, atts, nsDecls);
164 ++depth;
165 return;
166 }
167
168 if (SchemaNames.APPINFO.equals(name)) {
169 unmarshaller = new AppInfoUnmarshaller(getSchemaContext(), atts);
170 }
171 else if (SchemaNames.DOCUMENTATION.equals(name)) {
172 unmarshaller = new DocumentationUnmarshaller(getSchemaContext(), atts);
173 }
174 else illegalElement(name);
175
176 } //-- startElement
177
178 /**
179 * Signals to end of the element with the given name.
180 *
181 * @param name the NCName of the element. It is an error
182 * if the name is a QName (ie. contains a prefix).
183 * @param namespace the namespace of the element.
184 **/
185 public void endElement(String name, String namespace)
186 throws XMLException
187 {
188
189 //-- Do delagation if necessary
190 if ((unmarshaller != null) && (depth > 0)) {
191 unmarshaller.endElement(name, namespace);
192 --depth;
193 return;
194 }
195
196 //-- have unmarshaller perform any necessary clean up
197 unmarshaller.finish();
198
199 //-- appinfo
200 if (SchemaNames.APPINFO.equals(name)) {
201 _annotation.addAppInfo( (AppInfo)unmarshaller.getObject());
202 }
203 //-- info
204 else {
205 _annotation.addDocumentation( (Documentation)unmarshaller.getObject());
206 }
207 unmarshaller = null;
208 } //-- endElement
209
210 public void characters(char[] ch, int start, int length)
211 throws XMLException
212 {
213 //-- Do delagation if necessary
214 if (unmarshaller != null) {
215 unmarshaller.characters(ch, start, length);
216 }
217
218 } //-- characters
219
220 } //-- ArchetypeUnmarshaller