KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.output;
2 import com.icl.saxon.*;
3 //import com.icl.saxon.om.Name;
4
import com.icl.saxon.om.NamePool;
5 import org.xml.sax.*;
6 import org.xml.sax.helpers.AttributeListImpl JavaDoc;
7 import java.io.*;
8 import java.util.*;
9 import javax.xml.transform.TransformerException JavaDoc;
10
11 /**
12 * A DocumentHandlerProxy is an Emitter that filters data before passing it to an
13 * underlying SAX DocumentHandler. Note that in general the output passed to an Emitter
14 * corresponds to an External General Parsed Entity. A SAX DocumentHandler only expects
15 * to deal with well-formed XML documents, so we only pass it the contents of the first
16 * element encountered.
17 */

18   
19 public class DocumentHandlerProxy extends Emitter
20 {
21     protected DocumentHandler handler;
22 // protected CharacterSet characterSet;
23
protected AttributeListImpl JavaDoc outputAtts = new AttributeListImpl JavaDoc();
24     private int depth = 0;
25
26     /**
27     * Set the underlying document handler. This call is mandatory before using the Emitter.
28     */

29
30     public void setUnderlyingDocumentHandler(DocumentHandler handler) {
31         this.handler = handler;
32     }
33
34     /**
35     * Set Document Locator
36     */

37
38     public void setDocumentLocator(Locator locator) {
39         if (handler!=null)
40             handler.setDocumentLocator(locator);
41     }
42
43     /**
44     * Start of document
45     */

46
47     public void startDocument() throws TransformerException JavaDoc {
48         if (handler==null) {
49             throw new TransformerException JavaDoc("DocumentHandlerProxy.startDocument(): no underlying handler provided");
50         }
51         try {
52             handler.startDocument();
53         } catch (SAXException err) {
54             throw new TransformerException JavaDoc(err);
55         }
56         
57         depth = 0;
58     }
59
60     /**
61     * End of document
62     */

63
64     public void endDocument() throws TransformerException JavaDoc {
65         try {
66             handler.endDocument();
67         } catch (SAXException err) {
68             throw new TransformerException JavaDoc(err);
69         }
70     }
71
72     /**
73     * Start of element
74     */

75
76     public void startElement(int nameCode, Attributes attributes,
77                              int[] namespaces, int nscount) throws TransformerException JavaDoc {
78         depth++;
79         outputAtts.clear();
80         for (int a=0; a<attributes.getLength(); a++) {
81             outputAtts.addAttribute(
82                 attributes.getQName(a),
83                 attributes.getType(a),
84                 attributes.getValue(a) );
85         }
86         if (depth>0) { // only one top-level element allowed
87

88             for (int n=0; n<nscount; n++) {
89                 String JavaDoc prefix = namePool.getPrefixFromNamespaceCode(namespaces[n]);
90                 String JavaDoc uri = namePool.getURIFromNamespaceCode(namespaces[n]);
91                 if (prefix.equals("")) {
92                     outputAtts.addAttribute("xmlns", "NMTOKEN", uri);
93                 } else {
94                     outputAtts.addAttribute("xmlns:" + prefix, "NMTOKEN", uri);
95                 }
96             }
97             try {
98                 handler.startElement(namePool.getDisplayName(nameCode), outputAtts);
99             } catch (SAXException err) {
100                 throw new TransformerException JavaDoc(err);
101             }
102         }
103     }
104
105     /**
106     * End of element
107     */

108
109     public void endElement(int nameCode) throws TransformerException JavaDoc {
110         if (depth>0) {
111             try {
112                 handler.endElement(namePool.getDisplayName(nameCode));
113             } catch (SAXException err) {
114                 throw new TransformerException JavaDoc(err);
115             }
116         }
117         depth--;
118         // if this was the outermost element, no further elements will be processed
119
if (depth<=0) {
120             depth = Integer.MIN_VALUE; // crude but effective
121
}
122     }
123
124     /**
125     * Character data
126     */

127
128     public void characters(char[] chars, int start, int len) throws TransformerException JavaDoc {
129         if (depth>0) {
130             try {
131                 handler.characters(chars, start, len);
132             } catch (SAXException err) {
133                 throw new TransformerException JavaDoc(err);
134             }
135         }
136     }
137
138     /**
139     * Ignorable Whitespace
140     */

141
142     //public void ignorableWhitespace(char[] chars, int start, int len) throws TransformerException {
143
// if (depth>0) {
144
// handler.ignorableWhitespace(chars, start, len);
145
// }
146
//}
147

148     /**
149     * Processing Instruction
150     */

151
152     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws TransformerException JavaDoc {
153         try {
154             handler.processingInstruction(target, data);
155         } catch (SAXException err) {
156             throw new TransformerException JavaDoc(err);
157         }
158     }
159
160     /**
161     * Output a comment
162     */

163
164     public void comment (char ch[], int start, int length) {}
165
166
167     /**
168     * Switch escaping on or off. This is called when the XSLT disable-output-escaping attribute
169     * is used to switch escaping on or off. It is not called for other sections of output (e.g.
170     * element names) where escaping is inappropriate.
171     */

172
173     //public void setEscaping(boolean escaping) throws TransformerException {}
174

175 }
176
177 //
178
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
179
// you may not use this file except in compliance with the License. You may obtain a copy of the
180
// License at http://www.mozilla.org/MPL/
181
//
182
// Software distributed under the License is distributed on an "AS IS" basis,
183
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
184
// See the License for the specific language governing rights and limitations under the License.
185
//
186
// The Original Code is: all this file.
187
//
188
// The Initial Developer of the Original Code is
189
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
190
//
191
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
192
//
193
// Contributor(s): none.
194
//
195
Popular Tags