View Javadoc
1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * SAX <code>ContentHandler</code> that transforms callback calls to DOM <code>Node</code>s.
36   *
37   * @author Arjen Poutsma
38   * @see org.w3c.dom.Node
39   * @since 1.0.0
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       * Creates a new instance of the <code>DomContentHandler</code> with the given node.
51       *
52       * @param node the node to publish events to
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      * Unsupported
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 }