001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 2001-2002 (C) Intalio Inc. All Rights Reserved.
042: *
043: * $Id: IdentityConstraintUnmarshaller.java 6230 2006-09-19 07:56:07Z wguttmn $
044: */package org.exolab.castor.xml.schema.reader;
045:
046: //-- imported classes and packages
047: import org.exolab.castor.xml.AttributeSet;
048: import org.exolab.castor.xml.Namespaces;
049: import org.exolab.castor.xml.XMLException;
050: import org.exolab.castor.xml.schema.Annotation;
051: import org.exolab.castor.xml.schema.IdentityConstraint;
052: import org.exolab.castor.xml.schema.IdentityField;
053: import org.exolab.castor.xml.schema.IdentitySelector;
054: import org.exolab.castor.xml.schema.Key;
055: import org.exolab.castor.xml.schema.KeyRef;
056: import org.exolab.castor.xml.schema.SchemaNames;
057: import org.exolab.castor.xml.schema.Unique;
058:
059: /**
060: * A class for Unmarshalling Identity Constraints
061: *
062: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
063: * @version $Revision: 6230 $ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
064: **/
065: public class IdentityConstraintUnmarshaller extends ComponentReader {
066:
067: //--------------------/
068: //- Member Variables -/
069: //--------------------/
070:
071: /**
072: * The current ComponentReader
073: **/
074: private ComponentReader _unmarshaller;
075:
076: /**
077: * The current branch depth
078: **/
079: private int _depth = 0;
080:
081: /**
082: * The IdentityConstraint we are unmarshalling
083: **/
084: private IdentityConstraint _identityConstraint = null;
085:
086: private boolean _foundAnnotation = false;
087: private boolean _foundSelector = false;
088: private boolean _foundField = false;
089:
090: private String _elementName = null;
091:
092: //----------------/
093: //- Constructors -/
094: //----------------/
095:
096: /**
097: * Creates a new IdentityConstraintUnmarshaller
098: *
099: * @param elementName the resolver being used for reference resolving
100: * @param atts the AttributeList
101: **/
102: public IdentityConstraintUnmarshaller(String elementName,
103: AttributeSet atts) throws XMLException {
104: super ();
105:
106: _elementName = elementName;
107:
108: String name = atts.getValue(SchemaNames.NAME_ATTR);
109: if (name == null) {
110: error("The 'name' attribute for an identity-constraint must exist.");
111: }
112:
113: atts.getValue(SchemaNames.ID_ATTR);
114:
115: //-- keyref
116: if (SchemaNames.KEYREF.equals(elementName)) {
117: String refer = atts.getValue("refer");
118: if (refer == null) {
119: error("The 'refer' attribute for keyref must exist.");
120: }
121: _identityConstraint = new KeyRef(name, refer);
122: }
123: //-- unique
124: else if (SchemaNames.UNIQUE.equals(elementName)) {
125: _identityConstraint = new Unique(name);
126: }
127: //-- key
128: else {
129: _identityConstraint = new Key(name);
130: }
131:
132: } //-- IdentityConstraintUnmarshaller
133:
134: //-----------/
135: //- Methods -/
136: //-----------/
137:
138: /**
139: * Returns the name of the element that this ComponentReader
140: * handles
141: * @return the name of the element that this ComponentReader
142: * handles
143: **/
144: public String elementName() {
145: return _elementName;
146: } //-- elementName
147:
148: /**
149: * Returns the IdentityConstraint created.
150: *
151: * @return the IdentityConstraint created.
152: **/
153: public IdentityConstraint getIdentityConstraint() {
154: return _identityConstraint;
155: } //-- getIdentityConstraint
156:
157: /**
158: * Returns the Object created by this ComponentReader
159: *
160: * @return the Object created by this ComponentReader
161: **/
162: public Object getObject() {
163: return getIdentityConstraint();
164: } //-- getObject
165:
166: public void finish() throws XMLException {
167: if (!_foundSelector) {
168: error("Invalid " + _elementName + "; missing 'selector'.");
169: } else if (!_foundField) {
170: error("Invalid " + _elementName + "; missing 'field'.");
171: }
172: } //-- finish
173:
174: /**
175: * Signals the start of an element with the given name.
176: *
177: * @param name the NCName of the element. It is an error
178: * if the name is a QName (ie. contains a prefix).
179: * @param namespace the namespace of the element. This may be null.
180: * Note: A null namespace is not the same as the default namespace unless
181: * the default namespace is also null.
182: * @param atts the AttributeSet containing the attributes associated
183: * with the element.
184: * @param nsDecls the namespace declarations being declared for this
185: * element. This may be null.
186: **/
187: public void startElement(String name, String namespace,
188: AttributeSet atts, Namespaces nsDecls) throws XMLException {
189: //-- Do delagation if necessary
190: if (_unmarshaller != null) {
191: _unmarshaller.startElement(name, namespace, atts, nsDecls);
192: ++_depth;
193: return;
194: }
195:
196: if (SchemaNames.ANNOTATION.equals(name)) {
197:
198: if (_foundAnnotation)
199: error("Only one (1) annotation may appear as a child of '"
200: + _elementName + "'.");
201:
202: if (_foundSelector || _foundField)
203: error("An annotation may only appear as the first child of '"
204: + _elementName + "'.");
205:
206: _foundAnnotation = true;
207: _unmarshaller = new AnnotationUnmarshaller(atts);
208: } else if (SchemaNames.SELECTOR.equals(name)) {
209:
210: if (_foundField) {
211: String err = "The 'selector' element of '"
212: + _elementName
213: + "' must appear before any 'field' elements.";
214: error(err);
215: }
216: if (_foundSelector)
217: error("Only one (1) 'selector' may appear as a child of '"
218: + _elementName + "'.");
219:
220: _foundSelector = true;
221:
222: _unmarshaller = new FieldOrSelectorUnmarshaller(name, atts);
223: } else if (SchemaNames.FIELD.equals(name)) {
224: _foundField = true;
225: _unmarshaller = new FieldOrSelectorUnmarshaller(name, atts);
226: } else
227: illegalElement(name);
228:
229: } //-- startElement
230:
231: /**
232: * Signals to end of the element with the given name.
233: *
234: * @param name the NCName of the element. It is an error
235: * if the name is a QName (ie. contains a prefix).
236: * @param namespace the namespace of the element.
237: **/
238: public void endElement(String name, String namespace)
239: throws XMLException {
240:
241: //-- Do delagation if necessary
242: if ((_unmarshaller != null) && (_depth > 0)) {
243: _unmarshaller.endElement(name, namespace);
244: --_depth;
245: return;
246: }
247:
248: //-- have unmarshaller perform any necessary clean up
249: _unmarshaller.finish();
250:
251: if (SchemaNames.ANNOTATION.equals(name)) {
252: Annotation annotation = (Annotation) _unmarshaller
253: .getObject();
254: _identityConstraint.addAnnotation(annotation);
255: } else if (SchemaNames.SELECTOR.equals(name)) {
256: IdentitySelector selector = (IdentitySelector) _unmarshaller
257: .getObject();
258: _identityConstraint.setSelector(selector);
259: } else if (SchemaNames.FIELD.equals(name)) {
260: IdentityField field = (IdentityField) _unmarshaller
261: .getObject();
262: _identityConstraint.addField(field);
263: }
264:
265: _unmarshaller = null;
266: } //-- endElement
267:
268: public void characters(char[] ch, int start, int length)
269: throws XMLException {
270: //-- Do delagation if necessary
271: if (_unmarshaller != null) {
272: _unmarshaller.characters(ch, start, length);
273: }
274: } //-- characters
275:
276: } //-- IdentityConstraintUnmarshaller
|