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