1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001, 2002 The Apache Software Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 2001, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.dom;
  58. import org.w3c.dom.ls.LSInput;
  59. import java.io.Reader;
  60. import java.io.InputStream;
  61. /**
  62. * This Class <code>DOMInputImpl</code> represents a single input source for an XML entity.
  63. * <p> This Class allows an application to encapsulate information about
  64. * an input source in a single object, which may include a public
  65. * identifier, a system identifier, a byte stream (possibly with a specified
  66. * encoding), and/or a character stream.
  67. * <p> The exact definitions of a byte stream and a character stream are
  68. * binding dependent.
  69. * <p> There are two places that the application will deliver this input
  70. * source to the parser: as the argument to the <code>parse</code> method,
  71. * or as the return value of the <code>DOMResourceResolver.resolveEntity</code>
  72. * method.
  73. * <p> The <code>DOMParser</code> will use the <code>LSInput</code>
  74. * object to determine how to read XML input. If there is a character stream
  75. * available, the parser will read that stream directly; if not, the parser
  76. * will use a byte stream, if available; if neither a character stream nor a
  77. * byte stream is available, the parser will attempt to open a URI
  78. * connection to the resource identified by the system identifier.
  79. * <p> An <code>LSInput</code> object belongs to the application: the
  80. * parser shall never modify it in any way (it may modify a copy if
  81. * necessary). Eventhough all attributes in this interface are writable the
  82. * DOM implementation is expected to never mutate a LSInput.
  83. * <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
  84. and Save Specification</a>.
  85. *
  86. *
  87. * @author Gopal Sharma, SUN Microsystems Inc.
  88. * @version $Id: DOMInputImpl.java,v 1.2 2003/11/17 13:48:40 venu Exp $
  89. */
  90. // REVISIT:
  91. // 1. it should be possible to do the following
  92. // DOMInputImpl extends XMLInputSource implements LSInput
  93. // 2. we probably need only the default constructor. -- el
  94. public class DOMInputImpl implements LSInput {
  95. //
  96. // Data
  97. //
  98. protected String fPublicId = null;
  99. protected String fSystemId = null;
  100. protected String fBaseSystemId = null;
  101. protected InputStream fByteStream = null;
  102. protected Reader fCharStream = null;
  103. protected String fData = null;
  104. protected String fEncoding = null;
  105. protected boolean fCertifiedText = false;
  106. /**
  107. * Default Constructor, constructs an input source
  108. *
  109. *
  110. */
  111. public DOMInputImpl() {}
  112. /**
  113. * Constructs an input source from just the public and system
  114. * identifiers, leaving resolution of the entity and opening of
  115. * the input stream up to the caller.
  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. */
  127. public DOMInputImpl(String publicId, String systemId,
  128. String baseSystemId) {
  129. fPublicId = publicId;
  130. fSystemId = systemId;
  131. fBaseSystemId = baseSystemId;
  132. } // DOMInputImpl(String,String,String)
  133. /**
  134. * Constructs an input source from a byte stream.
  135. *
  136. * @param publicId The public identifier, if known.
  137. * @param systemId The system identifier. This value should
  138. * always be set, if possible, and can be
  139. * relative or absolute. If the system identifier
  140. * is relative, then the base system identifier
  141. * should be set.
  142. * @param baseSystemId The base system identifier. This value should
  143. * always be set to the fully expanded URI of the
  144. * base system identifier, if possible.
  145. * @param byteStream The byte stream.
  146. * @param encoding The encoding of the byte stream, if known.
  147. */
  148. public DOMInputImpl(String publicId, String systemId,
  149. String baseSystemId, InputStream byteStream,
  150. String encoding) {
  151. fPublicId = publicId;
  152. fSystemId = systemId;
  153. fBaseSystemId = baseSystemId;
  154. fByteStream = byteStream;
  155. fEncoding = encoding;
  156. } // DOMInputImpl(String,String,String,InputStream,String)
  157. /**
  158. * Constructs an input source from a character stream.
  159. *
  160. * @param publicId The public identifier, if known.
  161. * @param systemId The system identifier. This value should
  162. * always be set, if possible, and can be
  163. * relative or absolute. If the system identifier
  164. * is relative, then the base system identifier
  165. * should be set.
  166. * @param baseSystemId The base system identifier. This value should
  167. * always be set to the fully expanded URI of the
  168. * base system identifier, if possible.
  169. * @param charStream The character stream.
  170. * @param encoding The original encoding of the byte stream
  171. * used by the reader, if known.
  172. */
  173. public DOMInputImpl(String publicId, String systemId,
  174. String baseSystemId, Reader charStream,
  175. String encoding) {
  176. fPublicId = publicId;
  177. fSystemId = systemId;
  178. fBaseSystemId = baseSystemId;
  179. fCharStream = charStream;
  180. fEncoding = encoding;
  181. } // DOMInputImpl(String,String,String,Reader,String)
  182. /**
  183. * Constructs an input source from a String.
  184. *
  185. * @param publicId The public identifier, if known.
  186. * @param systemId The system identifier. This value should
  187. * always be set, if possible, and can be
  188. * relative or absolute. If the system identifier
  189. * is relative, then the base system identifier
  190. * should be set.
  191. * @param baseSystemId The base system identifier. This value should
  192. * always be set to the fully expanded URI of the
  193. * base system identifier, if possible.
  194. * @param data The String Data.
  195. * @param encoding The original encoding of the byte stream
  196. * used by the reader, if known.
  197. */
  198. public DOMInputImpl(String publicId, String systemId,
  199. String baseSystemId, String data,
  200. String encoding) {
  201. fPublicId = publicId;
  202. fSystemId = systemId;
  203. fBaseSystemId = baseSystemId;
  204. fData = data;
  205. fEncoding = encoding;
  206. } // DOMInputImpl(String,String,String,String,String)
  207. /**
  208. * An attribute of a language-binding dependent type that represents a
  209. * stream of bytes.
  210. * <br>The parser will ignore this if there is also a character stream
  211. * specified, but it will use a byte stream in preference to opening a
  212. * URI connection itself.
  213. * <br>If the application knows the character encoding of the byte stream,
  214. * it should set the encoding property. Setting the encoding in this way
  215. * will override any encoding specified in the XML declaration itself.
  216. */
  217. public InputStream getByteStream(){
  218. return fByteStream;
  219. }
  220. /**
  221. * An attribute of a language-binding dependent type that represents a
  222. * stream of bytes.
  223. * <br>The parser will ignore this if there is also a character stream
  224. * specified, but it will use a byte stream in preference to opening a
  225. * URI connection itself.
  226. * <br>If the application knows the character encoding of the byte stream,
  227. * it should set the encoding property. Setting the encoding in this way
  228. * will override any encoding specified in the XML declaration itself.
  229. */
  230. public void setByteStream(InputStream byteStream){
  231. fByteStream = byteStream;
  232. }
  233. /**
  234. * An attribute of a language-binding dependent type that represents a
  235. * stream of 16-bit units. Application must encode the stream using
  236. * UTF-16 (defined in and Amendment 1 of ).
  237. * <br>If a character stream is specified, the parser will ignore any byte
  238. * stream and will not attempt to open a URI connection to the system
  239. * identifier.
  240. */
  241. public Reader getCharacterStream(){
  242. return fCharStream;
  243. }
  244. /**
  245. * An attribute of a language-binding dependent type that represents a
  246. * stream of 16-bit units. Application must encode the stream using
  247. * UTF-16 (defined in and Amendment 1 of ).
  248. * <br>If a character stream is specified, the parser will ignore any byte
  249. * stream and will not attempt to open a URI connection to the system
  250. * identifier.
  251. */
  252. public void setCharacterStream(Reader characterStream){
  253. fCharStream = characterStream;
  254. }
  255. /**
  256. * A string attribute that represents a sequence of 16 bit units (utf-16
  257. * encoded characters).
  258. * <br>If string data is available in the input source, the parser will
  259. * ignore the character stream and the byte stream and will not attempt
  260. * to open a URI connection to the system identifier.
  261. */
  262. public String getStringData(){
  263. return fData;
  264. }
  265. /**
  266. * A string attribute that represents a sequence of 16 bit units (utf-16
  267. * encoded characters).
  268. * <br>If string data is available in the input source, the parser will
  269. * ignore the character stream and the byte stream and will not attempt
  270. * to open a URI connection to the system identifier.
  271. */
  272. public void setStringData(String stringData){
  273. fData = stringData;
  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. public String getEncoding(){
  286. return fEncoding;
  287. }
  288. /**
  289. * The character encoding, if known. The encoding must be a string
  290. * acceptable for an XML encoding declaration ( section 4.3.3 "Character
  291. * Encoding in Entities").
  292. * <br>This attribute has no effect when the application provides a
  293. * character stream. For other sources of input, an encoding specified
  294. * by means of this attribute will override any encoding specified in
  295. * the XML claration or the Text Declaration, or an encoding obtained
  296. * from a higher level protocol, such as HTTP .
  297. */
  298. public void setEncoding(String encoding){
  299. fEncoding = encoding;
  300. }
  301. /**
  302. * The public identifier for this input source. The public identifier is
  303. * always optional: if the application writer includes one, it will be
  304. * provided as part of the location information.
  305. */
  306. public String getPublicId(){
  307. return fPublicId;
  308. }
  309. /**
  310. * The public identifier for this input source. The public identifier is
  311. * always optional: if the application writer includes one, it will be
  312. * provided as part of the location information.
  313. */
  314. public void setPublicId(String publicId){
  315. fPublicId = publicId;
  316. }
  317. /**
  318. * The system identifier, a URI reference , for this input source. The
  319. * system identifier is optional if there is a byte stream or a
  320. * character stream, but it is still useful to provide one, since the
  321. * application can use it to resolve relative URIs and can include it in
  322. * error messages and warnings (the parser will attempt to fetch the
  323. * ressource identifier by the URI reference only if there is no byte
  324. * stream or character stream specified).
  325. * <br>If the application knows the character encoding of the object
  326. * pointed to by the system identifier, it can register the encoding by
  327. * setting the encoding attribute.
  328. * <br>If the system ID is a relative URI reference (see section 5 in ),
  329. * the behavior is implementation dependent.
  330. */
  331. public String getSystemId(){
  332. return fSystemId;
  333. }
  334. /**
  335. * The system identifier, a URI reference , for this input source. The
  336. * system identifier is optional if there is a byte stream or a
  337. * character stream, but it is still useful to provide one, since the
  338. * application can use it to resolve relative URIs and can include it in
  339. * error messages and warnings (the parser will attempt to fetch the
  340. * ressource identifier by the URI reference only if there is no byte
  341. * stream or character stream specified).
  342. * <br>If the application knows the character encoding of the object
  343. * pointed to by the system identifier, it can register the encoding by
  344. * setting the encoding attribute.
  345. * <br>If the system ID is a relative URI reference (see section 5 in ),
  346. * the behavior is implementation dependent.
  347. */
  348. public void setSystemId(String systemId){
  349. fSystemId = systemId;
  350. }
  351. /**
  352. * The base URI to be used (see section 5.1.4 in ) for resolving relative
  353. * URIs to absolute URIs. If the baseURI is itself a relative URI, the
  354. * behavior is implementation dependent.
  355. */
  356. public String getBaseURI(){
  357. return fBaseSystemId;
  358. }
  359. /**
  360. * The base URI to be used (see section 5.1.4 in ) for resolving relative
  361. * URIs to absolute URIs. If the baseURI is itself a relative URI, the
  362. * behavior is implementation dependent.
  363. */
  364. public void setBaseURI(String baseURI){
  365. fBaseSystemId = baseURI;
  366. }
  367. /**
  368. * If set to true, assume that the input is certified (see section 2.13
  369. * in [<a href='http://www.w3.org/TR/2002/CR-xml11-20021015/'>XML 1.1</a>]) when
  370. * parsing [<a href='http://www.w3.org/TR/2002/CR-xml11-20021015/'>XML 1.1</a>].
  371. */
  372. public boolean getCertifiedText(){
  373. return fCertifiedText;
  374. }
  375. /**
  376. * If set to true, assume that the input is certified (see section 2.13
  377. * in [<a href='http://www.w3.org/TR/2002/CR-xml11-20021015/'>XML 1.1</a>]) when
  378. * parsing [<a href='http://www.w3.org/TR/2002/CR-xml11-20021015/'>XML 1.1</a>].
  379. */
  380. public void setCertifiedText(boolean certifiedText){
  381. fCertifiedText = certifiedText;
  382. }
  383. }// class DOMInputImpl