KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.output;
2
3 import com.icl.saxon.*;
4 import com.icl.saxon.om.Namespace;
5 import java.util.*;
6 import java.io.Writer JavaDoc;
7 import org.xml.sax.Attributes JavaDoc;
8 import javax.xml.transform.OutputKeys JavaDoc;
9 import javax.xml.transform.TransformerException JavaDoc;
10
11 /**
12   * This class generates XML or HTML output depending on whether the first tag output is "<html>"
13   * @author <A HREF="mailto:mhkay@iclway.co.uk>Michael H. Kay</A>
14   */

15
16 public class UncommittedEmitter extends ProxyEmitter {
17
18     boolean committed = false;
19     boolean initialNewline = false;
20     boolean initialEscaping = true;
21     StringBuffer JavaDoc pendingCharacters;
22
23     public void startDocument() throws TransformerException JavaDoc {
24         committed = false;
25     }
26
27     /**
28     * End of document
29     */

30
31     public void endDocument() throws TransformerException JavaDoc {
32         // empty output: must send a beginDocument()/endDocument() pair to the content handler
33
if (!committed) {
34             switchToXML();
35         }
36         super.endDocument();
37     }
38
39
40
41     /**
42     * Produce character output using the current Writer. <BR>
43     */

44
45     public void characters(char ch[], int start, int length) throws TransformerException JavaDoc {
46         if (!committed) {
47             boolean allWhite = true;
48             if (pendingCharacters==null) {
49                 pendingCharacters=new StringBuffer JavaDoc();
50             }
51             for (int i=start; i<start+length; i++) {
52                 char c = ch[i];
53                 if (!Character.isWhitespace(c)) {
54                     allWhite = false;
55                 }
56                 if (initialEscaping) {
57                     if (c=='<') {
58                         pendingCharacters.append("&lt;");
59                     } else if (c=='>') {
60                         pendingCharacters.append("&gt;");
61                     } else if (c=='&') {
62                         pendingCharacters.append("&amp;");
63                     } else {
64                         pendingCharacters.append(c);
65                     }
66                 } else {
67                     pendingCharacters.append(c);
68                 }
69             }
70             if (!allWhite) {
71                 switchToXML();
72             }
73         } else {
74             super.characters(ch, start, length);
75         }
76     }
77     
78     /**
79     * Processing Instruction
80     */

81
82     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws TransformerException JavaDoc {
83         if (!committed) {
84             if (pendingCharacters==null) {
85                 pendingCharacters=new StringBuffer JavaDoc();
86             }
87             pendingCharacters.append("<?" + target + " " + data + "?>");
88         } else {
89             super.processingInstruction(target, data);
90         }
91     }
92
93     /**
94     * Output a comment
95     */

96
97     public void comment (char ch[], int start, int length) throws TransformerException JavaDoc {
98         if (!committed) {
99             if (pendingCharacters==null) {
100                 pendingCharacters=new StringBuffer JavaDoc();
101             }
102             pendingCharacters.append("<!--" + new String JavaDoc(ch, start, length) + "-->");
103         } else {
104             super.comment(ch, start, length);
105         }
106     }
107
108     /**
109     * Output an element start tag. <br>
110     * This can only be called once: it switches to a substitute output generator for XML or HTML,
111     * depending on whether the tag is "HTML".
112     * @param name The element name (tag)
113     */

114
115     public void startElement(int nameCode, Attributes JavaDoc attributes,
116                              int[] namespaces, int nscount) throws TransformerException JavaDoc {
117         if (!committed) {
118             String JavaDoc name = namePool.getLocalName(nameCode);
119             short uriCode = namePool.getURICode(nameCode);
120             if (name.equalsIgnoreCase("html") && uriCode==Namespace.NULL_CODE) {
121                 switchToHTML();
122             } else {
123                 switchToXML();
124             }
125         }
126         super.startElement(nameCode, attributes, namespaces, nscount);
127     }
128
129     /**
130     * Switch to an XML emitter
131     */

132
133     private void switchToXML() throws TransformerException JavaDoc {
134         Emitter e = new XMLEmitter();
135         String JavaDoc indent = outputProperties.getProperty(OutputKeys.INDENT);
136         if (indent!=null && indent.equals("yes")) {
137             XMLIndenter in = new XMLIndenter();
138             in.setUnderlyingEmitter(e);
139             e = in;
140         }
141         String JavaDoc cdata = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS);
142         if (cdata!=null && cdata.length()>0) {
143             CDATAFilter filter = new CDATAFilter();
144             filter.setUnderlyingEmitter(e);
145             e=filter;
146         }
147         switchTo(e);
148     }
149
150     /**
151     * Switch to an HTML emitter
152     */

153
154     private void switchToHTML() throws TransformerException JavaDoc {
155         Emitter e = new HTMLEmitter();
156         String JavaDoc indent = outputProperties.getProperty(OutputKeys.INDENT);
157         if (indent==null || indent.equals("yes")) {
158             HTMLIndenter in = new HTMLIndenter();
159             in.setUnderlyingEmitter(e);
160             e = in;
161         }
162         switchTo(e);
163     }
164
165     /**
166     * Switch escaping on or off. This is called when the XSLT disable-output-escaping attribute
167     * is used to switch escaping on or off. It is not called for other sections of output (e.g.
168     * element names) where escaping is inappropriate.
169     */

170
171     public void setEscaping(boolean escaping) throws TransformerException JavaDoc {
172         if (!committed) {
173             initialEscaping = escaping;
174         }
175         super.setEscaping(escaping);
176     }
177         
178     /**
179     * Switch to a new underlying emitter
180     */

181
182     private void switchTo(Emitter emitter) throws TransformerException JavaDoc {
183         setUnderlyingEmitter(emitter);
184         committed = true;
185         emitter.setWriter(writer);
186         emitter.setOutputProperties(outputProperties);
187         emitter.startDocument();
188         if (pendingCharacters!=null) {
189             emitter.setEscaping(false);
190             int len = pendingCharacters.length();
191             char[] chars = new char[len];
192             pendingCharacters.getChars(0, len, chars, 0);
193             emitter.characters(chars, 0, len);
194         }
195         emitter.setEscaping(initialEscaping);
196     }
197
198 }
199
200 //
201
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
202
// you may not use this file except in compliance with the License. You may obtain a copy of the
203
// License at http://www.mozilla.org/MPL/
204
//
205
// Software distributed under the License is distributed on an "AS IS" basis,
206
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
207
// See the License for the specific language governing rights and limitations under the License.
208
//
209
// The Original Code is: all this file.
210
//
211
// The Initial Developer of the Original Code is
212
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
213
//
214
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
215
//
216
// Contributor(s): none.
217
//
218
Popular Tags