Save This Page
Home » openjdk-7 » com.sun.org.apache.xalan » internal » xsltc » runtime » output » [javadoc | source]
    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       static public TransletOutputHandlerFactory newInstance() {
   76           return new TransletOutputHandlerFactory();
   77       }
   78   
   79       public void setOutputType(int outputType) {
   80           _outputType = outputType;
   81       }
   82   
   83       public void setEncoding(String encoding) {
   84           if (encoding != null) {
   85               _encoding = encoding;
   86           }
   87       }
   88   
   89       public void setOutputMethod(String method) {
   90           _method = method;
   91       }
   92   
   93       public void setOutputStream(OutputStream ostream) {
   94           _ostream = ostream;
   95       }
   96   
   97       public void setWriter(Writer writer) {
   98           _writer = writer;
   99       }
  100   
  101       public void setHandler(ContentHandler handler) {
  102           _handler = handler;
  103       }
  104   
  105       public void setLexicalHandler(LexicalHandler lex) {
  106           _lexHandler = lex;
  107       }
  108   
  109       public void setNode(Node node) {
  110           _node = node;
  111       }
  112   
  113       public Node getNode() {
  114           return (_handler instanceof SAX2DOM) ? ((SAX2DOM)_handler).getDOM()
  115              : null;
  116       }
  117   
  118       public void setNextSibling(Node nextSibling) {
  119           _nextSibling = nextSibling;
  120       }
  121   
  122       public XMLEventWriter getXMLEventWriter() {
  123           return (_handler instanceof SAX2StAXEventWriter) ? ((SAX2StAXEventWriter) _handler).getEventWriter() : null;
  124       }
  125   
  126       public void setXMLEventWriter(XMLEventWriter eventWriter) {
  127           _xmlStAXEventWriter = eventWriter;
  128       }
  129   
  130       public XMLStreamWriter getXMLStreamWriter() {
  131           return (_handler instanceof SAX2StAXStreamWriter) ? ((SAX2StAXStreamWriter) _handler).getStreamWriter() : null;
  132       }
  133   
  134       public void setXMLStreamWriter(XMLStreamWriter streamWriter) {
  135           _xmlStAXStreamWriter = streamWriter;
  136       }
  137   
  138       public void setIndentNumber(int value) {
  139           _indentNumber = value;
  140       }
  141   
  142       public SerializationHandler getSerializationHandler()
  143           throws IOException, ParserConfigurationException
  144       {
  145           SerializationHandler result = null;
  146           switch (_outputType)
  147           {
  148               case STREAM :
  149   
  150                   if (_method == null)
  151                   {
  152                       result = new ToUnknownStream();
  153                   }
  154                   else if (_method.equalsIgnoreCase("xml"))
  155                   {
  156   
  157                       result = new ToXMLStream();
  158   
  159                   }
  160                   else if (_method.equalsIgnoreCase("html"))
  161                   {
  162   
  163                       result = new ToHTMLStream();
  164   
  165                   }
  166                   else if (_method.equalsIgnoreCase("text"))
  167                   {
  168   
  169                       result = new ToTextStream();
  170   
  171                   }
  172   
  173                   if (result != null && _indentNumber >= 0)
  174                   {
  175                       result.setIndentAmount(_indentNumber);
  176                   }
  177   
  178                   result.setEncoding(_encoding);
  179   
  180                   if (_writer != null)
  181                   {
  182                       result.setWriter(_writer);
  183                   }
  184                   else
  185                   {
  186                       result.setOutputStream(_ostream);
  187                   }
  188                   return result;
  189   
  190               case DOM :
  191                   _handler = (_node != null) ? new SAX2DOM(_node, _nextSibling) : new SAX2DOM();
  192                   _lexHandler = (LexicalHandler) _handler;
  193                   // falls through
  194               case STAX :
  195                   if (_xmlStAXEventWriter != null) {
  196                       _handler =  new SAX2StAXEventWriter(_xmlStAXEventWriter);
  197                   } else if (_xmlStAXStreamWriter != null) {
  198                       _handler =  new SAX2StAXStreamWriter(_xmlStAXStreamWriter);
  199                   }
  200                   _lexHandler = (LexicalHandler) _handler;
  201                   // again falls through - Padmaja Vedula
  202               case SAX :
  203                   if (_method == null)
  204                   {
  205                       _method = "xml"; // default case
  206                   }
  207   
  208                   if (_method.equalsIgnoreCase("xml"))
  209                   {
  210   
  211                       if (_lexHandler == null)
  212                       {
  213                           result = new ToXMLSAXHandler(_handler, _encoding);
  214                       }
  215                       else
  216                       {
  217                           result =
  218                               new ToXMLSAXHandler(
  219                                   _handler,
  220                                   _lexHandler,
  221                                   _encoding);
  222                       }
  223   
  224                   }
  225                   else if (_method.equalsIgnoreCase("html"))
  226                   {
  227   
  228                       if (_lexHandler == null)
  229                       {
  230                           result = new ToHTMLSAXHandler(_handler, _encoding);
  231                       }
  232                       else
  233                       {
  234                           result =
  235                               new ToHTMLSAXHandler(
  236                                   _handler,
  237                                   _lexHandler,
  238                                   _encoding);
  239                       }
  240   
  241                   }
  242                   else if (_method.equalsIgnoreCase("text"))
  243                   {
  244   
  245                       if (_lexHandler == null)
  246                       {
  247                           result = new ToTextSAXHandler(_handler, _encoding);
  248                       }
  249                       else
  250                       {
  251                           result =
  252                               new ToTextSAXHandler(
  253                                   _handler,
  254                                   _lexHandler,
  255                                   _encoding);
  256                       }
  257   
  258                   }
  259                   return result;
  260           }
  261           return null;
  262       }
  263   
  264   }

Save This Page
Home » openjdk-7 » com.sun.org.apache.xalan » internal » xsltc » runtime » output » [javadoc | source]