001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.dom;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.springframework.util.Assert;
023: import org.w3c.dom.Document;
024: import org.w3c.dom.Element;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.ProcessingInstruction;
027: import org.w3c.dom.Text;
028: import org.xml.sax.Attributes;
029: import org.xml.sax.SAXException;
030: import org.xml.sax.helpers.DefaultHandler;
031:
032: /**
033: * SAX <code>ContentHandler</code> that transforms callback calls to DOM <code>Node</code>s.
034: *
035: * @author Arjen Poutsma
036: * @see org.w3c.dom.Node
037: * @since 1.0.0
038: */
039: public class DomContentHandler extends DefaultHandler {
040:
041: private final Document document;
042:
043: private final List elements = new ArrayList();
044:
045: private final Node node;
046:
047: /**
048: * Creates a new instance of the <code>DomContentHandler</code> with the given node.
049: *
050: * @param node the node to publish events to
051: */
052: public DomContentHandler(Node node) {
053: Assert.notNull(node, "node must not be null");
054: this .node = node;
055: if (node instanceof Document) {
056: document = (Document) node;
057: } else {
058: document = node.getOwnerDocument();
059: }
060: Assert.notNull(document, "document must not be null");
061: }
062:
063: private Node getParent() {
064: if (!elements.isEmpty()) {
065: return (Node) elements.get(elements.size() - 1);
066: } else {
067: return node;
068: }
069: }
070:
071: public void characters(char ch[], int start, int length)
072: throws SAXException {
073: String data = new String(ch, start, length);
074: Node parent = getParent();
075: Node lastChild = parent.getLastChild();
076: if (lastChild != null
077: && lastChild.getNodeType() == Node.TEXT_NODE) {
078: ((Text) lastChild).appendData(data);
079: } else {
080: Text text = document.createTextNode(data);
081: parent.appendChild(text);
082: }
083: }
084:
085: public void endElement(String uri, String localName, String qName)
086: throws SAXException {
087: elements.remove(elements.size() - 1);
088: }
089:
090: public void processingInstruction(String target, String data)
091: throws SAXException {
092: Node parent = getParent();
093: ProcessingInstruction pi = document
094: .createProcessingInstruction(target, data);
095: parent.appendChild(pi);
096: }
097:
098: public void startElement(String uri, String localName,
099: String qName, Attributes attributes) throws SAXException {
100: Node parent = getParent();
101: Element element = document.createElementNS(uri, qName);
102: for (int i = 0; i < attributes.getLength(); i++) {
103: String attrUri = attributes.getURI(i);
104: String attrQname = attributes.getQName(i);
105: String value = attributes.getValue(i);
106: element.setAttributeNS(attrUri, attrQname, value);
107: }
108: parent.appendChild(element);
109: elements.add(element);
110: }
111:
112: }
|