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