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 com.sun.org.apache.xalan.internal.XalanConstants; 27 import java.io.InputStream; 28 import java.io.Reader; 29 30 import javax.xml.XMLConstants; 31 import javax.xml.parsers.ParserConfigurationException; 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.StAXSource; 42 import javax.xml.transform.stream.StreamSource; 43 44 import com.sun.org.apache.xalan.internal.utils.FactoryImpl; 45 import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager; 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 try { 109 reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, 110 xsltc.isSecureProcessing()); 111 } catch (SAXNotRecognizedException e) { 112 XMLSecurityManager.printWarning(reader.getClass().getName(), 113 XMLConstants.FEATURE_SECURE_PROCESSING, e); 114 } 115 } catch (Exception e ) { 116 try { 117 118 //Incase there is an exception thrown 119 // resort to JAXP 120 SAXParserFactory parserFactory = FactoryImpl.getSAXFactory(xsltc.useServicesMechnism()); 121 parserFactory.setNamespaceAware(true); 122 123 if (xsltc.isSecureProcessing()) { 124 try { 125 parserFactory.setFeature( 126 XMLConstants.FEATURE_SECURE_PROCESSING, true); 127 } 128 catch (org.xml.sax.SAXException se) {} 129 } 130 131 reader = parserFactory.newSAXParser() 132 .getXMLReader(); 133 134 135 } catch (ParserConfigurationException pce ) { 136 throw new TransformerConfigurationException 137 ("ParserConfigurationException" ,pce); 138 } 139 } 140 } 141 reader.setFeature 142 ("http://xml.org/sax/features/namespaces",true); 143 reader.setFeature 144 ("http://xml.org/sax/features/namespace-prefixes",false); 145 146 try { 147 reader.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, 148 xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_DTD)); 149 } catch (SAXNotRecognizedException e) { 150 XMLSecurityManager.printWarning(reader.getClass().getName(), 151 XMLConstants.ACCESS_EXTERNAL_DTD, e); 152 } 153 154 String lastProperty = ""; 155 try { 156 XMLSecurityManager securityManager = 157 (XMLSecurityManager)xsltc.getProperty(XalanConstants.SECURITY_MANAGER); 158 if (securityManager != null) { 159 for (XMLSecurityManager.Limit limit : XMLSecurityManager.Limit.values()) { 160 lastProperty = limit.apiProperty(); 161 reader.setProperty(lastProperty, 162 securityManager.getLimitValueAsString(limit)); 163 } 164 if (securityManager.printEntityCountInfo()) { 165 lastProperty = XalanConstants.JDK_ENTITY_COUNT_INFO; 166 reader.setProperty(XalanConstants.JDK_ENTITY_COUNT_INFO, XalanConstants.JDK_YES); 167 } 168 } 169 } catch (SAXException se) { 170 XMLSecurityManager.printWarning(reader.getClass().getName(), lastProperty, se); 171 } 172 xsltc.setXMLReader(reader); 173 }catch (SAXNotRecognizedException snre ) { 174 throw new TransformerConfigurationException 175 ("SAXNotRecognizedException ",snre); 176 }catch (SAXNotSupportedException snse ) { 177 throw new TransformerConfigurationException 178 ("SAXNotSupportedException ",snse); 179 }catch (SAXException se ) { 180 throw new TransformerConfigurationException 181 ("SAXException ",se); 182 } 183 184 } 185 // handle DOMSource 186 else if (source instanceof DOMSource) { 187 final DOMSource domsrc = (DOMSource)source; 188 final Document dom = (Document)domsrc.getNode(); 189 final DOM2SAX dom2sax = new DOM2SAX(dom); 190 xsltc.setXMLReader(dom2sax); 191 192 // Try to get SAX InputSource from DOM Source. 193 input = SAXSource.sourceToInputSource(source); 194 if (input == null){ 195 input = new InputSource(domsrc.getSystemId()); 196 } 197 } 198 199 // handle StAXSource 200 else if (source instanceof StAXSource) { 201 final StAXSource staxSource = (StAXSource)source; 202 StAXEvent2SAX staxevent2sax = null; 203 StAXStream2SAX staxStream2SAX = null; 204 if (staxSource.getXMLEventReader() != null) { 205 final XMLEventReader xmlEventReader = staxSource.getXMLEventReader(); 206 staxevent2sax = new StAXEvent2SAX(xmlEventReader); 207 xsltc.setXMLReader(staxevent2sax); 208 } else if (staxSource.getXMLStreamReader() != null) { 209 final XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader(); 210 staxStream2SAX = new StAXStream2SAX(xmlStreamReader); 211 xsltc.setXMLReader(staxStream2SAX); 212 } 213 214 // get sax InputSource from StAXSource 215 input = SAXSource.sourceToInputSource(source); 216 if (input == null){ 217 input = new InputSource(staxSource.getSystemId()); 218 } 219 } 220 221 // Try to get InputStream or Reader from StreamSource 222 else if (source instanceof StreamSource) { 223 final StreamSource stream = (StreamSource)source; 224 final InputStream istream = stream.getInputStream(); 225 final Reader reader = stream.getReader(); 226 xsltc.setXMLReader(null); // Clear old XML reader 227 228 // Create InputSource from Reader or InputStream in Source 229 if (istream != null) { 230 input = new InputSource(istream); 231 } 232 else if (reader != null) { 233 input = new InputSource(reader); 234 } 235 else { 236 input = new InputSource(systemId); 237 } 238 } 239 else { 240 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR); 241 throw new TransformerConfigurationException(err.toString()); 242 } 243 input.setSystemId(systemId); 244 } 245 catch (NullPointerException e) { 246 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR, 247 "TransformerFactory.newTemplates()"); 248 throw new TransformerConfigurationException(err.toString()); 249 } 250 catch (SecurityException e) { 251 ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId); 252 throw new TransformerConfigurationException(err.toString()); 253 } 254 return input; 255 } 256 257 }