
net.sf.xolite.utils.RootHolder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xo-lite Show documentation
Show all versions of xo-lite Show documentation
This project provides a lightweight framework to
serialize/deserialize (or marshall/unmarshall) java objects into
XML. The implementation is based on standard SAX (Simple Api for
Xml) but it follows a original approach based on the strong data
encapsulation paradigm of Object Oriented (OO)
programming.
The newest version!
/*-------------------------------------------------------------------------
Copyright 2006 Olivier Berlanger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-------------------------------------------------------------------------*/
package net.sf.xolite.utils;
import net.sf.xolite.XMLEventParser;
import net.sf.xolite.XMLParseException;
import net.sf.xolite.XMLSerializable;
import net.sf.xolite.XMLSerializeException;
import net.sf.xolite.XMLSerializer;
/**
* Utility class used when the root of a object tree parsed from XML is not known statically.
* This class is used internally by XML event parsers to dynamically instantiate the tree root when there is a factory defined.
*/
public class RootHolder implements XMLSerializable {
private XMLSerializable root;
private String rootURI;
private String rootTag;
public XMLSerializable getRoot() {
return root;
}
/**
* Instantiate the real root element from the parser factory and delegate parsing to it.
*
* @exception NullPointerException
* if the factory is not defined.
* @exception XMLParseException
* if the object corresponding to the started element is not defined or cannot be instantiated.
*/
public void startElement(String uri, String localName, XMLEventParser parser) throws XMLParseException {
if (root != null) throw new IllegalStateException("Root!=null");
rootURI = uri;
rootTag = localName;
root = parser.parseElement(uri, localName);
}
/**
* Notification of the end tag of the root element.
*/
public void endElement(String uri, String localName, XMLEventParser parser) throws XMLParseException {
if ((rootURI != null) && !rootURI.equals(uri)) parser.throwUnexpectedNamespaceException(rootURI);
if (!rootTag.equals(localName)) parser.throwUnexpectedElementException(rootTag);
}
/**
* Serialize the inner root object.
*/
public void serialize(XMLSerializer serializer) throws XMLSerializeException {
if (root == null) throw new XMLSerializeException("Empty RootHolder cannot be serialized");
root.serialize(serializer);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy