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 //-- imported classes and packages
49 import org.exolab.castor.xml.AttributeSet;
50 import org.exolab.castor.xml.Namespaces;
51 import org.exolab.castor.xml.XMLException;
52 import org.exolab.castor.xml.schema.Annotation;
53 import org.exolab.castor.xml.schema.Facet;
54 import org.exolab.castor.xml.schema.FacetFactory;
55 import org.exolab.castor.xml.schema.SchemaContext;
56 import org.exolab.castor.xml.schema.SchemaException;
57 import org.exolab.castor.xml.schema.SchemaNames;
58
59 /**
60 * A class for Unmarshalling facets
61 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
62 * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
63 **/
64 public class FacetUnmarshaller extends ComponentReader {
65
66
67 //--------------------/
68 //- Member Variables -/
69 //--------------------/
70
71 /**
72 * The current ComponentReader.
73 **/
74 private ComponentReader unmarshaller = null;
75
76 /**
77 * The current branch depth
78 **/
79 private int depth = 0;
80
81 /**
82 * The Facet we are constructing
83 **/
84 private Facet _facet = null;
85
86 /**
87 * The element name of the Facet currently being unmarshalled
88 **/
89 private String _elementName = null;
90
91 //----------------/
92 //- Constructors -/
93 //----------------/
94
95 /**
96 * Creates a new FacetUnmarshaller.
97 * @param schemaContext the {@link SchemaContext} to get some configuration settings from
98 * @param name the name of the Facet
99 * @param atts the AttributeList
100 **/
101 public FacetUnmarshaller (
102 final SchemaContext schemaContext,
103 final String name,
104 final AttributeSet atts)
105 throws XMLException {
106 super(schemaContext);
107
108 _elementName = name;
109
110 if (!isFacet(name)) {
111 String err = "'" + name + "' is not a valid or supported facet.";
112 throw new IllegalArgumentException(err);
113 }
114
115 _facet = FacetFactory.getInstance().createFacet(
116 name, atts.getValue(SchemaNames.VALUE_ATTR));
117
118 } //-- FacetUnmarshaller
119
120 //-----------/
121 //- Methods -/
122 //-----------/
123
124 /**
125 * Returns the name of the element that this ComponentReader
126 * handles
127 * @return the name of the element that this ComponentReader
128 * handles
129 **/
130 public String elementName() {
131 return _elementName;
132 } //-- elementName
133
134 /**
135 *
136 **/
137 public Facet getFacet() {
138 return _facet;
139 } //-- getArchetype
140
141 /**
142 * Returns the Object created by this ComponentReader
143 * @return the Object created by this ComponentReader
144 **/
145 public Object getObject() {
146 return getFacet();
147 } //-- getObject
148
149 /**
150 * Signals the start of an element with the given name.
151 *
152 * @param name the NCName of the element. It is an error
153 * if the name is a QName (ie. contains a prefix).
154 * @param namespace the namespace of the element. This may be null.
155 * Note: A null namespace is not the same as the default namespace unless
156 * the default namespace is also null.
157 * @param atts the AttributeSet containing the attributes associated
158 * with the element.
159 * @param nsDecls the namespace declarations being declared for this
160 * element. This may be null.
161 **/
162 public void startElement(String name, String namespace, AttributeSet atts,
163 Namespaces nsDecls)
164 throws XMLException
165 {
166 //-- Do delagation if necessary
167 if (unmarshaller != null) {
168 unmarshaller.startElement(name, namespace, atts, nsDecls);
169 ++depth;
170 return;
171 }
172
173 if (SchemaNames.ANNOTATION.equals(name)) {
174 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
175 }
176 else illegalElement(name);
177
178 } //-- startElement
179
180 /**
181 * Signals to end of the element with the given name.
182 *
183 * @param name the NCName of the element. It is an error
184 * if the name is a QName (ie. contains a prefix).
185 * @param namespace the namespace of the element.
186 **/
187 public void endElement(String name, String namespace)
188 throws XMLException
189 {
190 //-- Do delagation if necessary
191 if ((unmarshaller != null) && (depth > 0)) {
192 unmarshaller.endElement(name, namespace);
193 --depth;
194 return;
195 }
196
197 if (unmarshaller == null)
198 throw new SchemaException("missing start element: " + name);
199 else if (SchemaNames.ANNOTATION.equals(name)) {
200 Annotation annotation = (Annotation)unmarshaller.getObject();
201 _facet.addAnnotation(annotation);
202 }
203
204 } //-- endElement
205
206 public void characters(char[] ch, int start, int length)
207 throws XMLException
208 {
209 //-- Do delagation if necessary
210 if (unmarshaller != null) {
211 unmarshaller.characters(ch, start, length);
212 }
213 } //-- characters
214
215 protected static boolean isFacet(String name) {
216
217 if (Facet.ENUMERATION.equals(name)) return true;
218 if (Facet.LENGTH.equals(name)) return true;
219 if (Facet.PATTERN.equals(name)) return true;
220 if (Facet.MAX_EXCLUSIVE.equals(name)) return true;
221 if (Facet.MIN_EXCLUSIVE.equals(name)) return true;
222 if (Facet.MAX_INCLUSIVE.equals(name)) return true;
223 if (Facet.MIN_INCLUSIVE.equals(name)) return true;
224 if (Facet.MAX_LENGTH.equals(name)) return true;
225 if (Facet.MIN_LENGTH.equals(name)) return true;
226 if (Facet.WHITESPACE.equals(name)) return true;
227 if (Facet.TOTALDIGITS.equals(name)) return true;
228 if (Facet.FRACTIONDIGITS.equals(name))return true;
229 return false;
230 } //-- isFacet
231
232 } //-- FacetUnmarshaller