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