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 1999-2003 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: DocumentationUnmarshaller.java 5961 2006-06-03 13:02:04Z rjoachim $
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.Documentation;
051: import org.exolab.castor.xml.schema.SchemaNames;
052: import org.exolab.castor.types.AnyNode;
053:
054: import java.util.Enumeration;
055: import java.util.Stack;
056:
057: /**
058: * A class for Unmarshalling XML Schema <documentation> elements
059: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
060: * @version $Revision: 5961 $ $Date: 2004-10-01 07:25:46 -0600 (Fri, 01 Oct 2004) $
061: **/
062: public class DocumentationUnmarshaller extends ComponentReader {
063:
064: //--------------------/
065: //- Member Variables -/
066: //--------------------/
067:
068: /**
069: * The Attribute reference for the Attribute we are constructing
070: */
071: private Documentation _documentation = null;
072:
073: /**
074: * Stack of AnyNodes being unmarshalled
075: */
076: private Stack _nodes = null;
077:
078: //----------------/
079: //- Constructors -/
080: //----------------/
081:
082: /**
083: * Creates a new DocumentationUnmarshaller
084: * @param atts the AttributeList
085: **/
086: public DocumentationUnmarshaller(AttributeSet atts) {
087: super ();
088:
089: _documentation = new Documentation();
090:
091: //-- @source
092: _documentation
093: .setSource(atts.getValue(SchemaNames.SOURCE_ATTR));
094:
095: _nodes = new Stack();
096:
097: } //-- DocumentationUnmarshaller
098:
099: //-----------/
100: //- Methods -/
101: //-----------/
102:
103: /**
104: * Returns the name of the element that this ComponentReader
105: * handles
106: * @return the name of the element that this ComponentReader
107: * handles
108: **/
109: public String elementName() {
110: return SchemaNames.DOCUMENTATION;
111: } //-- elementName
112:
113: /**
114: * Called to signal an end of unmarshalling. This method should
115: * be overridden to perform any necessary clean up by an unmarshaller
116: **/
117: public void finish() {
118: //-- do nothing
119: } //-- finish
120:
121: /**
122: *
123: **/
124: public Documentation getDocumentation() {
125: return _documentation;
126: } //-- getDocumentation
127:
128: /**
129: * Returns the Object created by this ComponentReader
130: * @return the Object created by this ComponentReader
131: **/
132: public Object getObject() {
133: return getDocumentation();
134: } //-- getObject
135:
136: /**
137: * Signals the start of an element with the given name.
138: *
139: * @param name the NCName of the element. It is an error
140: * if the name is a QName (ie. contains a prefix).
141: * @param namespace the namespace of the element. This may be null.
142: * Note: A null namespace is not the same as the default namespace unless
143: * the default namespace is also null.
144: * @param atts the AttributeSet containing the attributes associated
145: * with the element.
146: * @param nsDecls the namespace declarations being declared for this
147: * element. This may be null.
148: **/
149: public void startElement(String name, String namespace,
150: AttributeSet atts, Namespaces nsDecls) throws XMLException {
151:
152: String prefix = null;
153: if (nsDecls != null) {
154: //-- find prefix (elements use default namespace if null)
155: if (namespace == null)
156: namespace = "";
157: prefix = nsDecls.getNamespacePrefix(namespace);
158: }
159:
160: AnyNode node = new AnyNode(AnyNode.ELEMENT, name, prefix,
161: namespace, null);
162: _nodes.push(node);
163:
164: //-- process namespace nodes
165: if (nsDecls != null) {
166: Enumeration enumeration = nsDecls.getLocalNamespaces();
167: while (enumeration.hasMoreElements()) {
168: namespace = (String) enumeration.nextElement();
169: prefix = nsDecls.getNamespacePrefix(namespace);
170: node.addNamespace(new AnyNode(AnyNode.NAMESPACE, null, //-- no local name for a ns decl.
171: prefix, namespace, null)); //-- no value
172: }
173: }
174: //-- process attributes
175: if (atts != null) {
176: for (int i = 0; i < atts.getSize(); i++) {
177: namespace = atts.getNamespace(i);
178: if ((nsDecls != null) && (namespace != null)) {
179: prefix = nsDecls.getNamespacePrefix(namespace);
180: } else
181: prefix = null;
182: node.addAttribute(new AnyNode(AnyNode.ATTRIBUTE, atts
183: .getName(i), prefix, namespace, atts
184: .getValue(i)));
185: }
186: }
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: AnyNode node = (AnyNode) _nodes.pop();
200: if (_nodes.isEmpty()) {
201: //-- add to appInfo
202: _documentation.add(node);
203: } else {
204: //-- add to parent AnyNode
205: ((AnyNode) _nodes.peek()).addChild(node);
206: }
207: } //-- endElement
208:
209: public void characters(char[] ch, int start, int length)
210: throws XMLException {
211: //-- Do delagation if necessary
212: AnyNode text = new AnyNode(AnyNode.TEXT, null, //-- no local name for text nodes
213: null, //-- no prefix
214: null, //-- no namespace
215: new String(ch, start, length));
216:
217: if (!_nodes.isEmpty()) {
218: AnyNode parent = (AnyNode) _nodes.peek();
219: parent.addChild(text);
220: } else {
221: _documentation.add(text);
222: }
223:
224: } //-- characters
225:
226: } //-- DocumentationUnmarshaller
|