KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.output;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.NamePool;
4 import org.xml.sax.ContentHandler JavaDoc;
5 import org.xml.sax.DocumentHandler JavaDoc;
6 import org.xml.sax.Attributes JavaDoc;
7 import org.xml.sax.Locator JavaDoc;
8 import java.io.*;
9 import javax.xml.transform.Result JavaDoc;
10 import javax.xml.transform.TransformerException JavaDoc;
11 import java.util.Properties JavaDoc;
12
13 /**
14   * Emitter: This interface defines methods that must be implemented by
15   * components that format SAXON output. There is one emitter for XML,
16   * one for HTML, and so on. Additional methods are concerned with
17   * setting options and providing a Writer.<p>
18   *
19   * The interface is deliberately designed to be as close as possible to the
20   * standard SAX2 ContentHandler interface, however, it allows additional
21   * information to be made available.
22   */

23   
24 public abstract class Emitter implements Result JavaDoc
25 {
26
27     protected NamePool namePool;
28     protected String JavaDoc systemId;
29     protected Writer writer;
30     protected OutputStream outputStream;
31     protected Properties JavaDoc outputProperties;
32     protected Locator JavaDoc locator;
33     
34     /**
35     * Set the namePool in which all name codes can be found
36     */

37     
38     public void setNamePool(NamePool namePool) {
39         this.namePool = namePool;
40     }
41
42     /**
43     * Get the namepool used for this document
44     */

45     
46     public NamePool getNamePool() {
47         return namePool;
48     }
49
50     /**
51     * Set the System ID
52     */

53
54     public void setSystemId(String JavaDoc systemId) {
55         this.systemId = systemId;
56     }
57
58     /**
59     * Get the System ID
60     */

61
62     public String JavaDoc getSystemId() {
63         return systemId;
64     }
65
66     /**
67     * Set the output properties
68     */

69
70     public void setOutputProperties(Properties JavaDoc props) {
71         outputProperties = props;
72     }
73
74     /**
75     * Get the output properties
76     */

77
78     public Properties JavaDoc getOutputProperties() {
79         return outputProperties;
80     }
81
82     /**
83     * Determine whether the Emitter wants a Writer for character output or
84     * an OutputStream for binary output
85     */

86     
87     public boolean usesWriter() {
88         return true;
89     }
90
91     /**
92     * Set the output destination as a character stream
93     */

94
95     public void setWriter(Writer writer) {
96         this.writer = writer;
97     }
98
99     /**
100     * Get the output writer
101     */

102
103     public Writer getWriter() {
104         return writer;
105     }
106
107     /**
108     * Set the output destination as a byte stream
109     */

110
111     public void setOutputStream(OutputStream stream) {
112         this.outputStream = stream;
113     }
114
115     /**
116     * Get the output stream
117     */

118
119     public OutputStream getOutputStream() {
120         return outputStream;
121     }
122
123     /**
124     * Notify document start
125     */

126
127     public abstract void startDocument() throws TransformerException JavaDoc;
128
129     /**
130     * Notify document end
131     */

132
133     public abstract void endDocument() throws TransformerException JavaDoc;
134
135     /**
136     * Output an element start tag.
137     * @params name The Name Code identifying the name of the Element within the name pool.
138     * @params attributes The attributes (excluding namespace declarations) associated with
139     * this element.
140     * @param namespaces Array of namespace codes identifying the namespace prefix/uri
141     * pairs associated with this element
142     * @param nscount Number of significant entries within namespaces array
143     */

144
145     public abstract void startElement(int nameCode, Attributes JavaDoc attributes,
146                              int[] namespaces, int nscount) throws TransformerException JavaDoc;
147
148     /**
149     * Output an element end tag
150     * @params name code The name code identifying the element.
151     * Use the namePool.getDisplayName() method
152     * to obtain the tag to display in XML output.
153     */

154
155     public abstract void endElement(int nameCode) throws TransformerException JavaDoc;
156
157     /**
158     * Output character data
159     */

160
161     public abstract void characters(char[] chars, int start, int len) throws TransformerException JavaDoc;
162
163     /**
164     * Output a processing instruction
165     */

166
167     public abstract void processingInstruction(String JavaDoc name, String JavaDoc data) throws TransformerException JavaDoc;
168     
169     /**
170     * Output a comment. <br>
171     * (The method signature is borrowed from the SAX2 LexicalHandler interface)
172     */

173
174     public abstract void comment (char[] chars, int start, int length) throws TransformerException JavaDoc;
175     
176     /**
177     * Switch escaping on or off. This is called when the XSLT disable-output-escaping attribute
178     * is used to switch escaping on or off. It is also called at the start and end of a CDATA section
179     * It is not called for other sections of output (e.g. comments) where escaping is inappropriate.
180     */

181
182     public void setEscaping(boolean escaping) throws TransformerException JavaDoc {}
183     
184     /**
185     * Set locator, to identify position in the document. Used only when supplying
186     * input from a parser.
187     */

188     
189     public void setDocumentLocator(Locator JavaDoc locator) {
190         this.locator = locator;
191     }
192
193     /**
194     * Set unparsed entity URI Used only when supplying
195     * input from a parser.
196     */

197     
198     public void setUnparsedEntity(String JavaDoc name, String JavaDoc uri) throws TransformerException JavaDoc {}
199
200     /**
201     * load a named output emitter or document handler and check it is OK.
202     */

203
204     public static Emitter makeEmitter (String JavaDoc className) throws TransformerException JavaDoc
205     {
206         Object JavaDoc handler = Loader.getInstance(className);
207
208         if (handler instanceof Emitter) {
209             return (Emitter)handler;
210         } else if (handler instanceof DocumentHandler JavaDoc) {
211             DocumentHandlerProxy emitter = new DocumentHandlerProxy();
212             emitter.setUnderlyingDocumentHandler((DocumentHandler JavaDoc)handler);
213             return emitter;
214         } else if (handler instanceof ContentHandler JavaDoc) {
215             ContentHandlerProxy emitter = new ContentHandlerProxy();
216             emitter.setUnderlyingContentHandler((ContentHandler JavaDoc)handler);
217             return emitter;
218         } else {
219             throw new TransformerException JavaDoc("Failed to load emitter " + className +
220                         ": it is not a SAX DocumentHandler or SAX2 ContentHandler");
221         }
222
223      }
224
225 }
226
227 //
228
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
229
// you may not use this file except in compliance with the License. You may obtain a copy of the
230
// License at http://www.mozilla.org/MPL/
231
//
232
// Software distributed under the License is distributed on an "AS IS" basis,
233
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
234
// See the License for the specific language governing rights and limitations under the License.
235
//
236
// The Original Code is: all this file.
237
//
238
// The Initial Developer of the Original Code is
239
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
240
//
241
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
242
//
243
// Contributor(s): none.
244
//
245
Popular Tags