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 reader.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, 109 xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_DTD)); 110 } catch (Exception e ) { 111 try { 112 113 //Incase there is an exception thrown 114 // resort to JAXP 115 SAXParserFactory parserFactory = FactoryImpl.getSAXFactory(xsltc.useServicesMechnism()); 116 parserFactory.setNamespaceAware(true); 117 118 if (xsltc.isSecureProcessing()) { 119 try { 120 parserFactory.setFeature( 121 XMLConstants.FEATURE_SECURE_PROCESSING, true); 122 } 123 catch (org.xml.sax.SAXException se) {} 124 } 125 126 reader = parserFactory.newSAXParser() 127 .getXMLReader(); 128 129 130 } catch (ParserConfigurationException pce ) { 131 throw new TransformerConfigurationException 132 ("ParserConfigurationException" ,pce); 133 } 134 } 135 } 136 reader.setFeature 137 ("http://xml.org/sax/features/namespaces",true); 138 reader.setFeature 139 ("http://xml.org/sax/features/namespace-prefixes",false); 140 141 xsltc.setXMLReader(reader); 142 }catch (SAXNotRecognizedException snre ) { 143 throw new TransformerConfigurationException 144 ("SAXNotRecognizedException ",snre); 145 }catch (SAXNotSupportedException snse ) { 146 throw new TransformerConfigurationException 147 ("SAXNotSupportedException ",snse); 148 }catch (SAXException se ) { 149 throw new TransformerConfigurationException 150 ("SAXException ",se); 151 } 152 153 } 154 // handle DOMSource 155 else if (source instanceof DOMSource) { 156 final DOMSource domsrc = (DOMSource)source; 157 final Document dom = (Document)domsrc.getNode(); 158 final DOM2SAX dom2sax = new DOM2SAX(dom); 159 xsltc.setXMLReader(dom2sax); 160 161 // Try to get SAX InputSource from DOM Source. 162 input = SAXSource.sourceToInputSource(source); 163 if (input == null){ 164 input = new InputSource(domsrc.getSystemId()); 165 } 166 } 167 168 // handle StAXSource 169 else if (source instanceof StAXSource) { 170 final StAXSource staxSource = (StAXSource)source; 171 StAXEvent2SAX staxevent2sax = null; 172 StAXStream2SAX staxStream2SAX = null; 173 if (staxSource.getXMLEventReader() != null) { 174 final XMLEventReader xmlEventReader = staxSource.getXMLEventReader(); 175 staxevent2sax = new StAXEvent2SAX(xmlEventReader); 176 xsltc.setXMLReader(staxevent2sax); 177 } else if (staxSource.getXMLStreamReader() != null) { 178 final XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader(); 179 staxStream2SAX = new StAXStream2SAX(xmlStreamReader); 180 xsltc.setXMLReader(staxStream2SAX); 181 } 182 183 // get sax InputSource from StAXSource 184 input = SAXSource.sourceToInputSource(source); 185 if (input == null){ 186 input = new InputSource(staxSource.getSystemId()); 187 } 188 } 189 190 // Try to get InputStream or Reader from StreamSource 191 else if (source instanceof StreamSource) { 192 final StreamSource stream = (StreamSource)source; 193 final InputStream istream = stream.getInputStream(); 194 final Reader reader = stream.getReader(); 195 xsltc.setXMLReader(null); // Clear old XML reader 196 197 // Create InputSource from Reader or InputStream in Source 198 if (istream != null) { 199 input = new InputSource(istream); 200 } 201 else if (reader != null) { 202 input = new InputSource(reader); 203 } 204 else { 205 input = new InputSource(systemId); 206 } 207 } 208 else { 209 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR); 210 throw new TransformerConfigurationException(err.toString()); 211 } 212 input.setSystemId(systemId); 213 } 214 catch (NullPointerException e) { 215 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR, 216 "TransformerFactory.newTemplates()"); 217 throw new TransformerConfigurationException(err.toString()); 218 } 219 catch (SecurityException e) { 220 ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId); 221 throw new TransformerConfigurationException(err.toString()); 222 } 223 return input; 224 } 225 226 }