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