1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.trax; 21 22 import java.io.InputStream ; 23 import java.io.Reader ; 24 import javax.xml.XMLConstants ; 25 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.parsers.SAXParser ; 28 import javax.xml.parsers.SAXParserFactory ; 29 30 import javax.xml.transform.Source ; 31 import javax.xml.transform.TransformerConfigurationException ; 32 import javax.xml.transform.dom.DOMSource ; 33 import javax.xml.transform.sax.SAXSource ; 34 import javax.xml.transform.stream.StreamSource ; 35 36 import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC; 37 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 38 39 import org.w3c.dom.Document ; 40 41 import org.xml.sax.InputSource ; 42 import org.xml.sax.SAXException ; 43 import org.xml.sax.SAXNotRecognizedException ; 44 import org.xml.sax.SAXNotSupportedException ; 45 import org.xml.sax.XMLReader ; 46 import org.xml.sax.helpers.XMLReaderFactory ; 47 48 51 public final class Util { 52 53 public static String baseName(String name) { 54 return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.baseName(name); 55 } 56 57 public static String noExtName(String name) { 58 return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.noExtName(name); 59 } 60 61 public static String toJavaName(String name) { 62 return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.toJavaName(name); 63 } 64 65 66 67 68 71 public static InputSource getInputSource(XSLTC xsltc, Source source) 72 throws TransformerConfigurationException 73 { 74 InputSource input = null; 75 76 String systemId = source.getSystemId(); 77 78 try { 79 if (source instanceof SAXSource ) { 81 final SAXSource sax = (SAXSource )source; 82 input = sax.getInputSource(); 83 try { 85 XMLReader reader = sax.getXMLReader(); 86 87 94 95 if (reader == null) { 96 try { 97 reader= XMLReaderFactory.createXMLReader(); 98 } catch (Exception e ) { 99 try { 100 101 SAXParserFactory parserFactory = 104 SAXParserFactory.newInstance(); 105 parserFactory.setNamespaceAware(true); 106 107 if (xsltc.isSecureProcessing()) { 108 try { 109 parserFactory.setFeature( 110 XMLConstants.FEATURE_SECURE_PROCESSING, true); 111 } 112 catch (org.xml.sax.SAXException se) {} 113 } 114 115 reader = parserFactory.newSAXParser() 116 .getXMLReader(); 117 118 119 } catch (ParserConfigurationException pce ) { 120 throw new TransformerConfigurationException 121 ("ParserConfigurationException" ,pce); 122 } 123 } 124 } 125 reader.setFeature 126 ("http://xml.org/sax/features/namespaces",true); 127 reader.setFeature 128 ("http://xml.org/sax/features/namespace-prefixes",false); 129 130 xsltc.setXMLReader(reader); 131 }catch (SAXNotRecognizedException snre ) { 132 throw new TransformerConfigurationException 133 ("SAXNotRecognizedException ",snre); 134 }catch (SAXNotSupportedException snse ) { 135 throw new TransformerConfigurationException 136 ("SAXNotSupportedException ",snse); 137 }catch (SAXException se ) { 138 throw new TransformerConfigurationException 139 ("SAXException ",se); 140 } 141 142 } 143 else if (source instanceof DOMSource ) { 145 final DOMSource domsrc = (DOMSource )source; 146 final Document dom = (Document )domsrc.getNode(); 147 final DOM2SAX dom2sax = new DOM2SAX(dom); 148 xsltc.setXMLReader(dom2sax); 149 150 input = SAXSource.sourceToInputSource(source); 152 if (input == null){ 153 input = new InputSource (domsrc.getSystemId()); 154 } 155 } 156 else if (source instanceof StreamSource ) { 158 final StreamSource stream = (StreamSource )source; 159 final InputStream istream = stream.getInputStream(); 160 final Reader reader = stream.getReader(); 161 xsltc.setXMLReader(null); 163 if (istream != null) { 165 input = new InputSource (istream); 166 } 167 else if (reader != null) { 168 input = new InputSource (reader); 169 } 170 else { 171 input = new InputSource (systemId); 172 } 173 } 174 else { 175 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR); 176 throw new TransformerConfigurationException (err.toString()); 177 } 178 input.setSystemId(systemId); 179 } 180 catch (NullPointerException e) { 181 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR, 182 "TransformerFactory.newTemplates()"); 183 throw new TransformerConfigurationException (err.toString()); 184 } 185 catch (SecurityException e) { 186 ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId); 187 throw new TransformerConfigurationException (err.toString()); 188 } 189 return input; 190 } 191 192 } 193 194 | Popular Tags |