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 2001-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.IdentityConstraint;
54 import org.exolab.castor.xml.schema.IdentityField;
55 import org.exolab.castor.xml.schema.IdentitySelector;
56 import org.exolab.castor.xml.schema.Key;
57 import org.exolab.castor.xml.schema.KeyRef;
58 import org.exolab.castor.xml.schema.SchemaContext;
59 import org.exolab.castor.xml.schema.SchemaNames;
60 import org.exolab.castor.xml.schema.Unique;
61
62 /**
63 * A class for Unmarshalling Identity Constraints
64 *
65 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
66 * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
67 **/
68 public class IdentityConstraintUnmarshaller extends ComponentReader {
69
70 //--------------------/
71 //- Member Variables -/
72 //--------------------/
73
74 /**
75 * The current ComponentReader
76 **/
77 private ComponentReader _unmarshaller;
78
79 /**
80 * The current branch depth
81 **/
82 private int _depth = 0;
83
84 /**
85 * The IdentityConstraint we are unmarshalling
86 **/
87 private IdentityConstraint _identityConstraint = null;
88
89 private boolean _foundAnnotation = false;
90 private boolean _foundSelector = false;
91 private boolean _foundField = false;
92
93 private String _elementName = null;
94
95 //----------------/
96 //- Constructors -/
97 //----------------/
98
99 /**
100 * Creates a new IdentityConstraintUnmarshaller.
101 *
102 * @param schemaContext the {@link SchemaContext} to get some configuration settings from
103 * @param elementName the resolver being used for reference resolving
104 * @param atts the AttributeList
105 **/
106 public IdentityConstraintUnmarshaller(
107 final SchemaContext schemaContext,
108 final String elementName,
109 final AttributeSet atts)
110 throws XMLException {
111 super(schemaContext);
112
113 _elementName = elementName;
114
115 String name = atts.getValue(SchemaNames.NAME_ATTR);
116 if (name == null) {
117 error("The 'name' attribute for an identity-constraint must exist.");
118 }
119
120 atts.getValue(SchemaNames.ID_ATTR);
121
122 //-- keyref
123 if (SchemaNames.KEYREF.equals(elementName)) {
124 String refer = atts.getValue("refer");
125 if (refer == null) {
126 error("The 'refer' attribute for keyref must exist.");
127 }
128 _identityConstraint = new KeyRef(name, refer);
129 }
130 //-- unique
131 else if (SchemaNames.UNIQUE.equals(elementName)) {
132 _identityConstraint = new Unique(name);
133 }
134 //-- key
135 else {
136 _identityConstraint = new Key(name);
137 }
138
139 } //-- IdentityConstraintUnmarshaller
140
141 //-----------/
142 //- Methods -/
143 //-----------/
144
145 /**
146 * Returns the name of the element that this ComponentReader
147 * handles
148 * @return the name of the element that this ComponentReader
149 * handles
150 **/
151 public String elementName() {
152 return _elementName;
153 } //-- elementName
154
155 /**
156 * Returns the IdentityConstraint created.
157 *
158 * @return the IdentityConstraint created.
159 **/
160 public IdentityConstraint getIdentityConstraint() {
161 return _identityConstraint;
162 } //-- getIdentityConstraint
163
164 /**
165 * Returns the Object created by this ComponentReader
166 *
167 * @return the Object created by this ComponentReader
168 **/
169 public Object getObject() {
170 return getIdentityConstraint();
171 } //-- getObject
172
173 public void finish() throws XMLException {
174 if (!_foundSelector) {
175 error ("Invalid " + _elementName + "; missing 'selector'.");
176 }
177 else if (!_foundField) {
178 error ("Invalid " + _elementName + "; missing 'field'.");
179 }
180 } //-- finish
181
182 /**
183 * Signals the start of an element with the given name.
184 *
185 * @param name the NCName of the element. It is an error
186 * if the name is a QName (ie. contains a prefix).
187 * @param namespace the namespace of the element. This may be null.
188 * Note: A null namespace is not the same as the default namespace unless
189 * the default namespace is also null.
190 * @param atts the AttributeSet containing the attributes associated
191 * with the element.
192 * @param nsDecls the namespace declarations being declared for this
193 * element. This may be null.
194 **/
195 public void startElement(String name, String namespace, AttributeSet atts,
196 Namespaces nsDecls)
197 throws XMLException
198 {
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 may appear as a child of '" +
210 _elementName + "'.");
211
212 if (_foundSelector || _foundField)
213 error("An annotation may only appear as the first child of '"+
214 _elementName + "'.");
215
216 _foundAnnotation = true;
217 _unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
218 }
219 else if (SchemaNames.SELECTOR.equals(name)) {
220
221 if (_foundField) {
222 String err = "The 'selector' element of '" + _elementName +
223 "' must appear before any 'field' elements.";
224 error(err);
225 }
226 if (_foundSelector)
227 error("Only one (1) 'selector' may appear as a child of '" +
228 _elementName + "'.");
229
230 _foundSelector = true;
231
232 _unmarshaller = new FieldOrSelectorUnmarshaller(getSchemaContext(), name, atts);
233 }
234 else if (SchemaNames.FIELD.equals(name)) {
235 _foundField = true;
236 _unmarshaller = new FieldOrSelectorUnmarshaller(getSchemaContext(), name, atts);
237 }
238 else illegalElement(name);
239
240 } //-- startElement
241
242 /**
243 * Signals to end of the element with the given name.
244 *
245 * @param name the NCName of the element. It is an error
246 * if the name is a QName (ie. contains a prefix).
247 * @param namespace the namespace of the element.
248 **/
249 public void endElement(String name, String namespace)
250 throws XMLException
251 {
252
253 //-- Do delagation if necessary
254 if ((_unmarshaller != null) && (_depth > 0)) {
255 _unmarshaller.endElement(name, namespace);
256 --_depth;
257 return;
258 }
259
260 //-- have unmarshaller perform any necessary clean up
261 _unmarshaller.finish();
262
263 if (SchemaNames.ANNOTATION.equals(name)) {
264 Annotation annotation = (Annotation)_unmarshaller.getObject();
265 _identityConstraint.addAnnotation(annotation);
266 }
267 else if (SchemaNames.SELECTOR.equals(name)) {
268 IdentitySelector selector
269 = (IdentitySelector)_unmarshaller.getObject();
270 _identityConstraint.setSelector(selector);
271 }
272 else if (SchemaNames.FIELD.equals(name)) {
273 IdentityField field
274 = (IdentityField)_unmarshaller.getObject();
275 _identityConstraint.addField(field);
276 }
277
278
279 _unmarshaller = null;
280 } //-- endElement
281
282 public void characters(char[] ch, int start, int length)
283 throws XMLException
284 {
285 //-- Do delagation if necessary
286 if (_unmarshaller != null) {
287 _unmarshaller.characters(ch, start, length);
288 }
289 } //-- characters
290
291 } //-- IdentityConstraintUnmarshaller