1. // $Id: SAXSource.java,v 1.7.14.1.2.2 2004/07/13 22:27:50 jsuttor Exp $
  2. /*
  3. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  4. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  5. */
  6. /*
  7. * @(#)SAXSource.java 1.15 04/07/13
  8. */
  9. package javax.xml.transform.sax;
  10. import javax.xml.transform.Source;
  11. import javax.xml.transform.stream.StreamSource;
  12. import org.xml.sax.InputSource;
  13. import org.xml.sax.XMLReader;
  14. /**
  15. * <p>Acts as an holder for SAX-style Source.</p>
  16. *
  17. * <p>Note that XSLT requires namespace support. Attempting to transform an
  18. * input source that is not
  19. * generated with a namespace-aware parser may result in errors.
  20. * Parsers can be made namespace aware by calling the
  21. * {@link javax.xml.parsers.SAXParserFactory#setNamespaceAware(boolean awareness)} method.</p>
  22. *
  23. * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
  24. * @version $Revision: 1.7.14.1.2.2 $, $Date: 2004/07/13 22:27:50 $
  25. */
  26. public class SAXSource implements Source {
  27. /**
  28. * If {@link javax.xml.transform.TransformerFactory#getFeature}
  29. * returns true when passed this value as an argument,
  30. * the Transformer supports Source input of this type.
  31. */
  32. public static final String FEATURE =
  33. "http://javax.xml.transform.sax.SAXSource/feature";
  34. /**
  35. * <p>Zero-argument default constructor. If this constructor is used, and
  36. * no SAX source is set using
  37. * {@link #setInputSource(InputSource inputSource)} , then the
  38. * <code>Transformer</code> will
  39. * create an empty source {@link org.xml.sax.InputSource} using
  40. * {@link org.xml.sax.InputSource#InputSource() new InputSource()}.</p>
  41. *
  42. * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
  43. */
  44. public SAXSource() { }
  45. /**
  46. * Create a <code>SAXSource</code>, using an {@link org.xml.sax.XMLReader}
  47. * and a SAX InputSource. The {@link javax.xml.transform.Transformer}
  48. * or {@link javax.xml.transform.sax.SAXTransformerFactory} will set itself
  49. * to be the reader's {@link org.xml.sax.ContentHandler}, and then will call
  50. * reader.parse(inputSource).
  51. *
  52. * @param reader An XMLReader to be used for the parse.
  53. * @param inputSource A SAX input source reference that must be non-null
  54. * and that will be passed to the reader parse method.
  55. */
  56. public SAXSource(XMLReader reader, InputSource inputSource) {
  57. this.reader = reader;
  58. this.inputSource = inputSource;
  59. }
  60. /**
  61. * Create a <code>SAXSource</code>, using a SAX <code>InputSource</code>.
  62. * The {@link javax.xml.transform.Transformer} or
  63. * {@link javax.xml.transform.sax.SAXTransformerFactory} creates a
  64. * reader via {@link org.xml.sax.helpers.XMLReaderFactory}
  65. * (if setXMLReader is not used), sets itself as
  66. * the reader's {@link org.xml.sax.ContentHandler}, and calls
  67. * reader.parse(inputSource).
  68. *
  69. * @param inputSource An input source reference that must be non-null
  70. * and that will be passed to the parse method of the reader.
  71. */
  72. public SAXSource(InputSource inputSource) {
  73. this.inputSource = inputSource;
  74. }
  75. /**
  76. * Set the XMLReader to be used for the Source.
  77. *
  78. * @param reader A valid XMLReader or XMLFilter reference.
  79. */
  80. public void setXMLReader(XMLReader reader) {
  81. this.reader = reader;
  82. }
  83. /**
  84. * Get the XMLReader to be used for the Source.
  85. *
  86. * @return A valid XMLReader or XMLFilter reference, or null.
  87. */
  88. public XMLReader getXMLReader() {
  89. return reader;
  90. }
  91. /**
  92. * Set the SAX InputSource to be used for the Source.
  93. *
  94. * @param inputSource A valid InputSource reference.
  95. */
  96. public void setInputSource(InputSource inputSource) {
  97. this.inputSource = inputSource;
  98. }
  99. /**
  100. * Get the SAX InputSource to be used for the Source.
  101. *
  102. * @return A valid InputSource reference, or null.
  103. */
  104. public InputSource getInputSource() {
  105. return inputSource;
  106. }
  107. /**
  108. * Set the system identifier for this Source. If an input source
  109. * has already been set, it will set the system ID or that
  110. * input source, otherwise it will create a new input source.
  111. *
  112. * <p>The system identifier is optional if there is a byte stream
  113. * or a character stream, but it is still useful to provide one,
  114. * since the application can use it to resolve relative URIs
  115. * and can include it in error messages and warnings (the parser
  116. * will attempt to open a connection to the URI only if
  117. * no byte stream or character stream is specified).</p>
  118. *
  119. * @param systemId The system identifier as a URI string.
  120. */
  121. public void setSystemId(String systemId) {
  122. if (null == inputSource) {
  123. inputSource = new InputSource(systemId);
  124. } else {
  125. inputSource.setSystemId(systemId);
  126. }
  127. }
  128. /**
  129. * <p>Get the base ID (URI or system ID) from where URIs
  130. * will be resolved.</p>
  131. *
  132. * @return Base URL for the <code>Source</code>, or <code>null</code>.
  133. */
  134. public String getSystemId() {
  135. if (inputSource == null) {
  136. return null;
  137. } else {
  138. return inputSource.getSystemId();
  139. }
  140. }
  141. /**
  142. * The XMLReader to be used for the source tree input. May be null.
  143. */
  144. private XMLReader reader;
  145. /**
  146. * <p>The SAX InputSource to be used for the source tree input.
  147. * Should not be <code>null<code>.</p>
  148. */
  149. private InputSource inputSource;
  150. /**
  151. * Attempt to obtain a SAX InputSource object from a Source
  152. * object.
  153. *
  154. * @param source Must be a non-null Source reference.
  155. *
  156. * @return An InputSource, or null if Source can not be converted.
  157. */
  158. public static InputSource sourceToInputSource(Source source) {
  159. if (source instanceof SAXSource) {
  160. return ((SAXSource) source).getInputSource();
  161. } else if (source instanceof StreamSource) {
  162. StreamSource ss = (StreamSource) source;
  163. InputSource isource = new InputSource(ss.getSystemId());
  164. isource.setByteStream(ss.getInputStream());
  165. isource.setCharacterStream(ss.getReader());
  166. isource.setPublicId(ss.getPublicId());
  167. return isource;
  168. } else {
  169. return null;
  170. }
  171. }
  172. }