View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: TransletOutputHandlerFactory.java,v 1.2.4.2 2005/09/15 19:12:05 jeffsuttor Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.runtime.output;
25  
26  import java.io.IOException;
27  import java.io.OutputStream;
28  import java.io.Writer;
29  
30  import javax.xml.parsers.ParserConfigurationException;
31  import javax.xml.stream.XMLEventWriter;
32  import javax.xml.stream.util.XMLEventConsumer;
33  import javax.xml.stream.XMLStreamWriter;
34  
35  import com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM;
36  import com.sun.org.apache.xalan.internal.xsltc.trax.SAX2StAXEventWriter;
37  import com.sun.org.apache.xalan.internal.xsltc.trax.SAX2StAXStreamWriter;
38  
39  import com.sun.org.apache.xml.internal.serializer.ToHTMLSAXHandler;
40  import com.sun.org.apache.xml.internal.serializer.ToHTMLStream;
41  import com.sun.org.apache.xml.internal.serializer.ToTextSAXHandler;
42  import com.sun.org.apache.xml.internal.serializer.ToTextStream;
43  import com.sun.org.apache.xml.internal.serializer.ToUnknownStream;
44  import com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler;
45  import com.sun.org.apache.xml.internal.serializer.ToXMLStream;
46  import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
47  import org.w3c.dom.Node;
48  
49  import org.xml.sax.ContentHandler;
50  import org.xml.sax.ext.LexicalHandler;
51  
52  /**
53   * @author Santiago Pericas-Geertsen
54   */
55  public class TransletOutputHandlerFactory {
56  
57      public static final int STREAM = 0;
58      public static final int SAX    = 1;
59      public static final int DOM    = 2;
60      public static final int STAX   = 3;
61  
62      private String _encoding                        = "utf-8";
63      private String _method                          = null;
64      private int    _outputType                      = STREAM;
65      private OutputStream _ostream                   = System.out;
66      private Writer _writer                          = null;
67      private Node _node                              = null;
68      private Node   _nextSibling                     = null;
69      private XMLEventWriter _xmlStAXEventWriter      = null;
70      private XMLStreamWriter _xmlStAXStreamWriter    = null;
71      private int _indentNumber                       = -1;
72      private ContentHandler _handler                 = null;
73      private LexicalHandler _lexHandler              = null;
74  
75      private boolean _useServicesMechanism;
76  
77      static public TransletOutputHandlerFactory newInstance() {
78          return new TransletOutputHandlerFactory(true);
79      }
80      static public TransletOutputHandlerFactory newInstance(boolean useServicesMechanism) {
81          return new TransletOutputHandlerFactory(useServicesMechanism);
82      }
83  
84      public TransletOutputHandlerFactory(boolean useServicesMechanism) {
85          _useServicesMechanism = useServicesMechanism;
86      }
87      public void setOutputType(int outputType) {
88          _outputType = outputType;
89      }
90  
91      public void setEncoding(String encoding) {
92          if (encoding != null) {
93              _encoding = encoding;
94          }
95      }
96  
97      public void setOutputMethod(String method) {
98          _method = method;
99      }
100 
101     public void setOutputStream(OutputStream ostream) {
102         _ostream = ostream;
103     }
104 
105     public void setWriter(Writer writer) {
106         _writer = writer;
107     }
108 
109     public void setHandler(ContentHandler handler) {
110         _handler = handler;
111     }
112 
113     public void setLexicalHandler(LexicalHandler lex) {
114         _lexHandler = lex;
115     }
116 
117     public void setNode(Node node) {
118         _node = node;
119     }
120 
121     public Node getNode() {
122         return (_handler instanceof SAX2DOM) ? ((SAX2DOM)_handler).getDOM()
123            : null;
124     }
125 
126     public void setNextSibling(Node nextSibling) {
127         _nextSibling = nextSibling;
128     }
129 
130     public XMLEventWriter getXMLEventWriter() {
131         return (_handler instanceof SAX2StAXEventWriter) ? ((SAX2StAXEventWriter) _handler).getEventWriter() : null;
132     }
133 
134     public void setXMLEventWriter(XMLEventWriter eventWriter) {
135         _xmlStAXEventWriter = eventWriter;
136     }
137 
138     public XMLStreamWriter getXMLStreamWriter() {
139         return (_handler instanceof SAX2StAXStreamWriter) ? ((SAX2StAXStreamWriter) _handler).getStreamWriter() : null;
140     }
141 
142     public void setXMLStreamWriter(XMLStreamWriter streamWriter) {
143         _xmlStAXStreamWriter = streamWriter;
144     }
145 
146     public void setIndentNumber(int value) {
147         _indentNumber = value;
148     }
149 
150     public SerializationHandler getSerializationHandler()
151         throws IOException, ParserConfigurationException
152     {
153         SerializationHandler result = null;
154         switch (_outputType)
155         {
156             case STREAM :
157 
158                 if (_method == null)
159                 {
160                     result = new ToUnknownStream();
161                 }
162                 else if (_method.equalsIgnoreCase("xml"))
163                 {
164 
165                     result = new ToXMLStream();
166 
167                 }
168                 else if (_method.equalsIgnoreCase("html"))
169                 {
170 
171                     result = new ToHTMLStream();
172 
173                 }
174                 else if (_method.equalsIgnoreCase("text"))
175                 {
176 
177                     result = new ToTextStream();
178 
179                 }
180 
181                 if (result != null && _indentNumber >= 0)
182                 {
183                     result.setIndentAmount(_indentNumber);
184                 }
185 
186                 result.setEncoding(_encoding);
187 
188                 if (_writer != null)
189                 {
190                     result.setWriter(_writer);
191                 }
192                 else
193                 {
194                     result.setOutputStream(_ostream);
195                 }
196                 return result;
197 
198             case DOM :
199                 _handler = (_node != null) ? new SAX2DOM(_node, _nextSibling, _useServicesMechanism) : new SAX2DOM(_useServicesMechanism);
200                 _lexHandler = (LexicalHandler) _handler;
201                 // falls through
202             case STAX :
203                 if (_xmlStAXEventWriter != null) {
204                     _handler =  new SAX2StAXEventWriter(_xmlStAXEventWriter);
205                 } else if (_xmlStAXStreamWriter != null) {
206                     _handler =  new SAX2StAXStreamWriter(_xmlStAXStreamWriter);
207                 }
208                 _lexHandler = (LexicalHandler) _handler;
209                 // again falls through - Padmaja Vedula
210             case SAX :
211                 if (_method == null)
212                 {
213                     _method = "xml"; // default case
214                 }
215 
216                 if (_method.equalsIgnoreCase("xml"))
217                 {
218 
219                     if (_lexHandler == null)
220                     {
221                         result = new ToXMLSAXHandler(_handler, _encoding);
222                     }
223                     else
224                     {
225                         result =
226                             new ToXMLSAXHandler(
227                                 _handler,
228                                 _lexHandler,
229                                 _encoding);
230                     }
231 
232                 }
233                 else if (_method.equalsIgnoreCase("html"))
234                 {
235 
236                     if (_lexHandler == null)
237                     {
238                         result = new ToHTMLSAXHandler(_handler, _encoding);
239                     }
240                     else
241                     {
242                         result =
243                             new ToHTMLSAXHandler(
244                                 _handler,
245                                 _lexHandler,
246                                 _encoding);
247                     }
248 
249                 }
250                 else if (_method.equalsIgnoreCase("text"))
251                 {
252 
253                     if (_lexHandler == null)
254                     {
255                         result = new ToTextSAXHandler(_handler, _encoding);
256                     }
257                     else
258                     {
259                         result =
260                             new ToTextSAXHandler(
261                                 _handler,
262                                 _lexHandler,
263                                 _encoding);
264                     }
265 
266                 }
267                 return result;
268         }
269         return null;
270     }
271 
272 }