001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.oxm.impl;
020:
021: import java.io.ByteArrayInputStream;
022:
023: import javax.xml.parsers.DocumentBuilder;
024: import javax.xml.parsers.DocumentBuilderFactory;
025:
026: import org.w3c.dom.Document;
027: import org.w3c.dom.Element;
028: import org.w3c.dom.Node;
029: import org.w3c.dom.NodeList;
030:
031: /**
032: * @author mleidig@schlund.de
033: * @author Stephan Schmidt <schst@stubbles.net>
034: */
035: public class DOMWriter implements XMLWriter {
036:
037: private Node root;
038: private Node current;
039:
040: public DOMWriter(Node node) {
041: if (node.getNodeType() != Node.ELEMENT_NODE)
042: throw new IllegalArgumentException(
043: "Illegal node type: must be element node");
044: root = node;
045: current = root;
046: }
047:
048: public void writeStartElement(String localName) {
049: Element elem = root.getOwnerDocument().createElement(localName);
050: current.appendChild(elem);
051: current = elem;
052: }
053:
054: public void writeCharacters(String text) {
055: Node node = root.getOwnerDocument().createTextNode(text);
056: current.appendChild(node);
057: }
058:
059: public void writeEndElement() {
060: current = current.getParentNode();
061: }
062:
063: public void writeAttribute(String localName, String value) {
064: Element elem = (Element) current;
065: elem.setAttribute(localName, value);
066: }
067:
068: /**
069: * Writes a character data section
070: *
071: * @param cdata
072: */
073: public void writeCDataSection(String cdata) {
074: Node node = root.getOwnerDocument().createCDATASection(cdata);
075: current.appendChild(node);
076: }
077:
078: /**
079: * Writes an xml fragment to the document.
080: *
081: * The fragment does not need a root element, but it must
082: * be well-formed xml.
083: *
084: * @param xmlFragment The fragment to be written to the document.
085: */
086: public void writeFragment(String xmlFragment) {
087: try {
088: DocumentBuilderFactory builderFactory = DocumentBuilderFactory
089: .newInstance();
090: DocumentBuilder builder = builderFactory
091: .newDocumentBuilder();
092:
093: // make sure, that we have a root node
094: xmlFragment = "<root>" + xmlFragment + "</root>";
095:
096: byte currentXMLBytes[] = xmlFragment.getBytes();
097: ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
098: currentXMLBytes);
099:
100: Document doc = builder.parse(byteArrayInputStream);
101: Element root = doc.getDocumentElement();
102:
103: Document originalDoc = this .root.getOwnerDocument();
104: NodeList list = root.getChildNodes();
105: for (int i = 0; i < list.getLength(); i++) {
106: Node node = originalDoc.importNode(list.item(i), true);
107: current.appendChild(node);
108: }
109: } catch (Exception e) {
110: throw new RuntimeException("Unable to write XML fragment",
111: e);
112: }
113: }
114:
115: public Node getNode() {
116: return root;
117: }
118:
119: public XPathPosition getCurrentPosition() {
120: return new DOMXPathPosition(current);
121: }
122: }
|