View Javadoc

1   /*-------------------------------------------------------------------------
2    Copyright 2006 Olivier Berlanger
3   
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7   
8    http://www.apache.org/licenses/LICENSE-2.0
9   
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15   -------------------------------------------------------------------------*/
16  package net.sf.xolite.utils;
17  
18  
19  import net.sf.xolite.XMLEventParser;
20  import net.sf.xolite.XMLParseException;
21  import net.sf.xolite.XMLSerializable;
22  import net.sf.xolite.XMLSerializeException;
23  import net.sf.xolite.XMLSerializer;
24  
25  
26  /**
27   * Utility class used when the root of a object tree parsed from XML is not known statically. <br>
28   * This class is used internally by XML event parsers to dynamically instantiate the tree root when there is a factory defined.
29   */
30  public class RootHolder implements XMLSerializable {
31  
32  
33      private XMLSerializable root;
34      private String rootURI;
35      private String rootTag;
36  
37  
38      public XMLSerializable getRoot() {
39          return root;
40      }
41  
42  
43      /**
44       * Instantiate the real root element from the parser factory and delegate parsing to it.
45       * 
46       * @exception NullPointerException
47       *                if the factory is not defined.
48       * @exception XMLParseException
49       *                if the object corresponding to the started element is not defined or cannot be instantiated.
50       */
51      public void startElement(String uri, String localName, XMLEventParser parser) throws XMLParseException {
52          if (root != null) throw new IllegalStateException("Root!=null");
53          rootURI = uri;
54          rootTag = localName;
55          root = parser.parseElement(uri, localName);
56      }
57  
58  
59      /**
60       * Notification of the end tag of the root element.
61       */
62      public void endElement(String uri, String localName, XMLEventParser parser) throws XMLParseException {
63          if ((rootURI != null) && !rootURI.equals(uri)) parser.throwUnexpectedNamespaceException(rootURI);
64          if (!rootTag.equals(localName)) parser.throwUnexpectedElementException(rootTag);
65      }
66  
67  
68      /**
69       * Serialize the inner root object.
70       */
71      public void serialize(XMLSerializer serializer) throws XMLSerializeException {
72          if (root == null) throw new XMLSerializeException("Empty RootHolder cannot be serialized");
73          root.serialize(serializer);
74      }
75  
76  
77  }