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.SAXParser; 33 import javax.xml.parsers.SAXParserFactory; 34 35 import javax.xml.stream.XMLEventReader; 36 import javax.xml.stream.XMLStreamReader; 37 38 import javax.xml.transform.Source; 39 import javax.xml.transform.TransformerConfigurationException; 40 import javax.xml.transform.dom.DOMSource; 41 import javax.xml.transform.sax.SAXSource; 42 import javax.xml.transform.stax.StAXResult; 43 import javax.xml.transform.stax.StAXSource; 44 import javax.xml.transform.stream.StreamSource; 45 46 import com.sun.org.apache.xalan.internal.utils.FactoryImpl; 47 import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager; 48 import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC; 49 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 50 51 import org.w3c.dom.Document; 52 53 import org.xml.sax.InputSource; 54 import org.xml.sax.SAXException; 55 import org.xml.sax.SAXNotRecognizedException; 56 import org.xml.sax.SAXNotSupportedException; 57 import org.xml.sax.XMLReader; 58 import org.xml.sax.helpers.XMLReaderFactory; 59 60 /** 61 * @author Santiago Pericas-Geertsen 62 */ 63 public final class Util { 64 65 public static String baseName(String name) { 66 return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.baseName(name); 67 } 68 69 public static String noExtName(String name) { 70 return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.noExtName(name); 71 } 72 73 public static String toJavaName(String name) { 74 return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.toJavaName(name); 75 } 76 77 78 79 80 /** 81 * Creates a SAX2 InputSource object from a TrAX Source object 82 */ 83 public static InputSource getInputSource(XSLTC xsltc, Source source) 84 throws TransformerConfigurationException 85 { 86 InputSource input = null; 87 88 String systemId = source.getSystemId(); 89 90 try { 91 // Try to get InputSource from SAXSource input 92 if (source instanceof SAXSource) { 93 final SAXSource sax = (SAXSource)source; 94 input = sax.getInputSource(); 95 // Pass the SAX parser to the compiler 96 try { 97 XMLReader reader = sax.getXMLReader(); 98 99 /* 100 * Fix for bug 24695 101 * According to JAXP 1.2 specification if a SAXSource 102 * is created using a SAX InputSource the Transformer or 103 * TransformerFactory creates a reader via the 104 * XMLReaderFactory if setXMLReader is not used 105 */ 106 107 if (reader == null) { 108 try { 109 reader= XMLReaderFactory.createXMLReader(); 110 try { 111 reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, 112 xsltc.isSecureProcessing()); 113 } catch (SAXNotRecognizedException e) { 114 System.err.println("Warning: " + reader.getClass().getName() + ": " 115 + e.getMessage()); 116 } 117 } catch (Exception e ) { 118 try { 119 120 //Incase there is an exception thrown 121 // resort to JAXP 122 SAXParserFactory parserFactory = FactoryImpl.getSAXFactory(xsltc.useServicesMechnism()); 123 parserFactory.setNamespaceAware(true); 124 125 if (xsltc.isSecureProcessing()) { 126 try { 127 parserFactory.setFeature( 128 XMLConstants.FEATURE_SECURE_PROCESSING, true); 129 } 130 catch (org.xml.sax.SAXException se) {} 131 } 132 133 reader = parserFactory.newSAXParser() 134 .getXMLReader(); 135 136 137 } catch (ParserConfigurationException pce ) { 138 throw new TransformerConfigurationException 139 ("ParserConfigurationException" ,pce); 140 } 141 } 142 } 143 reader.setFeature 144 ("http://xml.org/sax/features/namespaces",true); 145 reader.setFeature 146 ("http://xml.org/sax/features/namespace-prefixes",false); 147 148 try { 149 reader.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, 150 xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_DTD)); 151 } catch (SAXNotRecognizedException e) { 152 System.err.println("Warning: " + reader.getClass().getName() + ": " 153 + e.getMessage()); 154 } 155 156 try { 157 XMLSecurityManager securityManager = 158 (XMLSecurityManager)xsltc.getProperty(XalanConstants.SECURITY_MANAGER); 159 if (securityManager != null) { 160 for (XMLSecurityManager.Limit limit : XMLSecurityManager.Limit.values()) { 161 reader.setProperty(limit.apiProperty(), 162 securityManager.getLimitValueAsString(limit)); 163 } 164 if (securityManager.printEntityCountInfo()) { 165 reader.setProperty(XalanConstants.JDK_ENTITY_COUNT_INFO, XalanConstants.JDK_YES); 166 } 167 } 168 } catch (SAXException se) { 169 System.err.println("Warning: " + reader.getClass().getName() + ": " 170 + se.getMessage()); 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 }