package com.skaringa.javaxml.handler.dom;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;
import com.skaringa.javaxml.handler.AbstractDocumentOutputHandler;
import com.skaringa.javaxml.handler.AttrImpl;
import com.skaringa.javaxml.handler.DocumentOutputHandlerInterface;
import com.skaringa.util.Log;
/**
* A concrete DocumentOutputHandlerInterface.
* It writes the output events to a DOM tree.
* It is used for serialization of Java objects into XML documents.
*/
public final class DOMOutputHandler
extends AbstractDocumentOutputHandler
implements DocumentOutputHandlerInterface {
private Document _doc;
private Node _curNode;
private boolean _isRootElement;
/**
* Construct a new DOMOutputHandler with a given document and element.
* This allows to insert the result of the serialization
* into an existing document.
* @param doc The DOM document.
* @param elem The parent element to insert output below.
*/
public DOMOutputHandler(org.w3c.dom.Document doc, org.w3c.dom.Element elem) {
_doc = doc;
_curNode = elem;
}
/**
* Construct a new DOMOutputHandler with a given document.
* The result of the serialization is inserted below its root element.
* @param doc The DOM document.
*/
public DOMOutputHandler(org.w3c.dom.Document doc) {
_doc = doc;
_curNode = null;
}
/**
* Construct a new DOMOutputHandler.
* A new DOM document is created during serialization.
*/
public DOMOutputHandler() {
_doc = null;
_curNode = null;
}
/**
* Get the document.
* @return The DOM document.
*/
public org.w3c.dom.Document getDocument() {
return _doc;
}
/**
* @see DocumentOutputHandlerInterface#startDocument()
*/
public void startDocument() {
_isRootElement = true;
try {
if (_doc == null) {
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = fac.newDocumentBuilder();
_doc = docBuilder.newDocument();
}
}
catch (ParserConfigurationException e) {
Log.error(e);
}
}
/**
* @see DocumentOutputHandlerInterface#endDocument()
*/
public void endDocument() {
}
/**
* @see DocumentOutputHandlerInterface#startElement(String, Attributes)
*/
public void startElement(String name, Attributes attrs) {
Element element = _doc.createElement(name);
if (_curNode == null) {
_doc.appendChild(element);
}
else {
_curNode.appendChild(element);
}
_curNode = element;
if (_isRootElement) {
_isRootElement = false;
// write namespace declarations
attrs = appendNamespaceDeclarations(name, attrs);
}
for (int i = 0; i < attrs.getLength(); ++i) {
element.setAttribute(attrs.getQName(i), attrs.getValue(i));
}
}
/**
* @see DocumentOutputHandlerInterface#startElement(String)
*/
public void startElement(String name) {
Attributes attrs = new AttrImpl();
startElement(name, attrs);
}
/**
* @see DocumentOutputHandlerInterface#appendText(String)
*/
public void appendText(String text) {
_curNode.appendChild(_doc.createTextNode(text));
}
/**
* @see DocumentOutputHandlerInterface#endElement(String)
*/
public void endElement(String name) {
_curNode = _curNode.getParentNode();
}
}