KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.output;
2 import com.icl.saxon.*;
3 import java.util.Properties JavaDoc;
4 import javax.xml.transform.TransformerException JavaDoc;
5 import javax.xml.transform.ErrorListener JavaDoc;
6
7 /**
8   * This class allows output to be generated. It channels output requests to an
9   * Emitter which does the actual writing. This is a specialized and simplified version
10   * that is used to handle xsl:attribute, xsl:comment, and xsl:processing-instruction.
11   *
12   * @author <A HREF="mailto:mhkay@iclway.co.uk>Michael H. Kay</A>
13   */

14
15 public final class StringOutputter extends Outputter {
16
17     StringBuffer JavaDoc buffer;
18     int ignoreElements = 0;
19     ErrorListener JavaDoc errorListener = null;
20
21     public StringOutputter(StringBuffer JavaDoc buffer) {
22         this.buffer = buffer;
23         // we need an Emitter to support xsl:copy-of; but they share the same string buffer.
24
emitter = new StringEmitter(buffer);
25     }
26     
27     public void setErrorListener(ErrorListener JavaDoc listener) {
28         errorListener = listener;
29     }
30     
31     public void reset() throws TransformerException JavaDoc {
32         // no-op
33
}
34
35
36     public Properties JavaDoc getOutputProperties() {
37         return TextFragment.getProperties();
38     }
39
40     /**
41     * Produce literal output. This is written as is, without any escaping.
42     * The method is provided for Java applications that wish to output literal HTML text.
43     * It is not used by the XSL system, which always writes using specific methods such as
44     * writeStartTag().
45     */

46
47     public void write(String JavaDoc s) throws TransformerException JavaDoc {
48         if (ignoreElements==0) {
49             buffer.append(s);
50         }
51     }
52
53     /**
54     * Produce text content output. <BR>
55     * Special characters are escaped using XML/HTML conventions if the output format
56     * requires it.
57     * @param s The String to be output
58     * @exception TransformerException for any failure
59     */

60
61     public void writeContent(String JavaDoc s) throws TransformerException JavaDoc {
62         if (s==null) return;
63         if (ignoreElements==0) {
64             buffer.append(s);
65         }
66     }
67     
68     /**
69     * Produce text content output. <BR>
70     * Special characters are escaped using XML/HTML conventions if the output format
71     * requires it.
72     * @param chars Character array to be output
73     * @param start start position of characters to be output
74     * @param length number of characters to be output
75     * @exception TransformerException for any failure
76     */

77
78     public void writeContent(char[] chars, int start, int length) throws TransformerException JavaDoc {
79         if (ignoreElements==0) {
80             buffer.append(chars, start, length);
81         }
82     }
83
84     /**
85     * Output an element start tag. With this outputter, this is a recoverable error.<br>
86     * @param nameCode The element name code
87     */

88
89     public void writeStartTag(int nameCode) throws TransformerException JavaDoc {
90         reportRecoverableError();
91         ignoreElements++;
92     }
93     
94     private void reportRecoverableError() throws TransformerException JavaDoc {
95         if (errorListener!=null) {
96             errorListener.warning(
97                 new TransformerException JavaDoc(
98                 "Non-text output nodes are ignored when writing an attribute, comment, or PI"));
99         }
100     }
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 int checkAttributePrefix(int nameCode) throws TransformerException JavaDoc {
111         return nameCode;
112     }
113             
114     /**
115     * Output a namespace declaration. <br>
116     * This is added to a list of pending namespaces for the current start tag.
117     * If there is already another declaration of the same prefix, this one is
118     * ignored.
119     * Note that unlike SAX2 startPrefixMapping(), this call is made AFTER writing the start tag.
120     * @param nscode The namespace code
121     * @throws TransformerException if there is no start tag to write to (created using writeStartTag),
122     * or if character content has been written since the start tag was written.
123     */

124
125     public void writeNamespaceDeclaration(int nscode)
126     throws TransformerException JavaDoc {
127         // no-op
128
}
129
130     /**
131     * Copy a namespace node to the current element node
132     * (Rules defined in XSLT 1.0 errata)
133     */

134     
135     public void copyNamespaceNode(int nscode) throws TransformerException JavaDoc {
136         // no-op
137
}
138
139     /**
140     * Test whether there is an open start tag. This determines whether it is
141     * possible to write an attribute node at this point.
142     */

143     
144     public boolean thereIsAnOpenStartTag() {
145         return false;
146     }
147
148     /**
149     * Output an attribute value. <br>
150     * No-op in this implementation.
151     * @param name The name of the attribute
152     * @param value The value of the attribute
153     * @param noEscape True if it's known there are no special characters in the value. If
154     * unsure, set this to false.
155     * @throws TransformerException if there is no start tag to write to (created using writeStartTag),
156     * or if character content has been written since the start tag was written.
157     */

158
159     public void writeAttribute(int nameCode, String JavaDoc value, boolean noEscape) throws TransformerException JavaDoc {
160         reportRecoverableError();
161     }
162
163
164     /**
165     * Output an element end tag.<br>
166     * @param nameCode The element name code
167     */

168
169     public void writeEndTag(int nameCode) throws TransformerException JavaDoc {
170         ignoreElements--;
171     }
172
173     /**
174     * Write a comment.
175     * No-op in this implementation
176     */

177
178     public void writeComment(String JavaDoc comment) throws TransformerException JavaDoc {
179         reportRecoverableError();
180     }
181
182     /**
183     * Write a processing instruction
184     * No-op in this implementation
185     */

186
187     public void writePI(String JavaDoc target, String JavaDoc data) throws TransformerException JavaDoc {
188         reportRecoverableError();
189     }
190
191     /**
192     * Close the output
193     */

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