KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > output > DOMEmitter


1 package com.icl.saxon.output;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.Namespace;
4 //import com.icl.saxon.om.NamePool;
5

6 import org.xml.sax.Attributes JavaDoc;
7 import org.w3c.dom.*;
8
9 import javax.xml.transform.Result JavaDoc;
10 import javax.xml.transform.TransformerException JavaDoc;
11 import javax.xml.transform.dom.DOMResult JavaDoc;
12
13
14 /**
15   * DOMEmitter is an Emitter that attaches the result tree to a specified Node in a DOM Document
16   */

17   
18 public class DOMEmitter extends Emitter
19 {
20     protected Node currentNode;
21     protected Document document;
22     private boolean canNormalize = true;
23
24     /**
25     * Start of the document.
26     */

27     
28     public void startDocument ()
29     {
30
31     }
32
33     /**
34     * End of the document.
35     */

36     
37     public void endDocument ()
38     {
39
40     }
41
42
43     /**
44     * Start of an element. Output the start tag, escaping special characters.
45     */

46     
47     public void startElement (int nameCode, Attributes JavaDoc attributes,
48                               int[] namespaces, int nscount) throws TransformerException JavaDoc
49     {
50         String JavaDoc name = namePool.getDisplayName(nameCode);
51         try {
52
53             Element element = document.createElement(name);
54             currentNode.appendChild(element);
55             currentNode = element;
56
57             // output the namespaces
58

59             for (int n=0; n<nscount; n++) {
60                 String JavaDoc prefix = namePool.getPrefixFromNamespaceCode(namespaces[n]);
61                 String JavaDoc uri = namePool.getURIFromNamespaceCode(namespaces[n]);
62                 if (!(uri.equals(Namespace.XML))) {
63                     if (prefix.equals("")) {
64                         element.setAttribute("xmlns", uri);
65                     } else {
66                         element.setAttribute("xmlns:" + prefix, uri);
67                     }
68                 }
69             }
70
71             // output the attributes
72

73             for (int i=0; i<attributes.getLength(); i++) {
74                 element.setAttribute(
75                     attributes.getQName(i),
76                     attributes.getValue(i));
77             }
78             
79         } catch (DOMException err) {
80             throw new TransformerException JavaDoc(err);
81         }
82     }
83     
84     /**
85     * End of an element.
86     */

87
88     public void endElement (int nameCode) throws TransformerException JavaDoc
89     {
90
91         if (canNormalize) {
92             try {
93                 currentNode.normalize();
94             } catch (Throwable JavaDoc err) {
95                 canNormalize = false;
96             } // in case it's a Level 1 DOM
97
}
98         
99         currentNode = currentNode.getParentNode();
100
101     }
102
103
104     /**
105     * Character data.
106     */

107
108     public void characters (char[] ch, int start, int length) throws TransformerException JavaDoc
109     {
110         try {
111             Text text = document.createTextNode(new String JavaDoc(ch, start, length));
112             currentNode.appendChild(text);
113         } catch (DOMException err) {
114             throw new TransformerException JavaDoc(err);
115         }
116     }
117
118
119     /**
120     * Handle a processing instruction.
121     */

122     
123     public void processingInstruction (String JavaDoc target, String JavaDoc data)
124         throws TransformerException JavaDoc
125     {
126         try {
127             ProcessingInstruction pi =
128                 document.createProcessingInstruction(target, data);
129             currentNode.appendChild(pi);
130         } catch (DOMException err) {
131             throw new TransformerException JavaDoc(err);
132         }
133     }
134
135     /**
136     * Handle a comment.
137     */

138     
139     public void comment (char ch[], int start, int length) throws TransformerException JavaDoc
140     {
141         try {
142             Comment comment = document.createComment(new String JavaDoc(ch, start, length));
143             currentNode.appendChild(comment);
144         } catch (DOMException err) {
145             throw new TransformerException JavaDoc(err);
146         }
147     }
148
149     /**
150     * Set output destination
151     */

152     
153     public void setNode (Node node) {
154         currentNode = node;
155         if (node instanceof Document) { // seems to be needed for Xerces DOM
156
document = (Document)node;
157         } else {
158             document = currentNode.getOwnerDocument();
159         }
160     }
161     
162 }
163
164 //
165
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
166
// you may not use this file except in compliance with the License. You may obtain a copy of the
167
// License at http://www.mozilla.org/MPL/
168
//
169
// Software distributed under the License is distributed on an "AS IS" basis,
170
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
171
// See the License for the specific language governing rights and limitations under the License.
172
//
173
// The Original Code is: all this file.
174
//
175
// The Initial Developer of the Original Code is
176
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
177
//
178
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
179
//
180
// Contributor(s): none.
181
//
182
Popular Tags