1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.xerces.internal.dom;
23 
24 import org.w3c.dom.ls.LSInput;
25 
26 import java.io.Reader;
27 import java.io.InputStream;
28 
29 /**
30  * This Class <code>DOMInputImpl</code> represents a single input source for an XML entity.
31  * <p> This Class allows an application to encapsulate information about
32  * an input source in a single object, which may include a public
33  * identifier, a system identifier, a byte stream (possibly with a specified
34  * encoding), and/or a character stream.
35  * <p> The exact definitions of a byte stream and a character stream are
36  * binding dependent.
37  * <p> There are two places that the application will deliver this input
38  * source to the parser: as the argument to the <code>parse</code> method,
39  * or as the return value of the <code>DOMResourceResolver.resolveEntity</code>
40  *  method.
41  * <p> The <code>DOMParser</code> will use the <code>LSInput</code>
42  * object to determine how to read XML input. If there is a character stream
43  * available, the parser will read that stream directly; if not, the parser
44  * will use a byte stream, if available; if neither a character stream nor a
45  * byte stream is available, the parser will attempt to open a URI
46  * connection to the resource identified by the system identifier.
47  * <p> An <code>LSInput</code> object belongs to the application: the
48  * parser shall never modify it in any way (it may modify a copy if
49  * necessary).  Eventhough all attributes in this interface are writable the
50  * DOM implementation is expected to never mutate a LSInput.
51  * <p>See also the <a href='http://www.w3.org/TR/2001/WD-DOM-Level-3-ASLS-20011025'>Document Object Model (DOM) Level 3 Abstract Schemas and Load
52 and Save Specification</a>.
53  *
54  * @xerces.internal
55  *
56  * @author Gopal Sharma, SUN Microsystems Inc.
57  */
58 
59 // REVISIT:
60 // 1. it should be possible to do the following
61 // DOMInputImpl extends XMLInputSource implements LSInput
62 // 2. we probably need only the default constructor.  -- el
63 
64 public class DOMInputImpl implements LSInput {
65 
66         //
67         // Data
68         //
69 
70         protected String fPublicId = null;
71         protected String fSystemId = null;
72         protected String fBaseSystemId = null;
73 
74         protected InputStream fByteStream = null;
75         protected Reader fCharStream    = null;
76         protected String fData = null;
77 
78         protected String fEncoding = null;
79 
80         protected boolean fCertifiedText = false;
81 
82    /**
83      * Default Constructor, constructs an input source
84      *
85      *
86      */
DOMInputImpl()87      public DOMInputImpl() {}
88 
89    /**
90      * Constructs an input source from just the public and system
91      * identifiers, leaving resolution of the entity and opening of
92      * the input stream up to the caller.
93      *
94      * @param publicId     The public identifier, if known.
95      * @param systemId     The system identifier. This value should
96      *                     always be set, if possible, and can be
97      *                     relative or absolute. If the system identifier
98      *                     is relative, then the base system identifier
99      *                     should be set.
100      * @param baseSystemId The base system identifier. This value should
101      *                     always be set to the fully expanded URI of the
102      *                     base system identifier, if possible.
103      */
104 
DOMInputImpl(String publicId, String systemId, String baseSystemId)105     public DOMInputImpl(String publicId, String systemId,
106                           String baseSystemId) {
107 
108                 fPublicId = publicId;
109                 fSystemId = systemId;
110                 fBaseSystemId = baseSystemId;
111 
112     } // DOMInputImpl(String,String,String)
113 
114     /**
115      * Constructs an input source from a byte stream.
116      *
117      * @param publicId     The public identifier, if known.
118      * @param systemId     The system identifier. This value should
119      *                     always be set, if possible, and can be
120      *                     relative or absolute. If the system identifier
121      *                     is relative, then the base system identifier
122      *                     should be set.
123      * @param baseSystemId The base system identifier. This value should
124      *                     always be set to the fully expanded URI of the
125      *                     base system identifier, if possible.
126      * @param byteStream   The byte stream.
127      * @param encoding     The encoding of the byte stream, if known.
128      */
129 
DOMInputImpl(String publicId, String systemId, String baseSystemId, InputStream byteStream, String encoding)130     public DOMInputImpl(String publicId, String systemId,
131                           String baseSystemId, InputStream byteStream,
132                           String encoding) {
133 
134                 fPublicId = publicId;
135                 fSystemId = systemId;
136                 fBaseSystemId = baseSystemId;
137                 fByteStream = byteStream;
138                 fEncoding = encoding;
139 
140     } // DOMInputImpl(String,String,String,InputStream,String)
141 
142    /**
143      * Constructs an input source from a character stream.
144      *
145      * @param publicId     The public identifier, if known.
146      * @param systemId     The system identifier. This value should
147      *                     always be set, if possible, and can be
148      *                     relative or absolute. If the system identifier
149      *                     is relative, then the base system identifier
150      *                     should be set.
151      * @param baseSystemId The base system identifier. This value should
152      *                     always be set to the fully expanded URI of the
153      *                     base system identifier, if possible.
154      * @param charStream   The character stream.
155      * @param encoding     The original encoding of the byte stream
156      *                     used by the reader, if known.
157      */
158 
DOMInputImpl(String publicId, String systemId, String baseSystemId, Reader charStream, String encoding)159      public DOMInputImpl(String publicId, String systemId,
160                           String baseSystemId, Reader charStream,
161                           String encoding) {
162 
163                 fPublicId = publicId;
164                 fSystemId = systemId;
165                 fBaseSystemId = baseSystemId;
166                 fCharStream = charStream;
167                 fEncoding = encoding;
168 
169      } // DOMInputImpl(String,String,String,Reader,String)
170 
171    /**
172      * Constructs an input source from a String.
173      *
174      * @param publicId     The public identifier, if known.
175      * @param systemId     The system identifier. This value should
176      *                     always be set, if possible, and can be
177      *                     relative or absolute. If the system identifier
178      *                     is relative, then the base system identifier
179      *                     should be set.
180      * @param baseSystemId The base system identifier. This value should
181      *                     always be set to the fully expanded URI of the
182      *                     base system identifier, if possible.
183      * @param data                 The String Data.
184      * @param encoding     The original encoding of the byte stream
185      *                     used by the reader, if known.
186      */
187 
DOMInputImpl(String publicId, String systemId, String baseSystemId, String data, String encoding)188      public DOMInputImpl(String publicId, String systemId,
189                           String baseSystemId, String data,
190                           String encoding) {
191                 fPublicId = publicId;
192                 fSystemId = systemId;
193                 fBaseSystemId = baseSystemId;
194                 fData = data;
195                 fEncoding = encoding;
196      } // DOMInputImpl(String,String,String,String,String)
197 
198    /**
199      * An attribute of a language-binding dependent type that represents a
200      * stream of bytes.
201      * <br>The parser will ignore this if there is also a character stream
202      * specified, but it will use a byte stream in preference to opening a
203      * URI connection itself.
204      * <br>If the application knows the character encoding of the byte stream,
205      * it should set the encoding property. Setting the encoding in this way
206      * will override any encoding specified in the XML declaration itself.
207      */
208 
getByteStream()209     public InputStream getByteStream(){
210         return fByteStream;
211     }
212 
213     /**
214      * An attribute of a language-binding dependent type that represents a
215      * stream of bytes.
216      * <br>The parser will ignore this if there is also a character stream
217      * specified, but it will use a byte stream in preference to opening a
218      * URI connection itself.
219      * <br>If the application knows the character encoding of the byte stream,
220      * it should set the encoding property. Setting the encoding in this way
221      * will override any encoding specified in the XML declaration itself.
222      */
223 
setByteStream(InputStream byteStream)224      public void setByteStream(InputStream byteStream){
225         fByteStream = byteStream;
226      }
227 
228     /**
229      *  An attribute of a language-binding dependent type that represents a
230      * stream of 16-bit units. Application must encode the stream using
231      * UTF-16 (defined in  and Amendment 1 of ).
232      * <br>If a character stream is specified, the parser will ignore any byte
233      * stream and will not attempt to open a URI connection to the system
234      * identifier.
235      */
getCharacterStream()236     public Reader getCharacterStream(){
237         return fCharStream;
238     }
239     /**
240      *  An attribute of a language-binding dependent type that represents a
241      * stream of 16-bit units. Application must encode the stream using
242      * UTF-16 (defined in  and Amendment 1 of ).
243      * <br>If a character stream is specified, the parser will ignore any byte
244      * stream and will not attempt to open a URI connection to the system
245      * identifier.
246      */
247 
setCharacterStream(Reader characterStream)248      public void setCharacterStream(Reader characterStream){
249         fCharStream = characterStream;
250      }
251 
252     /**
253      * A string attribute that represents a sequence of 16 bit units (utf-16
254      * encoded characters).
255      * <br>If string data is available in the input source, the parser will
256      * ignore the character stream and the byte stream and will not attempt
257      * to open a URI connection to the system identifier.
258      */
getStringData()259     public String getStringData(){
260         return fData;
261     }
262 
263    /**
264      * A string attribute that represents a sequence of 16 bit units (utf-16
265      * encoded characters).
266      * <br>If string data is available in the input source, the parser will
267      * ignore the character stream and the byte stream and will not attempt
268      * to open a URI connection to the system identifier.
269      */
270 
setStringData(String stringData)271      public void setStringData(String stringData){
272                 fData = stringData;
273      }
274 
275     /**
276      *  The character encoding, if known. The encoding must be a string
277      * acceptable for an XML encoding declaration ( section 4.3.3 "Character
278      * Encoding in Entities").
279      * <br>This attribute has no effect when the application provides a
280      * character stream. For other sources of input, an encoding specified
281      * by means of this attribute will override any encoding specified in
282      * the XML claration or the Text Declaration, or an encoding obtained
283      * from a higher level protocol, such as HTTP .
284      */
285 
getEncoding()286     public String getEncoding(){
287         return fEncoding;
288     }
289 
290     /**
291      *  The character encoding, if known. The encoding must be a string
292      * acceptable for an XML encoding declaration ( section 4.3.3 "Character
293      * Encoding in Entities").
294      * <br>This attribute has no effect when the application provides a
295      * character stream. For other sources of input, an encoding specified
296      * by means of this attribute will override any encoding specified in
297      * the XML claration or the Text Declaration, or an encoding obtained
298      * from a higher level protocol, such as HTTP .
299      */
setEncoding(String encoding)300     public void setEncoding(String encoding){
301         fEncoding = encoding;
302     }
303 
304     /**
305      * The public identifier for this input source. The public identifier is
306      * always optional: if the application writer includes one, it will be
307      * provided as part of the location information.
308      */
getPublicId()309     public String getPublicId(){
310         return fPublicId;
311     }
312     /**
313      * The public identifier for this input source. The public identifier is
314      * always optional: if the application writer includes one, it will be
315      * provided as part of the location information.
316      */
setPublicId(String publicId)317     public void setPublicId(String publicId){
318         fPublicId = publicId;
319     }
320 
321     /**
322      * The system identifier, a URI reference , for this input source. The
323      * system identifier is optional if there is a byte stream or a
324      * character stream, but it is still useful to provide one, since the
325      * application can use it to resolve relative URIs and can include it in
326      * error messages and warnings (the parser will attempt to fetch the
327      * ressource identifier by the URI reference only if there is no byte
328      * stream or character stream specified).
329      * <br>If the application knows the character encoding of the object
330      * pointed to by the system identifier, it can register the encoding by
331      * setting the encoding attribute.
332      * <br>If the system ID is a relative URI reference (see section 5 in ),
333      * the behavior is implementation dependent.
334      */
getSystemId()335     public String getSystemId(){
336         return fSystemId;
337     }
338     /**
339      * The system identifier, a URI reference , for this input source. The
340      * system identifier is optional if there is a byte stream or a
341      * character stream, but it is still useful to provide one, since the
342      * application can use it to resolve relative URIs and can include it in
343      * error messages and warnings (the parser will attempt to fetch the
344      * ressource identifier by the URI reference only if there is no byte
345      * stream or character stream specified).
346      * <br>If the application knows the character encoding of the object
347      * pointed to by the system identifier, it can register the encoding by
348      * setting the encoding attribute.
349      * <br>If the system ID is a relative URI reference (see section 5 in ),
350      * the behavior is implementation dependent.
351      */
setSystemId(String systemId)352     public void setSystemId(String systemId){
353         fSystemId = systemId;
354     }
355 
356     /**
357      *  The base URI to be used (see section 5.1.4 in ) for resolving relative
358      * URIs to absolute URIs. If the baseURI is itself a relative URI, the
359      * behavior is implementation dependent.
360      */
getBaseURI()361     public String getBaseURI(){
362         return fBaseSystemId;
363     }
364     /**
365      *  The base URI to be used (see section 5.1.4 in ) for resolving relative
366      * URIs to absolute URIs. If the baseURI is itself a relative URI, the
367      * behavior is implementation dependent.
368      */
setBaseURI(String baseURI)369     public void setBaseURI(String baseURI){
370         fBaseSystemId = baseURI;
371     }
372 
373     /**
374       *  If set to true, assume that the input is certified (see section 2.13
375       * in [<a href='http://www.w3.org/TR/2002/CR-xml11-20021015/'>XML 1.1</a>]) when
376       * parsing [<a href='http://www.w3.org/TR/2002/CR-xml11-20021015/'>XML 1.1</a>].
377       */
getCertifiedText()378     public boolean getCertifiedText(){
379       return fCertifiedText;
380     }
381 
382     /**
383       *  If set to true, assume that the input is certified (see section 2.13
384       * in [<a href='http://www.w3.org/TR/2002/CR-xml11-20021015/'>XML 1.1</a>]) when
385       * parsing [<a href='http://www.w3.org/TR/2002/CR-xml11-20021015/'>XML 1.1</a>].
386       */
387 
setCertifiedText(boolean certifiedText)388     public void setCertifiedText(boolean certifiedText){
389       fCertifiedText = certifiedText;
390     }
391 
392 }// class DOMInputImpl
393