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 1999-2003 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  package org.exolab.castor.xml.schema.reader;
37  
38  import org.exolab.castor.xml.AttributeSet;
39  import org.exolab.castor.xml.Namespaces;
40  import org.exolab.castor.xml.XMLException;
41  import org.exolab.castor.xml.schema.Documentation;
42  import org.exolab.castor.xml.schema.SchemaContext;
43  import org.exolab.castor.xml.schema.SchemaNames;
44  import org.exolab.castor.types.AnyNode;
45  
46  import java.util.Enumeration;
47  import java.util.Stack;
48  
49  /**
50   * A class for Unmarshalling XML Schema <documentation> elements
51   * 
52   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
53   * @version $Revision$ $Date: 2004-10-01 07:25:46 -0600 (Fri, 01 Oct 2004) $
54   **/
55  public class DocumentationUnmarshaller extends ComponentReader {
56  
57  
58    // --------------------/
59    // - Member Variables -/
60    // --------------------/
61  
62    /**
63     * The Attribute reference for the Attribute we are constructing
64     */
65    private final Documentation _documentation = new Documentation();
66  
67    /**
68     * Stack of AnyNodes being unmarshalled
69     */
70    private final Stack<AnyNode> _nodes = new Stack<>();
71  
72    // ----------------/
73    // - Constructors -/
74    // ----------------/
75  
76    /**
77     * Creates a new DocumentationUnmarshaller.
78     * 
79     * @param schemaContext the schema context to get some configuration settings from
80     * @param atts the AttributeList
81     **/
82    public DocumentationUnmarshaller(final SchemaContext schemaContext, final AttributeSet atts) {
83      super(schemaContext);
84  
85      // -- @source
86      _documentation.setSource(atts.getValue(SchemaNames.SOURCE_ATTR));
87  
88    } // -- DocumentationUnmarshaller
89  
90    // -----------/
91    // - Methods -/
92    // -----------/
93  
94    /**
95     * Returns the name of the element that this ComponentReader handles
96     * 
97     * @return the name of the element that this ComponentReader handles
98     **/
99    public String elementName() {
100     return SchemaNames.DOCUMENTATION;
101   } // -- elementName
102 
103   /**
104    * Called to signal an end of unmarshalling. This method should be overridden to perform any
105    * necessary clean up by an unmarshaller
106    **/
107   public void finish() {
108     // -- do nothing
109   } // -- finish
110 
111   /**
112    *
113   **/
114   public Documentation getDocumentation() {
115     return _documentation;
116   } // -- getDocumentation
117 
118   /**
119    * Returns the Object created by this ComponentReader
120    * 
121    * @return the Object created by this ComponentReader
122    **/
123   public Object getObject() {
124     return getDocumentation();
125   } // -- getObject
126 
127   /**
128    * Signals the start of an element with the given name.
129    *
130    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
131    *        prefix).
132    * @param namespace the namespace of the element. This may be null. Note: A null namespace is not
133    *        the same as the default namespace unless the default namespace is also null.
134    * @param atts the AttributeSet containing the attributes associated with the element.
135    * @param nsDecls the namespace declarations being declared for this element. This may be null.
136    **/
137   public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
138       throws XMLException {
139 
140     String prefix = null;
141     if (nsDecls != null) {
142       // -- find prefix (elements use default namespace if null)
143       if (namespace == null)
144         namespace = "";
145       prefix = nsDecls.getNamespacePrefix(namespace);
146     }
147 
148     AnyNode node = new AnyNode(AnyNode.ELEMENT, name, prefix, namespace, null);
149     _nodes.push(node);
150 
151     // -- process namespace nodes
152     if (nsDecls != null) {
153       Enumeration enumeration = nsDecls.getLocalNamespaces();
154       while (enumeration.hasMoreElements()) {
155         namespace = (String) enumeration.nextElement();
156         prefix = nsDecls.getNamespacePrefix(namespace);
157         node.addNamespace(new AnyNode(AnyNode.NAMESPACE, null, // -- no local name for a ns decl.
158             prefix, namespace, null)); // -- no value
159       }
160     }
161     // -- process attributes
162     if (atts != null) {
163       for (int i = 0; i < atts.getSize(); i++) {
164         namespace = atts.getNamespace(i);
165         if ((nsDecls != null) && (namespace != null)) {
166           prefix = nsDecls.getNamespacePrefix(namespace);
167         } else
168           prefix = null;
169         node.addAttribute(
170             new AnyNode(AnyNode.ATTRIBUTE, atts.getName(i), prefix, namespace, atts.getValue(i)));
171       }
172     }
173 
174   } // -- startElement
175 
176   /**
177    * Signals to end of the element with the given name.
178    *
179    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
180    *        prefix).
181    * @param namespace the namespace of the element.
182    **/
183   public void endElement(String name, String namespace) throws XMLException {
184     AnyNode node = (AnyNode) _nodes.pop();
185     if (_nodes.isEmpty()) {
186       // -- add to appInfo
187       _documentation.add(node);
188     } else {
189       // -- add to parent AnyNode
190       ((AnyNode) _nodes.peek()).addChild(node);
191     }
192   } // -- endElement
193 
194   public void characters(char[] ch, int start, int length) throws XMLException {
195     // -- Do delagation if necessary
196     AnyNode text = new AnyNode(AnyNode.TEXT, null, // -- no local name for text nodes
197         null, // -- no prefix
198         null, // -- no namespace
199         new String(ch, start, length));
200 
201     if (!_nodes.isEmpty()) {
202       AnyNode parent = (AnyNode) _nodes.peek();
203       parent.addChild(text);
204     } else {
205       _documentation.add(text);
206     }
207 
208   } // -- characters
209 
210 } // -- DocumentationUnmarshaller
211