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