package com.sun.xml.internal.txw2.output;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMResult;
import java.util.ArrayList;
import java.util.Stack;
import com.sun.xml.internal.txw2.TxwException;
public class DomSerializer implements XmlSerializer {
private final SaxSerializer serializer;
public DomSerializer(Node node) {
Dom2SaxAdapter adapter = new Dom2SaxAdapter(node);
serializer = new SaxSerializer(adapter,adapter,false);
}
public DomSerializer(DOMResult domResult) {
Node node = domResult.getNode();
if (node == null) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
domResult.setNode(doc);
serializer = new SaxSerializer(new Dom2SaxAdapter(doc),null,false);
} catch (ParserConfigurationException pce) {
throw new TxwException(pce);
}
} else {
serializer = new SaxSerializer(new Dom2SaxAdapter(node),null,false);
}
}
public void startDocument() {
serializer.startDocument();
}
public void beginStartTag(String uri, String localName, String prefix) {
serializer.beginStartTag(uri, localName, prefix);
}
public void writeAttribute(String uri, String localName, String prefix, StringBuilder value) {
serializer.writeAttribute(uri, localName, prefix, value);
}
public void writeXmlns(String prefix, String uri) {
serializer.writeXmlns(prefix, uri);
}
public void endStartTag(String uri, String localName, String prefix) {
serializer.endStartTag(uri, localName, prefix);
}
public void endTag() {
serializer.endTag();
}
public void text(StringBuilder text) {
serializer.text(text);
}
public void cdata(StringBuilder text) {
serializer.cdata(text);
}
public void (StringBuilder comment) {
serializer.comment(comment);
}
public void endDocument() {
serializer.endDocument();
}
public void flush() {
}
}
class Dom2SaxAdapter implements ContentHandler, LexicalHandler {
private final Node _node;
private final Stack _nodeStk = new Stack();
private boolean inCDATA;
public final Element getCurrentElement() {
return (Element) _nodeStk.peek();
}
private final Document _document;
public Dom2SaxAdapter(Node node)
{
_node = node;
_nodeStk.push(_node);
if( node instanceof Document )
this._document = (Document)node;
else
this._document = node.getOwnerDocument();
}
public Dom2SaxAdapter() throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
_document = factory.newDocumentBuilder().newDocument();
_node = _document;
_nodeStk.push( _document );
}
public Node getDOM() {
return _node;
}
public void startDocument() {
}
public void endDocument(){
}
public void startElement(String namespace, String localName, String qName, Attributes attrs){
Element element = _document.createElementNS(namespace, qName);
if( element==null ) {
throw new TxwException("Your DOM provider doesn't support the createElementNS method properly");
}
for( int i=0; i<unprocessedNamespaces.size(); i+=2 ) {
String prefix = (String)unprocessedNamespaces.get(i+0);
String uri = (String)unprocessedNamespaces.get(i+1);
String qname;
if( "".equals(prefix) || prefix==null )
qname = "xmlns";
else
qname = "xmlns:"+prefix;
if( element.hasAttributeNS("http://www.w3.org/2000/xmlns/",qname) ) {
element.removeAttributeNS("http://www.w3.org/2000/xmlns/",qname);
}
element.setAttributeNS("http://www.w3.org/2000/xmlns/",qname, uri);
}
unprocessedNamespaces.clear();
int length = attrs.getLength();
for(int i=0;i<length;i++){
String namespaceuri = attrs.getURI(i);
String value = attrs.getValue(i);
String qname = attrs.getQName(i);
element.setAttributeNS(namespaceuri, qname, value);
}
getParent().appendChild(element);
_nodeStk.push(element);
}
private final Node getParent() {
return (Node) _nodeStk.peek();
}
public void endElement(String namespace, String localName, String qName){
_nodeStk.pop();
}
public void characters(char[] ch, int start, int length) {
Node text;
if(inCDATA)
text = _document.createCDATASection(new String(ch, start, length));
else
text = _document.createTextNode(new String(ch, start, length));
getParent().appendChild(text);
}
public void (char ch[], int start, int length) throws SAXException {
getParent().appendChild(_document.createComment(new String(ch,start,length)));
}
public void ignorableWhitespace(char[] ch, int start, int length) {
}
public void processingInstruction(String target, String data) throws org.xml.sax.SAXException{
Node node = _document.createProcessingInstruction(target, data);
getParent().appendChild(node);
}
public void setDocumentLocator(Locator locator) {
}
public void skippedEntity(String name) {
}
private ArrayList unprocessedNamespaces = new ArrayList();
public void startPrefixMapping(String prefix, String uri) {
unprocessedNamespaces.add(prefix);
unprocessedNamespaces.add(uri);
}
public void endPrefixMapping(String prefix) {
}
public void startDTD(String name, String publicId, String systemId) throws SAXException {
}
public void endDTD() throws SAXException {
}
public void startEntity(String name) throws SAXException {
}
public void endEntity(String name) throws SAXException {
}
public void startCDATA() throws SAXException {
inCDATA = true;
}
public void endCDATA() throws SAXException {
inCDATA = false;
}
}