1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: Util.java,v 1.10 2004/02/16 22:57:21 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.trax;
  20. import java.io.InputStream;
  21. import java.io.Reader;
  22. import javax.xml.parsers.ParserConfigurationException;
  23. import javax.xml.parsers.SAXParser;
  24. import javax.xml.parsers.SAXParserFactory;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.TransformerConfigurationException;
  27. import javax.xml.transform.dom.DOMSource;
  28. import javax.xml.transform.sax.SAXSource;
  29. import javax.xml.transform.stream.StreamSource;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
  31. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  32. import org.w3c.dom.Document;
  33. import org.xml.sax.InputSource;
  34. import org.xml.sax.SAXException;
  35. import org.xml.sax.SAXNotRecognizedException;
  36. import org.xml.sax.SAXNotSupportedException;
  37. import org.xml.sax.XMLReader;
  38. import org.xml.sax.helpers.XMLReaderFactory;
  39. /**
  40. * @author Santiago Pericas-Geertsen
  41. */
  42. public final class Util {
  43. public static String baseName(String name) {
  44. return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.baseName(name);
  45. }
  46. public static String noExtName(String name) {
  47. return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.noExtName(name);
  48. }
  49. public static String toJavaName(String name) {
  50. return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.toJavaName(name);
  51. }
  52. /**
  53. * Creates a SAX2 InputSource object from a TrAX Source object
  54. */
  55. public static InputSource getInputSource(XSLTC xsltc, Source source)
  56. throws TransformerConfigurationException
  57. {
  58. InputSource input = null;
  59. String systemId = source.getSystemId();
  60. try {
  61. // Try to get InputSource from SAXSource input
  62. if (source instanceof SAXSource) {
  63. final SAXSource sax = (SAXSource)source;
  64. input = sax.getInputSource();
  65. // Pass the SAX parser to the compiler
  66. try {
  67. XMLReader reader = sax.getXMLReader();
  68. /*
  69. * Fix for bug 24695
  70. * According to JAXP 1.2 specification if a SAXSource
  71. * is created using a SAX InputSource the Transformer or
  72. * TransformerFactory creates a reader via the
  73. * XMLReaderFactory if setXMLReader is not used
  74. */
  75. if (reader == null) {
  76. try {
  77. reader= XMLReaderFactory.createXMLReader();
  78. } catch (Exception e ) {
  79. try {
  80. //Incase there is an exception thrown
  81. // resort to JAXP
  82. SAXParserFactory parserFactory =
  83. SAXParserFactory.newInstance();
  84. parserFactory.setNamespaceAware(true);
  85. reader = parserFactory.newSAXParser()
  86. .getXMLReader();
  87. } catch (ParserConfigurationException pce ) {
  88. throw new TransformerConfigurationException
  89. ("ParserConfigurationException" ,pce);
  90. }
  91. }
  92. }
  93. reader.setFeature
  94. ("http://xml.org/sax/features/namespaces",true);
  95. reader.setFeature
  96. ("http://xml.org/sax/features/namespace-prefixes",false);
  97. xsltc.setXMLReader(reader);
  98. }catch (SAXNotRecognizedException snre ) {
  99. throw new TransformerConfigurationException
  100. ("SAXNotRecognizedException ",snre);
  101. }catch (SAXNotSupportedException snse ) {
  102. throw new TransformerConfigurationException
  103. ("SAXNotSupportedException ",snse);
  104. }catch (SAXException se ) {
  105. throw new TransformerConfigurationException
  106. ("SAXException ",se);
  107. }
  108. }
  109. // handle DOMSource
  110. else if (source instanceof DOMSource) {
  111. final DOMSource domsrc = (DOMSource)source;
  112. final Document dom = (Document)domsrc.getNode();
  113. final DOM2SAX dom2sax = new DOM2SAX(dom);
  114. xsltc.setXMLReader(dom2sax);
  115. // Try to get SAX InputSource from DOM Source.
  116. input = SAXSource.sourceToInputSource(source);
  117. if (input == null){
  118. input = new InputSource(domsrc.getSystemId());
  119. }
  120. }
  121. // Try to get InputStream or Reader from StreamSource
  122. else if (source instanceof StreamSource) {
  123. final StreamSource stream = (StreamSource)source;
  124. final InputStream istream = stream.getInputStream();
  125. final Reader reader = stream.getReader();
  126. // Create InputSource from Reader or InputStream in Source
  127. if (istream != null) {
  128. input = new InputSource(istream);
  129. }
  130. else if (reader != null) {
  131. input = new InputSource(reader);
  132. }
  133. else {
  134. input = new InputSource(systemId);
  135. }
  136. }
  137. else {
  138. ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR);
  139. throw new TransformerConfigurationException(err.toString());
  140. }
  141. input.setSystemId(systemId);
  142. }
  143. catch (NullPointerException e) {
  144. ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR,
  145. "TransformerFactory.newTemplates()");
  146. throw new TransformerConfigurationException(err.toString());
  147. }
  148. catch (SecurityException e) {
  149. ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId);
  150. throw new TransformerConfigurationException(err.toString());
  151. }
  152. return input;
  153. }
  154. }