1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.xml.dom;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.springframework.util.Assert;
23
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26 import org.w3c.dom.Node;
27 import org.w3c.dom.ProcessingInstruction;
28 import org.w3c.dom.Text;
29 import org.xml.sax.Attributes;
30 import org.xml.sax.ContentHandler;
31 import org.xml.sax.Locator;
32 import org.xml.sax.SAXException;
33
34
35
36
37
38
39
40
41 public class DomContentHandler implements ContentHandler {
42
43 private final Document document;
44
45 private final List<Element> elements = new ArrayList<Element>();
46
47 private final Node node;
48
49
50
51
52
53
54 public DomContentHandler(Node node) {
55 Assert.notNull(node, "node must not be null");
56 this.node = node;
57 if (node instanceof Document) {
58 document = (Document) node;
59 }
60 else {
61 document = node.getOwnerDocument();
62 }
63 Assert.notNull(document, "document must not be null");
64 }
65
66 private Node getParent() {
67 if (!elements.isEmpty()) {
68 return elements.get(elements.size() - 1);
69 }
70 else {
71 return node;
72 }
73 }
74
75 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
76 Node parent = getParent();
77 Element element = document.createElementNS(uri, qName);
78 for (int i = 0; i < attributes.getLength(); i++) {
79 String attrUri = attributes.getURI(i);
80 String attrQname = attributes.getQName(i);
81 String value = attributes.getValue(i);
82 if (!attrQname.startsWith("xmlns")) {
83 element.setAttributeNS(attrUri, attrQname, value);
84 }
85 }
86 element = (Element) parent.appendChild(element);
87 elements.add(element);
88 }
89
90 public void endElement(String uri, String localName, String qName) throws SAXException {
91 elements.remove(elements.size() - 1);
92 }
93
94 public void characters(char ch[], int start, int length) throws SAXException {
95 String data = new String(ch, start, length);
96 Node parent = getParent();
97 Node lastChild = parent.getLastChild();
98 if (lastChild != null && lastChild.getNodeType() == Node.TEXT_NODE) {
99 ((Text) lastChild).appendData(data);
100 }
101 else {
102 Text text = document.createTextNode(data);
103 parent.appendChild(text);
104 }
105 }
106
107 public void processingInstruction(String target, String data) throws SAXException {
108 Node parent = getParent();
109 ProcessingInstruction pi = document.createProcessingInstruction(target, data);
110 parent.appendChild(pi);
111 }
112
113
114
115
116
117 public void setDocumentLocator(Locator locator) {
118 }
119
120 public void startDocument() throws SAXException {
121 }
122
123 public void endDocument() throws SAXException {
124 }
125
126 public void startPrefixMapping(String prefix, String uri) throws SAXException {
127 }
128
129 public void endPrefixMapping(String prefix) throws SAXException {
130 }
131
132 public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
133 }
134
135 public void skippedEntity(String name) throws SAXException {
136 }
137 }