KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > util > DOMBuilder


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
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.apache.ws.jaxme.util;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.xml.XMLConstants JavaDoc;
23
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.EntityReference JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.ProcessingInstruction JavaDoc;
29 import org.w3c.dom.Text JavaDoc;
30 import org.xml.sax.Attributes JavaDoc;
31 import org.xml.sax.ContentHandler JavaDoc;
32 import org.xml.sax.Locator JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /** <p>Converts a stream of SAX events into a DOM node.</p>
36  *
37  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
38  * @version $Id: DOMBuilder.java,v 1.4 2004/04/18 19:15:30 jochen Exp $
39  */

40 public class DOMBuilder implements ContentHandler JavaDoc {
41     private Document JavaDoc document;
42     private Node JavaDoc target;
43     private Node JavaDoc currentNode;
44     private Locator JavaDoc locator;
45     private boolean prefixMappingIsAttribute;
46     private List JavaDoc prefixes;
47     
48     /** <p>Sets whether the event {@link #startPrefixMapping}
49      * shall create an <code>xmlns</code> attribute. Defaults
50      * to false.</p>
51      */

52     public boolean isPrefixMappingIsAttribute() {
53         return prefixMappingIsAttribute;
54     }
55     
56     /** <p>Returns whether the event {@link #startPrefixMapping}
57      * shall create an <code>xmlns</code> attribute. Defaults
58      * to false.</p>
59      */

60     public void setPrefixMappingIsAttribute(boolean pPrefixMappingIsAttribute) {
61         prefixMappingIsAttribute = pPrefixMappingIsAttribute;
62     }
63     
64     /** <p>Sets the document being used as object factory.</p>
65      */

66     public void setDocument(Document JavaDoc pDocument) {
67         document = pDocument;
68     }
69     
70     /** <p>Returns the document being used as object factory.</p>
71      */

72     public Document JavaDoc getDocument() {
73         return document;
74     }
75     
76     /** <p>Sets the Locator.</p>
77      */

78     public void setDocumentLocator(Locator JavaDoc pLocator) {
79         locator = pLocator;
80     }
81     
82     /** <p>Returns the Locator.</p>
83      */

84     public Locator JavaDoc getDocumentLocator() {
85         return locator;
86     }
87     
88     /** <p>Sets the target node. The document is built as a fragment
89      * in the target node.</p>
90      */

91     public void setTarget(Node JavaDoc pNode) {
92         target = pNode;
93         currentNode = pNode;
94         if (getDocument() == null) {
95             setDocument(pNode.getNodeType() == Node.DOCUMENT_NODE ?
96                     (Document JavaDoc) pNode : pNode.getOwnerDocument());
97         }
98     }
99     
100     /** <p>Returns the target node. The document is built as a fragment
101      * in the target node.</p>
102      */

103     public Node JavaDoc getTarget() {
104         return target;
105     }
106     
107     /**
108      * @see org.xml.sax.ContentHandler#startDocument()
109      */

110     public void startDocument() throws SAXException JavaDoc {
111     }
112     
113     /**
114      * @see org.xml.sax.ContentHandler#endDocument()
115      */

116     public void endDocument() throws SAXException JavaDoc {
117     }
118     
119     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
120             throws SAXException JavaDoc {
121         if (isPrefixMappingIsAttribute()) {
122             if (prefixes == null) {
123                 prefixes = new ArrayList JavaDoc();
124             }
125             prefixes.add(prefix);
126             prefixes.add(uri);
127         }
128     }
129     
130     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
131     }
132     
133     public void startElement(String JavaDoc pNamespaceURI, String JavaDoc pLocalName,
134                              String JavaDoc pQName, Attributes JavaDoc pAttr) throws SAXException JavaDoc {
135         Document JavaDoc doc = getDocument();
136         Element JavaDoc element;
137         if (pNamespaceURI == null || pNamespaceURI.length() == 0) {
138             element = doc.createElement(pQName);
139         } else {
140             element = doc.createElementNS(pNamespaceURI, pQName);
141         }
142         if (pAttr != null) {
143             for (int i = 0; i < pAttr.getLength(); i++) {
144                 String JavaDoc uri = pAttr.getURI(i);
145                 String JavaDoc qName = pAttr.getQName(i);
146                 String JavaDoc value = pAttr.getValue(i);
147                 if (uri == null || uri.length() == 0) {
148                     element.setAttribute(qName, value);
149                 } else {
150                     element.setAttributeNS(uri, qName, value);
151                 }
152             }
153         }
154         if (prefixes != null) {
155             for (int i = 0; i < prefixes.size(); i += 2) {
156                 String JavaDoc prefix = (String JavaDoc) prefixes.get(i);
157                 String JavaDoc uri = (String JavaDoc) prefixes.get(i+1);
158                 if (prefix == null || "".equals(prefix)) {
159                     element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
160                                            XMLConstants.XMLNS_ATTRIBUTE, uri);
161                 } else {
162                     element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
163                                            XMLConstants.XMLNS_ATTRIBUTE + ':' + prefix, uri);
164                 }
165             }
166             prefixes.clear();
167         }
168         currentNode.appendChild(element);
169         currentNode = element;
170     }
171     
172     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
173             throws SAXException JavaDoc {
174         currentNode = currentNode.getParentNode();
175     }
176     
177     public void characters(char[] ch, int start, int length)
178             throws SAXException JavaDoc {
179         Node JavaDoc node = currentNode.getLastChild();
180         String JavaDoc s = new String JavaDoc(ch, start, length);
181         if (node != null && node.getNodeType() == Node.TEXT_NODE) {
182             ((Text JavaDoc) node).appendData(s);
183         } else {
184             Text JavaDoc text = getDocument().createTextNode(s);
185             currentNode.appendChild(text);
186         }
187     }
188     
189     public void ignorableWhitespace(char[] ch, int start, int length)
190             throws SAXException JavaDoc {
191         characters(ch, start, length);
192     }
193     
194     public void processingInstruction(String JavaDoc pTarget, String JavaDoc pData)
195             throws SAXException JavaDoc {
196         ProcessingInstruction JavaDoc pi = getDocument().createProcessingInstruction(pTarget, pData);
197         currentNode.appendChild(pi);
198     }
199     
200     public void skippedEntity(String JavaDoc pName) throws SAXException JavaDoc {
201         EntityReference JavaDoc entity = getDocument().createEntityReference(pName);
202         currentNode.appendChild(entity);
203     }
204 }
205
Popular Tags