001: package org.kohsuke.rngom.digested;
002:
003: import org.w3c.dom.Attr;
004: import org.w3c.dom.CDATASection;
005: import org.w3c.dom.Comment;
006: import org.w3c.dom.Document;
007: import org.w3c.dom.DocumentFragment;
008: import org.w3c.dom.Element;
009: import org.w3c.dom.EntityReference;
010: import org.w3c.dom.NamedNodeMap;
011: import org.w3c.dom.Node;
012: import org.w3c.dom.NodeList;
013: import org.w3c.dom.ProcessingInstruction;
014: import org.w3c.dom.Text;
015:
016: import javax.xml.stream.XMLStreamException;
017: import javax.xml.stream.XMLStreamWriter;
018:
019: /**
020: * Printer of DOM to XML using StAX {@link XMLStreamWriter}.
021: *
022: * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
023: */
024: class DOMPrinter {
025: protected XMLStreamWriter out;
026:
027: public DOMPrinter(XMLStreamWriter out) {
028: this .out = out;
029: }
030:
031: public void print(Node node) throws XMLStreamException {
032: switch (node.getNodeType()) {
033: case Node.DOCUMENT_NODE:
034: visitDocument((Document) node);
035: break;
036: case Node.DOCUMENT_FRAGMENT_NODE:
037: visitDocumentFragment((DocumentFragment) node);
038: break;
039: case Node.ELEMENT_NODE:
040: visitElement((Element) node);
041: break;
042: case Node.TEXT_NODE:
043: visitText((Text) node);
044: break;
045: case Node.CDATA_SECTION_NODE:
046: visitCDATASection((CDATASection) node);
047: break;
048: case Node.PROCESSING_INSTRUCTION_NODE:
049: visitProcessingInstruction((ProcessingInstruction) node);
050: break;
051: case Node.ENTITY_REFERENCE_NODE:
052: visitReference((EntityReference) node);
053: break;
054: case Node.COMMENT_NODE:
055: visitComment((Comment) node);
056: break;
057: case Node.DOCUMENT_TYPE_NODE:
058: break;
059: case Node.ATTRIBUTE_NODE:
060: case Node.ENTITY_NODE:
061: default:
062: throw new XMLStreamException("Unexpected DOM Node Type "
063: + node.getNodeType());
064: }
065: }
066:
067: protected void visitChildren(Node node) throws XMLStreamException {
068: NodeList nodeList = node.getChildNodes();
069: if (nodeList != null) {
070: for (int i = 0; i < nodeList.getLength(); i++) {
071: print(nodeList.item(i));
072: }
073: }
074: }
075:
076: protected void visitDocument(Document document)
077: throws XMLStreamException {
078: out.writeStartDocument();
079: print(document.getDocumentElement());
080: out.writeEndDocument();
081: }
082:
083: protected void visitDocumentFragment(
084: DocumentFragment documentFragment)
085: throws XMLStreamException {
086: visitChildren(documentFragment);
087: }
088:
089: protected void visitElement(Element node) throws XMLStreamException {
090: out.writeStartElement(node.getPrefix(), node.getLocalName(),
091: node.getNamespaceURI());
092: NamedNodeMap attrs = node.getAttributes();
093: for (int i = 0; i < attrs.getLength(); i++) {
094: visitAttr((Attr) attrs.item(i));
095: }
096: visitChildren(node);
097: out.writeEndElement();
098: }
099:
100: protected void visitAttr(Attr node) throws XMLStreamException {
101: String name = node.getLocalName();
102: if (name.equals("xmlns")) {
103: out.writeDefaultNamespace(node.getNamespaceURI());
104: } else {
105: String prefix = node.getPrefix();
106: if (prefix != null && prefix.equals("xmlns")) {
107: out.writeNamespace(prefix, node.getNamespaceURI());
108: } else {
109: out.writeAttribute(prefix, node.getNamespaceURI(),
110: name, node.getNodeValue());
111: }
112: }
113: }
114:
115: protected void visitComment(Comment comment)
116: throws XMLStreamException {
117: out.writeComment(comment.getData());
118: }
119:
120: protected void visitText(Text node) throws XMLStreamException {
121: out.writeCharacters(node.getNodeValue());
122: }
123:
124: protected void visitCDATASection(CDATASection cdata)
125: throws XMLStreamException {
126: out.writeCData(cdata.getNodeValue());
127: }
128:
129: protected void visitProcessingInstruction(
130: ProcessingInstruction processingInstruction)
131: throws XMLStreamException {
132: out.writeProcessingInstruction(processingInstruction
133: .getNodeName(), processingInstruction.getData());
134: }
135:
136: protected void visitReference(EntityReference entityReference)
137: throws XMLStreamException {
138: visitChildren(entityReference);
139: }
140: }
|