KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > runtime > output > TransletOutputHandlerFactory


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: TransletOutputHandlerFactory.java,v 1.16 2004/02/16 22:56:25 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.runtime.output;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.Writer JavaDoc;
25
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27
28 import org.apache.xalan.xsltc.trax.SAX2DOM;
29 import org.apache.xml.serializer.ToHTMLSAXHandler;
30 import org.apache.xml.serializer.ToHTMLStream;
31 import org.apache.xml.serializer.ToTextSAXHandler;
32 import org.apache.xml.serializer.ToTextStream;
33 import org.apache.xml.serializer.ToUnknownStream;
34 import org.apache.xml.serializer.ToXMLSAXHandler;
35 import org.apache.xml.serializer.ToXMLStream;
36 import org.apache.xml.serializer.SerializationHandler;
37 import org.w3c.dom.Node JavaDoc;
38
39 import org.xml.sax.ContentHandler JavaDoc;
40 import org.xml.sax.ext.LexicalHandler JavaDoc;
41
42 /**
43  * @author Santiago Pericas-Geertsen
44  */

45 public class TransletOutputHandlerFactory {
46
47     public static final int STREAM = 0;
48     public static final int SAX = 1;
49     public static final int DOM = 2;
50
51     private String JavaDoc _encoding = "utf-8";
52     private String JavaDoc _method = null;
53     private int _outputType = STREAM;
54     private OutputStream JavaDoc _ostream = System.out;
55     private Writer JavaDoc _writer = null;
56     private Node JavaDoc _node = null;
57     private int _indentNumber = -1;
58     private ContentHandler JavaDoc _handler = null;
59     private LexicalHandler JavaDoc _lexHandler = null;
60
61     static public TransletOutputHandlerFactory newInstance() {
62     return new TransletOutputHandlerFactory();
63     }
64
65     public void setOutputType(int outputType) {
66     _outputType = outputType;
67     }
68
69     public void setEncoding(String JavaDoc encoding) {
70     if (encoding != null) {
71         _encoding = encoding;
72     }
73     }
74
75     public void setOutputMethod(String JavaDoc method) {
76     _method = method;
77     }
78
79     public void setOutputStream(OutputStream JavaDoc ostream) {
80     _ostream = ostream;
81     }
82
83     public void setWriter(Writer JavaDoc writer) {
84     _writer = writer;
85     }
86
87     public void setHandler(ContentHandler JavaDoc handler) {
88         _handler = handler;
89     }
90
91     public void setLexicalHandler(LexicalHandler JavaDoc lex) {
92     _lexHandler = lex;
93     }
94
95     public void setNode(Node JavaDoc node) {
96     _node = node;
97     }
98
99     public Node JavaDoc getNode() {
100     return (_handler instanceof SAX2DOM) ? ((SAX2DOM)_handler).getDOM()
101        : null;
102     }
103
104     public void setIndentNumber(int value) {
105     _indentNumber = value;
106     }
107
108     public SerializationHandler getSerializationHandler()
109         throws IOException JavaDoc, ParserConfigurationException JavaDoc
110     {
111         SerializationHandler result = null;
112         switch (_outputType)
113         {
114             case STREAM :
115
116                 if (_method == null)
117                 {
118                     result = new ToUnknownStream();
119                 }
120                 else if (_method.equalsIgnoreCase("xml"))
121                 {
122
123                     result = new ToXMLStream();
124
125                 }
126                 else if (_method.equalsIgnoreCase("html"))
127                 {
128
129                     result = new ToHTMLStream();
130
131                 }
132                 else if (_method.equalsIgnoreCase("text"))
133                 {
134
135                     result = new ToTextStream();
136
137                 }
138
139                 if (result != null && _indentNumber >= 0)
140                 {
141                     result.setIndentAmount(_indentNumber);
142                 }
143
144                 result.setEncoding(_encoding);
145
146                 if (_writer != null)
147                 {
148                     result.setWriter(_writer);
149                 }
150                 else
151                 {
152                     result.setOutputStream(_ostream);
153                 }
154                 return result;
155
156             case DOM :
157                 _handler = (_node != null) ? new SAX2DOM(_node) : new SAX2DOM();
158                 _lexHandler = (LexicalHandler JavaDoc) _handler;
159                 // falls through
160
case SAX :
161                 if (_method == null)
162                 {
163                     _method = "xml"; // default case
164
}
165
166                 if (_method.equalsIgnoreCase("xml"))
167                 {
168
169                     if (_lexHandler == null)
170                     {
171                         result = new ToXMLSAXHandler(_handler, _encoding);
172                     }
173                     else
174                     {
175                         result =
176                             new ToXMLSAXHandler(
177                                 _handler,
178                                 _lexHandler,
179                                 _encoding);
180                     }
181
182                 }
183                 else if (_method.equalsIgnoreCase("html"))
184                 {
185
186                     if (_lexHandler == null)
187                     {
188                         result = new ToHTMLSAXHandler(_handler, _encoding);
189                     }
190                     else
191                     {
192                         result =
193                             new ToHTMLSAXHandler(
194                                 _handler,
195                                 _lexHandler,
196                                 _encoding);
197                     }
198
199                 }
200                 else if (_method.equalsIgnoreCase("text"))
201                 {
202
203                     if (_lexHandler == null)
204                     {
205                         result = new ToTextSAXHandler(_handler, _encoding);
206                     }
207                     else
208                     {
209                         result =
210                             new ToTextSAXHandler(
211                                 _handler,
212                                 _lexHandler,
213                                 _encoding);
214                     }
215
216                 }
217                 return result;
218         }
219         return null;
220     }
221
222 }
223
Popular Tags