jaxp/src/com/sun/org/apache/xalan/internal/xsltc/trax/Util.java
changeset 12457 c348e06f0e82
parent 6 7f561c08de6b
child 12458 d601e4bba306
equal deleted inserted replaced
12324:1d7e6da6adc8 12457:c348e06f0e82
       
     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: Util.java,v 1.2.4.1 2005/09/14 09:37:34 pvedula Exp $
       
    22  */
       
    23 
       
    24 package com.sun.org.apache.xalan.internal.xsltc.trax;
       
    25 
       
    26 import java.io.InputStream;
       
    27 import java.io.Reader;
       
    28 
       
    29 import javax.xml.XMLConstants;
       
    30 import javax.xml.parsers.ParserConfigurationException;
       
    31 import javax.xml.parsers.SAXParser;
       
    32 import javax.xml.parsers.SAXParserFactory;
       
    33 
       
    34 import javax.xml.stream.XMLEventReader;
       
    35 import javax.xml.stream.XMLStreamReader;
       
    36 
       
    37 import javax.xml.transform.Source;
       
    38 import javax.xml.transform.TransformerConfigurationException;
       
    39 import javax.xml.transform.dom.DOMSource;
       
    40 import javax.xml.transform.sax.SAXSource;
       
    41 import javax.xml.transform.stax.StAXResult;
       
    42 import javax.xml.transform.stax.StAXSource;
       
    43 import javax.xml.transform.stream.StreamSource;
       
    44 
       
    45 import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
       
    46 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
       
    47 
       
    48 import org.w3c.dom.Document;
       
    49 
       
    50 import org.xml.sax.InputSource;
       
    51 import org.xml.sax.SAXException;
       
    52 import org.xml.sax.SAXNotRecognizedException;
       
    53 import org.xml.sax.SAXNotSupportedException;
       
    54 import org.xml.sax.XMLReader;
       
    55 import org.xml.sax.helpers.XMLReaderFactory;
       
    56 
       
    57 /**
       
    58  * @author Santiago Pericas-Geertsen
       
    59  */
       
    60 public final class Util {
       
    61 
       
    62     public static String baseName(String name) {
       
    63         return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.baseName(name);
       
    64     }
       
    65 
       
    66     public static String noExtName(String name) {
       
    67         return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.noExtName(name);
       
    68     }
       
    69 
       
    70     public static String toJavaName(String name) {
       
    71         return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.toJavaName(name);
       
    72     }
       
    73 
       
    74 
       
    75 
       
    76 
       
    77     /**
       
    78      * Creates a SAX2 InputSource object from a TrAX Source object
       
    79      */
       
    80     public static InputSource getInputSource(XSLTC xsltc, Source source)
       
    81         throws TransformerConfigurationException
       
    82     {
       
    83         InputSource input = null;
       
    84 
       
    85         String systemId = source.getSystemId();
       
    86 
       
    87         try {
       
    88             // Try to get InputSource from SAXSource input
       
    89             if (source instanceof SAXSource) {
       
    90                 final SAXSource sax = (SAXSource)source;
       
    91                 input = sax.getInputSource();
       
    92                 // Pass the SAX parser to the compiler
       
    93                 try {
       
    94                     XMLReader reader = sax.getXMLReader();
       
    95 
       
    96                      /*
       
    97                       * Fix for bug 24695
       
    98                       * According to JAXP 1.2 specification if a SAXSource
       
    99                       * is created using a SAX InputSource the Transformer or
       
   100                       * TransformerFactory creates a reader via the
       
   101                       * XMLReaderFactory if setXMLReader is not used
       
   102                       */
       
   103 
       
   104                     if (reader == null) {
       
   105                        try {
       
   106                            reader= XMLReaderFactory.createXMLReader();
       
   107                        } catch (Exception e ) {
       
   108                            try {
       
   109 
       
   110                                //Incase there is an exception thrown
       
   111                                // resort to JAXP
       
   112                                SAXParserFactory parserFactory =
       
   113                                       SAXParserFactory.newInstance();
       
   114                                parserFactory.setNamespaceAware(true);
       
   115 
       
   116                                if (xsltc.isSecureProcessing()) {
       
   117                                   try {
       
   118                                       parserFactory.setFeature(
       
   119                                           XMLConstants.FEATURE_SECURE_PROCESSING, true);
       
   120                                   }
       
   121                                   catch (org.xml.sax.SAXException se) {}
       
   122                                }
       
   123 
       
   124                                reader = parserFactory.newSAXParser()
       
   125                                      .getXMLReader();
       
   126 
       
   127 
       
   128                            } catch (ParserConfigurationException pce ) {
       
   129                                throw new TransformerConfigurationException
       
   130                                  ("ParserConfigurationException" ,pce);
       
   131                            }
       
   132                        }
       
   133                     }
       
   134                     reader.setFeature
       
   135                         ("http://xml.org/sax/features/namespaces",true);
       
   136                     reader.setFeature
       
   137                         ("http://xml.org/sax/features/namespace-prefixes",false);
       
   138 
       
   139                     xsltc.setXMLReader(reader);
       
   140                 }catch (SAXNotRecognizedException snre ) {
       
   141                   throw new TransformerConfigurationException
       
   142                        ("SAXNotRecognizedException ",snre);
       
   143                 }catch (SAXNotSupportedException snse ) {
       
   144                   throw new TransformerConfigurationException
       
   145                        ("SAXNotSupportedException ",snse);
       
   146                 }catch (SAXException se ) {
       
   147                   throw new TransformerConfigurationException
       
   148                        ("SAXException ",se);
       
   149                 }
       
   150 
       
   151             }
       
   152             // handle  DOMSource
       
   153             else if (source instanceof DOMSource) {
       
   154                 final DOMSource domsrc = (DOMSource)source;
       
   155                 final Document dom = (Document)domsrc.getNode();
       
   156                 final DOM2SAX dom2sax = new DOM2SAX(dom);
       
   157                 xsltc.setXMLReader(dom2sax);
       
   158 
       
   159                 // Try to get SAX InputSource from DOM Source.
       
   160                 input = SAXSource.sourceToInputSource(source);
       
   161                 if (input == null){
       
   162                     input = new InputSource(domsrc.getSystemId());
       
   163                 }
       
   164             }
       
   165 
       
   166             // handle StAXSource
       
   167             else if (source instanceof StAXSource) {
       
   168                 final StAXSource staxSource = (StAXSource)source;
       
   169                 StAXEvent2SAX staxevent2sax = null;
       
   170                 StAXStream2SAX staxStream2SAX = null;
       
   171                 if (staxSource.getXMLEventReader() != null) {
       
   172                     final XMLEventReader xmlEventReader = staxSource.getXMLEventReader();
       
   173                     staxevent2sax = new StAXEvent2SAX(xmlEventReader);
       
   174                     xsltc.setXMLReader(staxevent2sax);
       
   175                 } else if (staxSource.getXMLStreamReader() != null) {
       
   176                     final XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader();
       
   177                     staxStream2SAX = new StAXStream2SAX(xmlStreamReader);
       
   178                     xsltc.setXMLReader(staxStream2SAX);
       
   179                 }
       
   180 
       
   181                 // get sax InputSource from StAXSource
       
   182                 input = SAXSource.sourceToInputSource(source);
       
   183                 if (input == null){
       
   184                     input = new InputSource(staxSource.getSystemId());
       
   185                 }
       
   186             }
       
   187 
       
   188             // Try to get InputStream or Reader from StreamSource
       
   189             else if (source instanceof StreamSource) {
       
   190                 final StreamSource stream = (StreamSource)source;
       
   191                 final InputStream istream = stream.getInputStream();
       
   192                 final Reader reader = stream.getReader();
       
   193                 xsltc.setXMLReader(null);     // Clear old XML reader
       
   194 
       
   195                 // Create InputSource from Reader or InputStream in Source
       
   196                 if (istream != null) {
       
   197                     input = new InputSource(istream);
       
   198                 }
       
   199                 else if (reader != null) {
       
   200                     input = new InputSource(reader);
       
   201                 }
       
   202                 else {
       
   203                     input = new InputSource(systemId);
       
   204                 }
       
   205             }
       
   206             else {
       
   207                 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR);
       
   208                 throw new TransformerConfigurationException(err.toString());
       
   209             }
       
   210             input.setSystemId(systemId);
       
   211         }
       
   212         catch (NullPointerException e) {
       
   213             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR,
       
   214                                         "TransformerFactory.newTemplates()");
       
   215             throw new TransformerConfigurationException(err.toString());
       
   216         }
       
   217         catch (SecurityException e) {
       
   218             ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId);
       
   219             throw new TransformerConfigurationException(err.toString());
       
   220         }
       
   221         return input;
       
   222     }
       
   223 
       
   224 }