001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 /* 019 * $Id: Util.java 468653 2006-10-28 07:07:05Z minchau $ 020 */ 021 022 package org.apache.xalan.xsltc.trax; 023 024 import java.io.InputStream; 025 import java.io.Reader; 026 027 import javax.xml.XMLConstants; 028 import javax.xml.parsers.ParserConfigurationException; 029 import javax.xml.parsers.SAXParser; 030 import javax.xml.parsers.SAXParserFactory; 031 032 import javax.xml.transform.Source; 033 import javax.xml.transform.TransformerConfigurationException; 034 import javax.xml.transform.dom.DOMSource; 035 import javax.xml.transform.sax.SAXSource; 036 import javax.xml.transform.stream.StreamSource; 037 038 import org.apache.xalan.xsltc.compiler.XSLTC; 039 import org.apache.xalan.xsltc.compiler.util.ErrorMsg; 040 041 import org.w3c.dom.Document; 042 043 import org.xml.sax.InputSource; 044 import org.xml.sax.SAXException; 045 import org.xml.sax.SAXNotRecognizedException; 046 import org.xml.sax.SAXNotSupportedException; 047 import org.xml.sax.XMLReader; 048 import org.xml.sax.helpers.XMLReaderFactory; 049 050 /** 051 * @author Santiago Pericas-Geertsen 052 */ 053 public final class Util { 054 055 public static String baseName(String name) { 056 return org.apache.xalan.xsltc.compiler.util.Util.baseName(name); 057 } 058 059 public static String noExtName(String name) { 060 return org.apache.xalan.xsltc.compiler.util.Util.noExtName(name); 061 } 062 063 public static String toJavaName(String name) { 064 return org.apache.xalan.xsltc.compiler.util.Util.toJavaName(name); 065 } 066 067 068 069 070 /** 071 * Creates a SAX2 InputSource object from a TrAX Source object 072 */ 073 public static InputSource getInputSource(XSLTC xsltc, Source source) 074 throws TransformerConfigurationException 075 { 076 InputSource input = null; 077 078 String systemId = source.getSystemId(); 079 080 try { 081 // Try to get InputSource from SAXSource input 082 if (source instanceof SAXSource) { 083 final SAXSource sax = (SAXSource)source; 084 input = sax.getInputSource(); 085 // Pass the SAX parser to the compiler 086 try { 087 XMLReader reader = sax.getXMLReader(); 088 089 /* 090 * Fix for bug 24695 091 * According to JAXP 1.2 specification if a SAXSource 092 * is created using a SAX InputSource the Transformer or 093 * TransformerFactory creates a reader via the 094 * XMLReaderFactory if setXMLReader is not used 095 */ 096 097 if (reader == null) { 098 try { 099 reader= XMLReaderFactory.createXMLReader(); 100 } catch (Exception e ) { 101 try { 102 103 //Incase there is an exception thrown 104 // resort to JAXP 105 SAXParserFactory parserFactory = 106 SAXParserFactory.newInstance(); 107 parserFactory.setNamespaceAware(true); 108 109 if (xsltc.isSecureProcessing()) { 110 try { 111 parserFactory.setFeature( 112 XMLConstants.FEATURE_SECURE_PROCESSING, true); 113 } 114 catch (org.xml.sax.SAXException se) {} 115 } 116 117 reader = parserFactory.newSAXParser() 118 .getXMLReader(); 119 120 121 } catch (ParserConfigurationException pce ) { 122 throw new TransformerConfigurationException 123 ("ParserConfigurationException" ,pce); 124 } 125 } 126 } 127 reader.setFeature 128 ("http://xml.org/sax/features/namespaces",true); 129 reader.setFeature 130 ("http://xml.org/sax/features/namespace-prefixes",false); 131 132 xsltc.setXMLReader(reader); 133 }catch (SAXNotRecognizedException snre ) { 134 throw new TransformerConfigurationException 135 ("SAXNotRecognizedException ",snre); 136 }catch (SAXNotSupportedException snse ) { 137 throw new TransformerConfigurationException 138 ("SAXNotSupportedException ",snse); 139 }catch (SAXException se ) { 140 throw new TransformerConfigurationException 141 ("SAXException ",se); 142 } 143 144 } 145 // handle DOMSource 146 else if (source instanceof DOMSource) { 147 final DOMSource domsrc = (DOMSource)source; 148 final Document dom = (Document)domsrc.getNode(); 149 final DOM2SAX dom2sax = new DOM2SAX(dom); 150 xsltc.setXMLReader(dom2sax); 151 152 // Try to get SAX InputSource from DOM Source. 153 input = SAXSource.sourceToInputSource(source); 154 if (input == null){ 155 input = new InputSource(domsrc.getSystemId()); 156 } 157 } 158 // Try to get InputStream or Reader from StreamSource 159 else if (source instanceof StreamSource) { 160 final StreamSource stream = (StreamSource)source; 161 final InputStream istream = stream.getInputStream(); 162 final Reader reader = stream.getReader(); 163 xsltc.setXMLReader(null); // Clear old XML reader 164 165 // Create InputSource from Reader or InputStream in Source 166 if (istream != null) { 167 input = new InputSource(istream); 168 } 169 else if (reader != null) { 170 input = new InputSource(reader); 171 } 172 else { 173 input = new InputSource(systemId); 174 } 175 } 176 else { 177 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR); 178 throw new TransformerConfigurationException(err.toString()); 179 } 180 input.setSystemId(systemId); 181 } 182 catch (NullPointerException e) { 183 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR, 184 "TransformerFactory.newTemplates()"); 185 throw new TransformerConfigurationException(err.toString()); 186 } 187 catch (SecurityException e) { 188 ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId); 189 throw new TransformerConfigurationException(err.toString()); 190 } 191 return input; 192 } 193 194 } 195