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 */ 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); | 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 */ 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); |