325N/A/*
325N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.txw2.output;
325N/A
325N/Aimport org.w3c.dom.Document;
325N/Aimport org.w3c.dom.Element;
325N/Aimport org.w3c.dom.Node;
325N/Aimport org.w3c.dom.Text;
325N/Aimport org.xml.sax.Attributes;
325N/Aimport org.xml.sax.ContentHandler;
325N/Aimport org.xml.sax.Locator;
325N/Aimport org.xml.sax.SAXException;
325N/Aimport org.xml.sax.ext.LexicalHandler;
325N/A
325N/Aimport javax.xml.parsers.DocumentBuilder;
325N/Aimport javax.xml.parsers.DocumentBuilderFactory;
325N/Aimport javax.xml.parsers.ParserConfigurationException;
325N/Aimport javax.xml.transform.dom.DOMResult;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.Stack;
325N/A
325N/Aimport com.sun.xml.internal.txw2.TxwException;
325N/A
325N/A/**
325N/A * {@link XmlSerializer} for {@link javax.xml.transform.dom.DOMResult} and {@link org.w3c.dom.Node}.
325N/A *
325N/A * @author Ryan.Shoemaker@Sun.COM
325N/A */
325N/Apublic class DomSerializer implements XmlSerializer {
325N/A
325N/A // delegate to SaxSerializer
325N/A private final SaxSerializer serializer;
325N/A
325N/A public DomSerializer(Node node) {
325N/A Dom2SaxAdapter adapter = new Dom2SaxAdapter(node);
325N/A serializer = new SaxSerializer(adapter,adapter,false);
325N/A }
325N/A
325N/A public DomSerializer(DOMResult domResult) {
325N/A Node node = domResult.getNode();
325N/A
325N/A if (node == null) {
325N/A try {
325N/A DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
325N/A dbf.setNamespaceAware(true);
325N/A DocumentBuilder db = dbf.newDocumentBuilder();
325N/A Document doc = db.newDocument();
325N/A domResult.setNode(doc);
325N/A serializer = new SaxSerializer(new Dom2SaxAdapter(doc),null,false);
325N/A } catch (ParserConfigurationException pce) {
325N/A throw new TxwException(pce);
325N/A }
325N/A } else {
325N/A serializer = new SaxSerializer(new Dom2SaxAdapter(node),null,false);
325N/A }
325N/A }
325N/A
325N/A // XmlSerializer api's - delegate to SaxSerializer
325N/A public void startDocument() {
325N/A serializer.startDocument();
325N/A }
325N/A
325N/A public void beginStartTag(String uri, String localName, String prefix) {
325N/A serializer.beginStartTag(uri, localName, prefix);
325N/A }
325N/A
325N/A public void writeAttribute(String uri, String localName, String prefix, StringBuilder value) {
325N/A serializer.writeAttribute(uri, localName, prefix, value);
325N/A }
325N/A
325N/A public void writeXmlns(String prefix, String uri) {
325N/A serializer.writeXmlns(prefix, uri);
325N/A }
325N/A
325N/A public void endStartTag(String uri, String localName, String prefix) {
325N/A serializer.endStartTag(uri, localName, prefix);
325N/A }
325N/A
325N/A public void endTag() {
325N/A serializer.endTag();
325N/A }
325N/A
325N/A public void text(StringBuilder text) {
325N/A serializer.text(text);
325N/A }
325N/A
325N/A public void cdata(StringBuilder text) {
325N/A serializer.cdata(text);
325N/A }
325N/A
325N/A public void comment(StringBuilder comment) {
325N/A serializer.comment(comment);
325N/A }
325N/A
325N/A public void endDocument() {
325N/A serializer.endDocument();
325N/A }
325N/A
325N/A public void flush() {
325N/A // no flushing
325N/A }
325N/A}
325N/A
325N/A
325N/A
325N/A
325N/A/**
325N/A * Builds a DOM tree from SAX2 events.
325N/A *
325N/A * @author Vivek Pandey
325N/A */
325N/Aclass Dom2SaxAdapter implements ContentHandler, LexicalHandler {
325N/A
325N/A private final Node _node;
325N/A private final Stack _nodeStk = new Stack();
325N/A private boolean inCDATA;
325N/A
325N/A public final Element getCurrentElement() {
325N/A return (Element) _nodeStk.peek();
325N/A }
325N/A
325N/A /**
325N/A * Document object that owns the specified node.
325N/A */
325N/A private final Document _document;
325N/A
325N/A /**
325N/A * @param node
325N/A * Nodes will be created and added under this object.
325N/A */
325N/A public Dom2SaxAdapter(Node node)
325N/A {
325N/A _node = node;
325N/A _nodeStk.push(_node);
325N/A
325N/A if( node instanceof Document )
325N/A this._document = (Document)node;
325N/A else
325N/A this._document = node.getOwnerDocument();
325N/A }
325N/A
325N/A /**
325N/A * Creates a fresh empty DOM document and adds nodes under this document.
325N/A */
325N/A public Dom2SaxAdapter() throws ParserConfigurationException {
325N/A DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
325N/A factory.setNamespaceAware(true);
325N/A factory.setValidating(false);
325N/A
325N/A _document = factory.newDocumentBuilder().newDocument();
325N/A _node = _document;
325N/A _nodeStk.push( _document );
325N/A }
325N/A
325N/A public Node getDOM() {
325N/A return _node;
325N/A }
325N/A
325N/A public void startDocument() {
325N/A }
325N/A
325N/A public void endDocument(){
325N/A }
325N/A
325N/A public void startElement(String namespace, String localName, String qName, Attributes attrs){
325N/A
325N/A // some broken DOM implementatino (we confirmed it with SAXON)
325N/A // return null from this method.
325N/A Element element = _document.createElementNS(namespace, qName);
325N/A
325N/A if( element==null ) {
325N/A // if so, report an user-friendly error message,
325N/A // rather than dying mysteriously with NPE.
325N/A throw new TxwException("Your DOM provider doesn't support the createElementNS method properly");
325N/A }
325N/A
325N/A // process namespace bindings
325N/A for( int i=0; i<unprocessedNamespaces.size(); i+=2 ) {
325N/A String prefix = (String)unprocessedNamespaces.get(i+0);
325N/A String uri = (String)unprocessedNamespaces.get(i+1);
325N/A
325N/A String qname;
325N/A if( "".equals(prefix) || prefix==null )
325N/A qname = "xmlns";
325N/A else
325N/A qname = "xmlns:"+prefix;
325N/A
325N/A // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0)
325N/A // have a problem of re-setting the same namespace attribute twice.
325N/A // work around this bug removing it first.
325N/A if( element.hasAttributeNS("http://www.w3.org/2000/xmlns/",qname) ) {
325N/A // further workaround for an old Crimson bug where the removeAttribtueNS
325N/A // method throws NPE when the element doesn't have any attribute.
325N/A // to be on the safe side, check the existence of attributes before
325N/A // attempting to remove it.
325N/A // for details about this bug, see org.apache.crimson.tree.ElementNode2
325N/A // line 540 or the following message:
325N/A // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767
325N/A element.removeAttributeNS("http://www.w3.org/2000/xmlns/",qname);
325N/A }
325N/A // workaround until here
325N/A
325N/A element.setAttributeNS("http://www.w3.org/2000/xmlns/",qname, uri);
325N/A }
325N/A unprocessedNamespaces.clear();
325N/A
325N/A
325N/A int length = attrs.getLength();
325N/A for(int i=0;i<length;i++){
325N/A String namespaceuri = attrs.getURI(i);
325N/A String value = attrs.getValue(i);
325N/A String qname = attrs.getQName(i);
325N/A element.setAttributeNS(namespaceuri, qname, value);
325N/A }
325N/A // append this new node onto current stack node
325N/A getParent().appendChild(element);
325N/A // push this node onto stack
325N/A _nodeStk.push(element);
325N/A }
325N/A
325N/A private final Node getParent() {
325N/A return (Node) _nodeStk.peek();
325N/A }
325N/A
325N/A public void endElement(String namespace, String localName, String qName){
325N/A _nodeStk.pop();
325N/A }
325N/A
325N/A
325N/A public void characters(char[] ch, int start, int length) {
325N/A Node text;
325N/A if(inCDATA)
325N/A text = _document.createCDATASection(new String(ch, start, length));
325N/A else
325N/A text = _document.createTextNode(new String(ch, start, length));
325N/A getParent().appendChild(text);
325N/A }
325N/A
325N/A public void comment(char ch[], int start, int length) throws SAXException {
325N/A getParent().appendChild(_document.createComment(new String(ch,start,length)));
325N/A }
325N/A
325N/A
325N/A
325N/A public void ignorableWhitespace(char[] ch, int start, int length) {
325N/A }
325N/A
325N/A public void processingInstruction(String target, String data) throws org.xml.sax.SAXException{
325N/A Node node = _document.createProcessingInstruction(target, data);
325N/A getParent().appendChild(node);
325N/A }
325N/A
325N/A public void setDocumentLocator(Locator locator) {
325N/A }
325N/A
325N/A public void skippedEntity(String name) {
325N/A }
325N/A
325N/A private ArrayList unprocessedNamespaces = new ArrayList();
325N/A
325N/A public void startPrefixMapping(String prefix, String uri) {
325N/A unprocessedNamespaces.add(prefix);
325N/A unprocessedNamespaces.add(uri);
325N/A }
325N/A
325N/A public void endPrefixMapping(String prefix) {
325N/A }
325N/A
325N/A public void startDTD(String name, String publicId, String systemId) throws SAXException {
325N/A }
325N/A
325N/A public void endDTD() throws SAXException {
325N/A }
325N/A
325N/A public void startEntity(String name) throws SAXException {
325N/A }
325N/A
325N/A public void endEntity(String name) throws SAXException {
325N/A }
325N/A
325N/A public void startCDATA() throws SAXException {
325N/A inCDATA = true;
325N/A }
325N/A
325N/A public void endCDATA() throws SAXException {
325N/A inCDATA = false;
325N/A }
325N/A}