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.utils.FactoryImpl;
  46 import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
  47 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  48 
  49 import org.w3c.dom.Document;
  50 
  51 import org.xml.sax.InputSource;
  52 import org.xml.sax.SAXException;
  53 import org.xml.sax.SAXNotRecognizedException;
  54 import org.xml.sax.SAXNotSupportedException;
  55 import org.xml.sax.XMLReader;
  56 import org.xml.sax.helpers.XMLReaderFactory;
  57 
  58 /**
  59  * @author Santiago Pericas-Geertsen
  60  */
  61 public final class Util {
  62 
  63     public static String baseName(String name) {
  64         return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.baseName(name);
  65     }
  66 
  67     public static String noExtName(String name) {
  68         return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.noExtName(name);
  69     }
  70 
  71     public static String toJavaName(String name) {
  72         return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.toJavaName(name);
  73     }
  74 
  75 
  76 
  77 
  78     /**
  79      * Creates a SAX2 InputSource object from a TrAX Source object
  80      */
  81     public static InputSource getInputSource(XSLTC xsltc, Source source)
  82         throws TransformerConfigurationException
  83     {
  84         InputSource input = null;
  85 
  86         String systemId = source.getSystemId();
  87 
  88         try {
  89             // Try to get InputSource from SAXSource input
  90             if (source instanceof SAXSource) {
  91                 final SAXSource sax = (SAXSource)source;
  92                 input = sax.getInputSource();
  93                 // Pass the SAX parser to the compiler
  94                 try {
  95                     XMLReader reader = sax.getXMLReader();
  96 
  97                      /*
  98                       * Fix for bug 24695
  99                       * According to JAXP 1.2 specification if a SAXSource
 100                       * is created using a SAX InputSource the Transformer or
 101                       * TransformerFactory creates a reader via the
 102                       * XMLReaderFactory if setXMLReader is not used
 103                       */
 104 
 105                     if (reader == null) {
 106                        try {
 107                            reader= XMLReaderFactory.createXMLReader();
 108                        } catch (Exception e ) {
 109                            try {
 110 
 111                                //Incase there is an exception thrown
 112                                // resort to JAXP
 113                                SAXParserFactory parserFactory = FactoryImpl.getSAXFactory(xsltc.useServicesMechnism());
 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                     try {
 140                         reader.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, 
 141                                    xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_DTD));
 142                     } catch (SAXNotRecognizedException e) {
 143                         System.err.println("Warning:  " + reader.getClass().getName() + ": " 
 144                                 + e.getMessage());
 145                     }
 146 
 147                     xsltc.setXMLReader(reader);
 148                 }catch (SAXNotRecognizedException snre ) {
 149                   throw new TransformerConfigurationException
 150                        ("SAXNotRecognizedException ",snre);
 151                 }catch (SAXNotSupportedException snse ) {
 152                   throw new TransformerConfigurationException
 153                        ("SAXNotSupportedException ",snse);
 154                 }catch (SAXException se ) {
 155                   throw new TransformerConfigurationException
 156                        ("SAXException ",se);
 157                 }
 158 
 159             }
 160             // handle  DOMSource
 161             else if (source instanceof DOMSource) {
 162                 final DOMSource domsrc = (DOMSource)source;
 163                 final Document dom = (Document)domsrc.getNode();
 164                 final DOM2SAX dom2sax = new DOM2SAX(dom);
 165                 xsltc.setXMLReader(dom2sax);
 166 
 167                 // Try to get SAX InputSource from DOM Source.
 168                 input = SAXSource.sourceToInputSource(source);
 169                 if (input == null){
 170                     input = new InputSource(domsrc.getSystemId());
 171                 }
 172             }
 173 
 174             // handle StAXSource
 175             else if (source instanceof StAXSource) {
 176                 final StAXSource staxSource = (StAXSource)source;
 177                 StAXEvent2SAX staxevent2sax = null;
 178                 StAXStream2SAX staxStream2SAX = null;
 179                 if (staxSource.getXMLEventReader() != null) {
 180                     final XMLEventReader xmlEventReader = staxSource.getXMLEventReader();
 181                     staxevent2sax = new StAXEvent2SAX(xmlEventReader);
 182                     xsltc.setXMLReader(staxevent2sax);
 183                 } else if (staxSource.getXMLStreamReader() != null) {
 184                     final XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader();
 185                     staxStream2SAX = new StAXStream2SAX(xmlStreamReader);
 186                     xsltc.setXMLReader(staxStream2SAX);
 187                 }
 188 
 189                 // get sax InputSource from StAXSource
 190                 input = SAXSource.sourceToInputSource(source);
 191                 if (input == null){
 192                     input = new InputSource(staxSource.getSystemId());
 193                 }
 194             }
 195 
 196             // Try to get InputStream or Reader from StreamSource
 197             else if (source instanceof StreamSource) {
 198                 final StreamSource stream = (StreamSource)source;
 199                 final InputStream istream = stream.getInputStream();
 200                 final Reader reader = stream.getReader();
 201                 xsltc.setXMLReader(null);     // Clear old XML reader
 202 
 203                 // Create InputSource from Reader or InputStream in Source
 204                 if (istream != null) {
 205                     input = new InputSource(istream);
 206                 }
 207                 else if (reader != null) {
 208                     input = new InputSource(reader);
 209                 }
 210                 else {
 211                     input = new InputSource(systemId);
 212                 }
 213             }
 214             else {
 215                 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR);
 216                 throw new TransformerConfigurationException(err.toString());
 217             }
 218             input.setSystemId(systemId);
 219         }
 220         catch (NullPointerException e) {
 221             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR,
 222                                         "TransformerFactory.newTemplates()");
 223             throw new TransformerConfigurationException(err.toString());
 224         }
 225         catch (SecurityException e) {
 226             ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId);
 227             throw new TransformerConfigurationException(err.toString());
 228         }
 229         return input;
 230     }
 231 
 232 }