KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.output;
2 import com.icl.saxon.*;
3 import org.xml.sax.Attributes JavaDoc;
4 import java.util.Properties JavaDoc;
5 import javax.xml.transform.TransformerException JavaDoc;
6 import javax.xml.transform.Result JavaDoc;
7 import javax.xml.transform.OutputKeys JavaDoc;
8 import javax.xml.transform.stream.StreamResult JavaDoc;
9 import javax.xml.transform.dom.DOMResult JavaDoc;
10 import javax.xml.transform.sax.SAXResult JavaDoc;
11 import java.io.*;
12
13 /**
14   * This class allows output to be generated. It channels output requests to an
15   * Emitter which does the actual writing. This is an abstract class, there are
16   * concrete implementions for XML output and text output.
17   *
18   * @author <A HREF="mailto:mhkay@iclway.co.uk>Michael H. Kay</A>
19   */

20
21 public abstract class Outputter {
22
23     protected Emitter emitter;
24
25     /**
26     * Get emitter. This is used by xsl:copy-of, a fragment is copied directly to the
27     * Emitter rather than going via the Outputter.
28     */

29     
30     public Emitter getEmitter() throws TransformerException JavaDoc {
31         reset();
32         return emitter;
33     }
34     
35     /**
36     * Synchronize the state of the Outputter with that of the underlying Emitter
37     */

38     
39     public abstract void reset() throws TransformerException JavaDoc;
40
41     public abstract Properties JavaDoc getOutputProperties();
42
43     /**
44     * Switch escaping (of special characters) on or off.
45     * @param escaping: true if special characters are to be escaped, false if not.
46     */

47
48     public final void setEscaping(boolean escaping) throws TransformerException JavaDoc {
49         emitter.setEscaping(escaping);
50     }
51
52     /**
53     * Start the output process
54     */

55
56     public final void open() throws TransformerException JavaDoc {
57         // System.err.println("Open " + this + " using emitter " + emitter.getClass());
58
emitter.startDocument();
59     }
60
61     /**
62     * Produce literal output. This is written as is, without any escaping.
63     * The method is provided for Java applications that wish to output literal HTML text.
64     * It is not used by the XSL system, which always writes using specific methods such as
65     * writeStartTag().
66     */

67
68     public abstract void write(String JavaDoc s) throws TransformerException JavaDoc;
69
70     /**
71     * Produce text content output. <BR>
72     * Special characters are escaped using XML/HTML conventions if the output format
73     * requires it.
74     * @param s The String to be output
75     * @exception TransformerException for any failure
76     */

77
78     public abstract void writeContent(String JavaDoc s) throws TransformerException JavaDoc;
79     
80     /**
81     * Produce text content output. <BR>
82     * Special characters are escaped using XML/HTML conventions if the output format
83     * requires it.
84     * @param chars Character array to be output
85     * @param start start position of characters to be output
86     * @param length number of characters to be output
87     * @exception TransformerException for any failure
88     */

89
90     public abstract void writeContent(char[] chars, int start, int length)
91         throws TransformerException JavaDoc;
92
93     /**
94     * Output an element start tag. <br>
95     * The actual output of the tag is deferred until all attributes have been output
96     * using writeAttribute().
97     * @param nameCode The element name code
98     */

99
100     public abstract void writeStartTag(int nameCode) throws TransformerException JavaDoc;
101
102     /**
103     * Check that the prefix for an attribute is acceptable, returning a substitute
104     * prefix if not. The prefix is acceptable unless a namespace declaration has been
105     * written that assignes this prefix to a different namespace URI. This method
106     * also checks that the attribute namespace has been declared, and declares it
107     * if not.
108     */

109     
110     public abstract int checkAttributePrefix(int nameCode) throws TransformerException JavaDoc;
111             
112     /**
113     * Output a namespace declaration. <br>
114     * This is added to a list of pending namespaces for the current start tag.
115     * If there is already another declaration of the same prefix, this one is
116     * ignored.
117     * Note that unlike SAX2 startPrefixMapping(), this call is made AFTER writing the start tag.
118     * @param nscode The namespace code
119     * @throws TransformerException if there is no start tag to write to (created using writeStartTag),
120     * or if character content has been written since the start tag was written.
121     */

122
123     public abstract void writeNamespaceDeclaration(int nscode)
124     throws TransformerException JavaDoc;
125
126     /**
127     * Copy a namespace node to the current element node
128     * (Rules defined in XSLT 1.0 errata)
129     */

130     
131     public abstract void copyNamespaceNode(int nscode) throws TransformerException JavaDoc;
132
133     /**
134     * Test whether there is an open start tag. This determines whether it is
135     * possible to write an attribute node at this point.
136     */

137     
138     public abstract boolean thereIsAnOpenStartTag();
139
140     /**
141     * Output an attribute value. <br>
142     * This is added to a list of pending attributes for the current start tag, overwriting
143     * any previous attribute with the same name. <br>
144     * This method should NOT be used to output namespace declarations.
145     * @param nameCode The name code of the attribute
146     * @param value The value of the attribute
147     * @throws TransformerException if there is no start tag to write to (created using writeStartTag),
148     * or if character content has been written since the start tag was written.
149     */

150
151     public void writeAttribute(int nameCode, String JavaDoc value) throws TransformerException JavaDoc {
152         writeAttribute(nameCode, value, false);
153     }
154
155     /**
156     * Output an attribute value. <br>
157     * This is added to a list of pending attributes for the current start tag, overwriting
158     * any previous attribute with the same name. <br>
159     * This method should NOT be used to output namespace declarations.<br>
160     * Before calling this, checkAttributePrefix() should be called to ensure the namespace
161     * is OK.
162     * @param name The name of the attribute
163     * @param value The value of the attribute
164     * @param noEscape True if it's known there are no special characters in the value. If
165     * unsure, set this to false.
166     * @throws TransformerException if there is no start tag to write to (created using writeStartTag),
167     * or if character content has been written since the start tag was written.
168     */

169
170     public abstract void writeAttribute(int nameCode, String JavaDoc value, boolean noEscape)
171     throws TransformerException JavaDoc;
172
173
174     /**
175     * Output an element end tag.<br>
176     * @param nameCode The element name code
177     */

178
179     public abstract void writeEndTag(int nameCode) throws TransformerException JavaDoc;
180
181     /**
182     * Write a comment
183     */

184
185     public abstract void writeComment(String JavaDoc comment) throws TransformerException JavaDoc;
186
187     /**
188     * Write a processing instruction
189     */

190
191     public abstract void writePI(String JavaDoc target, String JavaDoc data) throws TransformerException JavaDoc;
192
193     /**
194     * Close the output
195     */

196
197     public abstract void close() throws TransformerException JavaDoc;
198
199
200 }
201
202 //
203
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
204
// you may not use this file except in compliance with the License. You may obtain a copy of the
205
// License at http://www.mozilla.org/MPL/
206
//
207
// Software distributed under the License is distributed on an "AS IS" basis,
208
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
209
// See the License for the specific language governing rights and limitations under the License.
210
//
211
// The Original Code is: all this file.
212
//
213
// The Initial Developer of the Original Code is
214
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
215
//
216
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
217
//
218
// Contributor(s): none.
219
//
220
Popular Tags