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