001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: TransletOutputHandlerFactory.java 475979 2006-11-16 23:32:48Z minchau $
020 */
021
022 package org.apache.xalan.xsltc.runtime.output;
023
024 import java.io.IOException;
025 import java.io.OutputStream;
026 import java.io.Writer;
027
028 import javax.xml.parsers.ParserConfigurationException;
029
030 import org.apache.xalan.xsltc.trax.SAX2DOM;
031 import org.apache.xml.serializer.ToHTMLStream;
032 import org.apache.xml.serializer.ToTextStream;
033 import org.apache.xml.serializer.ToUnknownStream;
034 import org.apache.xml.serializer.ToXMLSAXHandler;
035 import org.apache.xml.serializer.ToXMLStream;
036 import org.apache.xml.serializer.SerializationHandler;
037 import org.w3c.dom.Node;
038
039 import org.xml.sax.ContentHandler;
040 import org.xml.sax.ext.LexicalHandler;
041
042 /**
043 * @author Santiago Pericas-Geertsen
044 */
045 public class TransletOutputHandlerFactory {
046
047 public static final int STREAM = 0;
048 public static final int SAX = 1;
049 public static final int DOM = 2;
050
051 private String _encoding = "utf-8";
052 private String _method = null;
053 private int _outputType = STREAM;
054 private OutputStream _ostream = System.out;
055 private Writer _writer = null;
056 private Node _node = null;
057 private Node _nextSibling = null;
058 private int _indentNumber = -1;
059 private ContentHandler _handler = null;
060 private LexicalHandler _lexHandler = null;
061
062 static public TransletOutputHandlerFactory newInstance() {
063 return new TransletOutputHandlerFactory();
064 }
065
066 public void setOutputType(int outputType) {
067 _outputType = outputType;
068 }
069
070 public void setEncoding(String encoding) {
071 if (encoding != null) {
072 _encoding = encoding;
073 }
074 }
075
076 public void setOutputMethod(String method) {
077 _method = method;
078 }
079
080 public void setOutputStream(OutputStream ostream) {
081 _ostream = ostream;
082 }
083
084 public void setWriter(Writer writer) {
085 _writer = writer;
086 }
087
088 public void setHandler(ContentHandler handler) {
089 _handler = handler;
090 }
091
092 public void setLexicalHandler(LexicalHandler lex) {
093 _lexHandler = lex;
094 }
095
096 public void setNode(Node node) {
097 _node = node;
098 }
099
100 public Node getNode() {
101 return (_handler instanceof SAX2DOM) ? ((SAX2DOM)_handler).getDOM()
102 : null;
103 }
104
105 public void setNextSibling(Node nextSibling) {
106 _nextSibling = nextSibling;
107 }
108
109 public void setIndentNumber(int value) {
110 _indentNumber = value;
111 }
112
113 public SerializationHandler getSerializationHandler()
114 throws IOException, ParserConfigurationException
115 {
116 SerializationHandler result = null;
117 switch (_outputType)
118 {
119 case STREAM :
120
121 if (_method == null)
122 {
123 result = new ToUnknownStream();
124 }
125 else if (_method.equalsIgnoreCase("xml"))
126 {
127
128 result = new ToXMLStream();
129
130 }
131 else if (_method.equalsIgnoreCase("html"))
132 {
133
134 result = new ToHTMLStream();
135
136 }
137 else if (_method.equalsIgnoreCase("text"))
138 {
139
140 result = new ToTextStream();
141
142 }
143
144 if (result != null && _indentNumber >= 0)
145 {
146 result.setIndentAmount(_indentNumber);
147 }
148
149 result.setEncoding(_encoding);
150
151 if (_writer != null)
152 {
153 result.setWriter(_writer);
154 }
155 else
156 {
157 result.setOutputStream(_ostream);
158 }
159 return result;
160
161 case DOM :
162 _handler = (_node != null) ? new SAX2DOM(_node, _nextSibling) : new SAX2DOM();
163 _lexHandler = (LexicalHandler) _handler;
164 // falls through
165 case SAX :
166 if (_method == null)
167 {
168 _method = "xml"; // default case
169 }
170
171 if (_lexHandler == null)
172 {
173 result = new ToXMLSAXHandler(_handler, _encoding);
174 }
175 else
176 {
177 result =
178 new ToXMLSAXHandler(
179 _handler,
180 _lexHandler,
181 _encoding);
182 }
183
184 return result;
185 }
186 return null;
187 }
188
189 }