View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2005 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package com.sun.org.apache.xerces.internal.util;
22  
23  import java.io.InputStream;
24  import java.io.Reader;
25  
26  import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
27  import org.xml.sax.InputSource;
28  import org.xml.sax.XMLReader;
29  
30  /**
31   * <p>An <code>XMLInputSource</code> analogue to <code>javax.xml.transform.sax.SAXSource</code>.</p>
32   *
33   */
34  public final class SAXInputSource extends XMLInputSource {
35  
36      private XMLReader fXMLReader;
37      private InputSource fInputSource;
38  
39      public SAXInputSource() {
40          this(null);
41      }
42  
43      public SAXInputSource(InputSource inputSource) {
44          this(null, inputSource);
45      }
46  
47      public SAXInputSource(XMLReader reader, InputSource inputSource) {
48          super(inputSource != null ? inputSource.getPublicId() : null,
49                  inputSource != null ? inputSource.getSystemId() : null, null);
50          if (inputSource != null) {
51              setByteStream(inputSource.getByteStream());
52              setCharacterStream(inputSource.getCharacterStream());
53              setEncoding(inputSource.getEncoding());
54          }
55          fInputSource = inputSource;
56          fXMLReader = reader;
57      }
58  
59      public void setXMLReader(XMLReader reader) {
60          fXMLReader = reader;
61      }
62  
63      public XMLReader getXMLReader() {
64          return fXMLReader;
65      }
66  
67      public void setInputSource(InputSource inputSource) {
68          if (inputSource != null) {
69              setPublicId(inputSource.getPublicId());
70              setSystemId(inputSource.getSystemId());
71              setByteStream(inputSource.getByteStream());
72              setCharacterStream(inputSource.getCharacterStream());
73              setEncoding(inputSource.getEncoding());
74          }
75          else {
76              setPublicId(null);
77              setSystemId(null);
78              setByteStream(null);
79              setCharacterStream(null);
80              setEncoding(null);
81          }
82          fInputSource = inputSource;
83      }
84  
85      public InputSource getInputSource() {
86          return fInputSource;
87      }
88  
89      /**
90       * Sets the public identifier.
91       *
92       * @param publicId The new public identifier.
93       */
94      public void setPublicId(String publicId) {
95          super.setPublicId(publicId);
96          if (fInputSource == null) {
97              fInputSource = new InputSource();
98          }
99          fInputSource.setPublicId(publicId);
100     } // setPublicId(String)
101 
102     /**
103      * Sets the system identifier.
104      *
105      * @param systemId The new system identifier.
106      */
107     public void setSystemId(String systemId) {
108         super.setSystemId(systemId);
109         if (fInputSource == null) {
110             fInputSource = new InputSource();
111         }
112         fInputSource.setSystemId(systemId);
113     } // setSystemId(String)
114 
115     /**
116      * Sets the byte stream. If the byte stream is not already opened
117      * when this object is instantiated, then the code that opens the
118      * stream should also set the byte stream on this object. Also, if
119      * the encoding is auto-detected, then the encoding should also be
120      * set on this object.
121      *
122      * @param byteStream The new byte stream.
123      */
124     public void setByteStream(InputStream byteStream) {
125         super.setByteStream(byteStream);
126         if (fInputSource == null) {
127             fInputSource = new InputSource();
128         }
129         fInputSource.setByteStream(byteStream);
130     } // setByteStream(InputStream)
131 
132     /**
133      * Sets the character stream. If the character stream is not already
134      * opened when this object is instantiated, then the code that opens
135      * the stream should also set the character stream on this object.
136      * Also, the encoding of the byte stream used by the reader should
137      * also be set on this object, if known.
138      *
139      * @param charStream The new character stream.
140      *
141      * @see #setEncoding
142      */
143     public void setCharacterStream(Reader charStream) {
144         super.setCharacterStream(charStream);
145         if (fInputSource == null) {
146             fInputSource = new InputSource();
147         }
148         fInputSource.setCharacterStream(charStream);
149     } // setCharacterStream(Reader)
150 
151     /**
152      * Sets the encoding of the stream.
153      *
154      * @param encoding The new encoding.
155      */
156     public void setEncoding(String encoding) {
157         super.setEncoding(encoding);
158         if (fInputSource == null) {
159             fInputSource = new InputSource();
160         }
161         fInputSource.setEncoding(encoding);
162     } // setEncoding(String)
163 
164 } // SAXInputSource