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. package com.sun.org.apache.xalan.internal.xsltc.runtime.output;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.io.Writer;
  23. import javax.xml.parsers.ParserConfigurationException;
  24. import com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM;
  25. import com.sun.org.apache.xml.internal.serializer.ToHTMLSAXHandler;
  26. import com.sun.org.apache.xml.internal.serializer.ToHTMLStream;
  27. import com.sun.org.apache.xml.internal.serializer.ToTextSAXHandler;
  28. import com.sun.org.apache.xml.internal.serializer.ToTextStream;
  29. import com.sun.org.apache.xml.internal.serializer.ToUnknownStream;
  30. import com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler;
  31. import com.sun.org.apache.xml.internal.serializer.ToXMLStream;
  32. import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
  33. import org.w3c.dom.Node;
  34. import org.xml.sax.ContentHandler;
  35. import org.xml.sax.ext.LexicalHandler;
  36. /**
  37. * @author Santiago Pericas-Geertsen
  38. */
  39. public class TransletOutputHandlerFactory {
  40. public static final int STREAM = 0;
  41. public static final int SAX = 1;
  42. public static final int DOM = 2;
  43. private String _encoding = "utf-8";
  44. private String _method = null;
  45. private int _outputType = STREAM;
  46. private OutputStream _ostream = System.out;
  47. private Writer _writer = null;
  48. private Node _node = null;
  49. private int _indentNumber = -1;
  50. private ContentHandler _handler = null;
  51. private LexicalHandler _lexHandler = null;
  52. static public TransletOutputHandlerFactory newInstance() {
  53. return new TransletOutputHandlerFactory();
  54. }
  55. public void setOutputType(int outputType) {
  56. _outputType = outputType;
  57. }
  58. public void setEncoding(String encoding) {
  59. if (encoding != null) {
  60. _encoding = encoding;
  61. }
  62. }
  63. public void setOutputMethod(String method) {
  64. _method = method;
  65. }
  66. public void setOutputStream(OutputStream ostream) {
  67. _ostream = ostream;
  68. }
  69. public void setWriter(Writer writer) {
  70. _writer = writer;
  71. }
  72. public void setHandler(ContentHandler handler) {
  73. _handler = handler;
  74. }
  75. public void setLexicalHandler(LexicalHandler lex) {
  76. _lexHandler = lex;
  77. }
  78. public void setNode(Node node) {
  79. _node = node;
  80. }
  81. public Node getNode() {
  82. return (_handler instanceof SAX2DOM) ? ((SAX2DOM)_handler).getDOM()
  83. : null;
  84. }
  85. public void setIndentNumber(int value) {
  86. _indentNumber = value;
  87. }
  88. public SerializationHandler getSerializationHandler()
  89. throws IOException, ParserConfigurationException
  90. {
  91. SerializationHandler result = null;
  92. switch (_outputType)
  93. {
  94. case STREAM :
  95. if (_method == null)
  96. {
  97. result = new ToUnknownStream();
  98. }
  99. else if (_method.equalsIgnoreCase("xml"))
  100. {
  101. result = new ToXMLStream();
  102. }
  103. else if (_method.equalsIgnoreCase("html"))
  104. {
  105. result = new ToHTMLStream();
  106. }
  107. else if (_method.equalsIgnoreCase("text"))
  108. {
  109. result = new ToTextStream();
  110. }
  111. if (result != null && _indentNumber >= 0)
  112. {
  113. result.setIndentAmount(_indentNumber);
  114. }
  115. result.setEncoding(_encoding);
  116. if (_writer != null)
  117. {
  118. result.setWriter(_writer);
  119. }
  120. else
  121. {
  122. result.setOutputStream(_ostream);
  123. }
  124. return result;
  125. case DOM :
  126. _handler = (_node != null) ? new SAX2DOM(_node) : new SAX2DOM();
  127. _lexHandler = (LexicalHandler) _handler;
  128. // falls through
  129. case SAX :
  130. if (_method == null)
  131. {
  132. _method = "xml"; // default case
  133. }
  134. if (_method.equalsIgnoreCase("xml"))
  135. {
  136. if (_lexHandler == null)
  137. {
  138. result = new ToXMLSAXHandler(_handler, _encoding);
  139. }
  140. else
  141. {
  142. result =
  143. new ToXMLSAXHandler(
  144. _handler,
  145. _lexHandler,
  146. _encoding);
  147. }
  148. }
  149. else if (_method.equalsIgnoreCase("html"))
  150. {
  151. if (_lexHandler == null)
  152. {
  153. result = new ToHTMLSAXHandler(_handler, _encoding);
  154. }
  155. else
  156. {
  157. result =
  158. new ToHTMLSAXHandler(
  159. _handler,
  160. _lexHandler,
  161. _encoding);
  162. }
  163. }
  164. else if (_method.equalsIgnoreCase("text"))
  165. {
  166. if (_lexHandler == null)
  167. {
  168. result = new ToTextSAXHandler(_handler, _encoding);
  169. }
  170. else
  171. {
  172. result =
  173. new ToTextSAXHandler(
  174. _handler,
  175. _lexHandler,
  176. _encoding);
  177. }
  178. }
  179. return result;
  180. }
  181. return null;
  182. }
  183. }