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