KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > dom > DOMEmitter


1 package net.sf.saxon.dom;
2 import net.sf.saxon.event.Emitter;
3 import net.sf.saxon.om.NamespaceConstant;
4 import net.sf.saxon.trans.DynamicError;
5 import net.sf.saxon.trans.XPathException;
6 import org.w3c.dom.*;
7
8
9 /**
10   * DOMEmitter is an Emitter that attaches the result tree to a specified Node in a DOM Document
11   */

12
13 public class DOMEmitter extends Emitter
14 {
15     protected Node currentNode;
16     protected Document document;
17     private boolean canNormalize = true;
18
19     /**
20     * Start of the document.
21     */

22
23     public void open () {}
24
25     /**
26     * End of the document.
27     */

28
29     public void close () {}
30
31     /**
32      * Start of a document node.
33     */

34
35     public void startDocument(int properties) throws XPathException {}
36
37     /**
38      * Notify the end of a document node
39      */

40
41     public void endDocument() throws XPathException {}
42
43     /**
44     * Start of an element.
45     */

46
47     public void startElement (int nameCode, int typeCode, int locationId, int properties) throws XPathException {
48         String JavaDoc qname = namePool.getDisplayName(nameCode);
49         String JavaDoc uri = namePool.getURI(nameCode);
50         try {
51             Element element = document.createElementNS(uri, qname);
52             currentNode.appendChild(element);
53             currentNode = element;
54         } catch (DOMException err) {
55             throw new DynamicError(err);
56         }
57     }
58
59     public void namespace (int namespaceCode, int properties) throws XPathException {
60         try {
61             String JavaDoc prefix = namePool.getPrefixFromNamespaceCode(namespaceCode);
62             String JavaDoc uri = namePool.getURIFromNamespaceCode(namespaceCode);
63             Element element = (Element)currentNode;
64             if (!(uri.equals(NamespaceConstant.XML))) {
65                 if (prefix.equals("")) {
66                     element.setAttributeNS("http://www.w3.org/2000/xmlns/",
67                                            "xmlns", uri);
68                 } else {
69                     element.setAttributeNS("http://www.w3.org/2000/xmlns/",
70                                             "xmlns:" + prefix, uri);
71
72                 }
73             }
74         } catch (DOMException err) {
75             throw new DynamicError(err);
76         }
77     }
78
79     public void attribute (int nameCode, int typeCode, CharSequence JavaDoc value, int locationId, int properties)
80     throws XPathException {
81         String JavaDoc qname = namePool.getDisplayName(nameCode);
82         String JavaDoc uri = namePool.getURI(nameCode);
83         try {
84             Element element = (Element)currentNode;
85             element.setAttributeNS(uri, qname, value.toString());
86         } catch (DOMException err) {
87             throw new DynamicError(err);
88         }
89     }
90
91     public void startContent() throws XPathException {}
92
93     /**
94     * End of an element.
95     */

96
97     public void endElement () throws XPathException {
98         if (canNormalize) {
99             try {
100                 currentNode.normalize();
101             } catch (Throwable JavaDoc err) {
102                 canNormalize = false;
103             } // in case it's a Level 1 DOM
104
}
105
106         currentNode = currentNode.getParentNode();
107
108     }
109
110
111     /**
112     * Character data.
113     */

114
115     public void characters (CharSequence JavaDoc chars, int locationId, int properties) throws XPathException
116     {
117         try {
118             Text text = document.createTextNode(chars.toString());
119             currentNode.appendChild(text);
120         } catch (DOMException err) {
121             throw new DynamicError(err);
122         }
123     }
124
125
126     /**
127     * Handle a processing instruction.
128     */

129
130     public void processingInstruction (String JavaDoc target, CharSequence JavaDoc data, int locationId, int properties)
131         throws XPathException
132     {
133         try {
134             ProcessingInstruction pi =
135                 document.createProcessingInstruction(target, data.toString());
136             currentNode.appendChild(pi);
137         } catch (DOMException err) {
138             throw new DynamicError(err);
139         }
140     }
141
142     /**
143     * Handle a comment.
144     */

145
146     public void comment (CharSequence JavaDoc chars, int locationId, int properties) throws XPathException
147     {
148         try {
149             Comment comment = document.createComment(chars.toString());
150             currentNode.appendChild(comment);
151         } catch (DOMException err) {
152             throw new DynamicError(err);
153         }
154     }
155
156     /**
157     * Set output destination
158     */

159
160     public void setNode (Node node) {
161         if (node == null) {
162             return;
163         }
164         currentNode = node;
165         if (node.getNodeType() == Node.DOCUMENT_NODE) {
166             document = (Document)node;
167         } else {
168             document = currentNode.getOwnerDocument();
169         }
170     }
171
172 }
173
174 //
175
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
176
// you may not use this file except in compliance with the License. You may obtain a copy of the
177
// License at http://www.mozilla.org/MPL/
178
//
179
// Software distributed under the License is distributed on an "AS IS" basis,
180
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
181
// See the License for the specific language governing rights and limitations under the License.
182
//
183
// The Original Code is: all this file.
184
//
185
// The Initial Developer of the Original Code is Michael H. Kay.
186
//
187
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
188
//
189
// Contributor(s): none.
190
//
191
Popular Tags