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