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: FieldOrSelectorUnmarshaller.java 5951 2006-05-30 22:18:48Z bsnyder $
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.Annotated;
051: import org.exolab.castor.xml.schema.Annotation;
052: import org.exolab.castor.xml.schema.IdentitySelector;
053: import org.exolab.castor.xml.schema.IdentityField;
054: import org.exolab.castor.xml.schema.SchemaNames;
055:
056: /**
057: * A class for Unmarshalling Selector or Field elements for
058: * identity-constraints
059: *
060: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
061: * @version $Revision: 5951 $ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
062: **/
063: public class FieldOrSelectorUnmarshaller extends ComponentReader {
064:
065: //--------------------/
066: //- Member Variables -/
067: //--------------------/
068:
069: /**
070: * The current ComponentReader
071: **/
072: private ComponentReader _unmarshaller;
073:
074: /**
075: * The current branch depth
076: **/
077: private int _depth = 0;
078:
079: /**
080: * The Field or Selector we are unmarshalling
081: **/
082: private Annotated _fieldOrSelector = null;
083:
084: private boolean _foundAnnotation = false;
085:
086: private String _elementName = null;
087:
088: //----------------/
089: //- Constructors -/
090: //----------------/
091:
092: /**
093: * Creates a new FieldOrSelectorUnmarshaller
094: *
095: * @param elementName the name of the element being unmarshalled.
096: * @param atts the AttributeList.
097: **/
098: public FieldOrSelectorUnmarshaller(String elementName,
099: AttributeSet atts) throws XMLException {
100: super ();
101:
102: _elementName = elementName;
103:
104: String xpath = atts.getValue(SchemaNames.XPATH_ATTR);
105: if (xpath == null) {
106: error("The 'xpath' attribute for '" + _elementName
107: + "' must exist.");
108: }
109:
110: String id = atts.getValue(SchemaNames.ID_ATTR);
111:
112: //-- selector
113: if (SchemaNames.SELECTOR.equals(elementName)) {
114: _fieldOrSelector = new IdentitySelector(xpath);
115: if (id != null) {
116: ((IdentitySelector) _fieldOrSelector).setId(id);
117: }
118: }
119: //-- field
120: else {
121: _fieldOrSelector = new IdentityField(xpath);
122: if (id != null) {
123: ((IdentityField) _fieldOrSelector).setId(id);
124: }
125: }
126:
127: } //-- FieldOrSelectorUnmarshaller
128:
129: //-----------/
130: //- Methods -/
131: //-----------/
132:
133: /**
134: * Returns the name of the element that this ComponentReader
135: * handles
136: * @return the name of the element that this ComponentReader
137: * handles
138: **/
139: public String elementName() {
140: return _elementName;
141: } //-- elementName
142:
143: /**
144: * Returns the Object created by this ComponentReader
145: *
146: * @return the Object created by this ComponentReader
147: **/
148: public Object getObject() {
149: return _fieldOrSelector;
150: } //-- getObject
151:
152: public void finish() throws XMLException {
153: //-- do nothing
154: } //-- finish
155:
156: /**
157: * Signals the start of an element with the given name.
158: *
159: * @param name the NCName of the element. It is an error
160: * if the name is a QName (ie. contains a prefix).
161: * @param namespace the namespace of the element. This may be null.
162: * Note: A null namespace is not the same as the default namespace unless
163: * the default namespace is also null.
164: * @param atts the AttributeSet containing the attributes associated
165: * with the element.
166: * @param nsDecls the namespace declarations being declared for this
167: * element. This may be null.
168: **/
169: public void startElement(String name, String namespace,
170: AttributeSet atts, Namespaces nsDecls) throws XMLException {
171: //-- Do delagation if necessary
172: if (_unmarshaller != null) {
173: _unmarshaller.startElement(name, namespace, atts, nsDecls);
174: ++_depth;
175: return;
176: }
177:
178: if (SchemaNames.ANNOTATION.equals(name)) {
179:
180: if (_foundAnnotation)
181: error("Only one (1) annotation may appear as a child of '"
182: + _elementName + "'.");
183: _foundAnnotation = true;
184: _unmarshaller = new AnnotationUnmarshaller(atts);
185: } else
186: illegalElement(name);
187:
188: } //-- startElement
189:
190: /**
191: * Signals to end of the element with the given name.
192: *
193: * @param name the NCName of the element. It is an error
194: * if the name is a QName (ie. contains a prefix).
195: * @param namespace the namespace of the element.
196: **/
197: public void endElement(String name, String namespace)
198: throws XMLException {
199:
200: //-- Do delagation if necessary
201: if ((_unmarshaller != null) && (_depth > 0)) {
202: _unmarshaller.endElement(name, namespace);
203: --_depth;
204: return;
205: }
206:
207: //-- have unmarshaller perform any necessary clean up
208: _unmarshaller.finish();
209:
210: if (SchemaNames.ANNOTATION.equals(name)) {
211: Annotation annotation = (Annotation) _unmarshaller
212: .getObject();
213: _fieldOrSelector.addAnnotation(annotation);
214: }
215:
216: _unmarshaller = null;
217: } //-- endElement
218:
219: public void characters(char[] ch, int start, int length)
220: throws XMLException {
221: //-- Do delagation if necessary
222: if (_unmarshaller != null) {
223: _unmarshaller.characters(ch, start, length);
224: }
225: } //-- characters
226:
227: } //-- FieldOrSelectorUnmarshaller
|