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