1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2000-2002,2004 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.xni.parser;
  22 
  23 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
  24 import java.io.InputStream;
  25 import java.io.Reader;
  26 import org.xml.sax.InputSource;
  27 
  28 /**
  29  * This class represents an input source for an XML document. The
  30  * basic properties of an input source are the following:
  31  * <ul>
  32  *  <li>public identifier</li>
  33  *  <li>system identifier</li>
  34  *  <li>byte stream or character stream</li>
  35  *  <li>
  36  * </ul>
  37  *
  38  * @author Andy Clark, IBM
  39  *
  40  */
  41 public class XMLInputSource {
  42 
  43     //
  44     // Data
  45     //
  46 
  47     /** Public identifier. */
  48     protected String fPublicId;
  49 
  50     /** System identifier. */
  51     protected String fSystemId;
  52 
  53     /** Base system identifier. */
  54     protected String fBaseSystemId;
  55 
  56     /** Byte stream. */
  57     protected InputStream fByteStream;
  58 
  59     /** Character stream. */
  60     protected Reader fCharStream;
  61 
  62     /** Encoding. */
  63     protected String fEncoding;
  64 
  65     //indicates whether the source is created by a resolver
  66     boolean fIsCreatedByResolver = false;
  67     //
  68     // Constructors
  69     //
  70 
  71     /**
  72      * Constructs an input source from just the public and system
  73      * identifiers, leaving resolution of the entity and opening of
  74      * the input stream up to the caller.
  75      *
  76      * @param publicId     The public identifier, if known.
  77      * @param systemId     The system identifier. This value should
  78      *                     always be set, if possible, and can be
  79      *                     relative or absolute. If the system identifier
  80      *                     is relative, then the base system identifier
  81      *                     should be set.
  82      * @param baseSystemId The base system identifier. This value should
  83      *                     always be set to the fully expanded URI of the
  84      *                     base system identifier, if possible.
  85      * @param isCreatedByResolver a flag to indicate whether the source is
  86      * created by a resolver
  87      */
  88     public XMLInputSource(String publicId, String systemId,
  89                           String baseSystemId, boolean isCreatedByResolver) {
  90         fPublicId = publicId;
  91         fSystemId = systemId;
  92         fBaseSystemId = baseSystemId;
  93         fIsCreatedByResolver = isCreatedByResolver;
  94     } // <init>(String,String,String)
  95 
  96     /**
  97      * Constructs an input source from a XMLResourceIdentifier
  98      * object, leaving resolution of the entity and opening of
  99      * the input stream up to the caller.
 100      *
 101      * @param resourceIdentifier    the XMLResourceIdentifier containing the information
 102      */
 103     public XMLInputSource(XMLResourceIdentifier resourceIdentifier) {
 104 
 105         fPublicId = resourceIdentifier.getPublicId();
 106         fSystemId = resourceIdentifier.getLiteralSystemId();
 107         fBaseSystemId = resourceIdentifier.getBaseSystemId();
 108     } // <init>(XMLResourceIdentifier)
 109 
 110     /**
 111      * Constructs an input source from a SAX InputSource
 112      * object.
 113      *
 114      * @param inputSource  a SAX InputSource
 115      * @param isCreatedByResolver a flag to indicate whether the source is
 116      * created by a resolver
 117      */
 118     public XMLInputSource(InputSource inputSource, boolean isCreatedByResolver) {
 119         fPublicId = inputSource.getPublicId();
 120         fSystemId = inputSource.getSystemId();
 121         fByteStream = inputSource.getByteStream();
 122         fCharStream = inputSource.getCharacterStream();
 123         fEncoding = inputSource.getEncoding();
 124         fIsCreatedByResolver = isCreatedByResolver;
 125     }
 126 
 127     /**
 128      * Constructs an input source from a byte stream.
 129      *
 130      * @param publicId     The public identifier, if known.
 131      * @param systemId     The system identifier. This value should
 132      *                     always be set, if possible, and can be
 133      *                     relative or absolute. If the system identifier
 134      *                     is relative, then the base system identifier
 135      *                     should be set.
 136      * @param baseSystemId The base system identifier. This value should
 137      *                     always be set to the fully expanded URI of the
 138      *                     base system identifier, if possible.
 139      * @param byteStream   The byte stream.
 140      * @param encoding     The encoding of the byte stream, if known.
 141      */
 142     public XMLInputSource(String publicId, String systemId,
 143                           String baseSystemId, InputStream byteStream,
 144                           String encoding) {
 145         fPublicId = publicId;
 146         fSystemId = systemId;
 147         fBaseSystemId = baseSystemId;
 148         fByteStream = byteStream;
 149         fEncoding = encoding;
 150     } // <init>(String,String,String,InputStream,String)
 151 
 152     /**
 153      * Constructs an input source from a character stream.
 154      *
 155      * @param publicId     The public identifier, if known.
 156      * @param systemId     The system identifier. This value should
 157      *                     always be set, if possible, and can be
 158      *                     relative or absolute. If the system identifier
 159      *                     is relative, then the base system identifier
 160      *                     should be set.
 161      * @param baseSystemId The base system identifier. This value should
 162      *                     always be set to the fully expanded URI of the
 163      *                     base system identifier, if possible.
 164      * @param charStream   The character stream.
 165      * @param encoding     The original encoding of the byte stream
 166      *                     used by the reader, if known.
 167      */
 168     public XMLInputSource(String publicId, String systemId,
 169                           String baseSystemId, Reader charStream,
 170                           String encoding) {
 171         fPublicId = publicId;
 172         fSystemId = systemId;
 173         fBaseSystemId = baseSystemId;
 174         fCharStream = charStream;
 175         fEncoding = encoding;
 176     } // <init>(String,String,String,Reader,String)
 177 
 178     //
 179     // Public methods
 180     //
 181 
 182     /**
 183      * Sets the public identifier.
 184      *
 185      * @param publicId The new public identifier.
 186      */
 187     public void setPublicId(String publicId) {
 188         fPublicId = publicId;
 189     } // setPublicId(String)
 190 
 191     /** Returns the public identifier. */
 192     public String getPublicId() {
 193         return fPublicId;
 194     } // getPublicId():String
 195 
 196     /**
 197      * Sets the system identifier.
 198      *
 199      * @param systemId The new system identifier.
 200      */
 201     public void setSystemId(String systemId) {
 202         fSystemId = systemId;
 203     } // setSystemId(String)
 204 
 205     /** Returns the system identifier. */
 206     public String getSystemId() {
 207         return fSystemId;
 208     } // getSystemId():String
 209 
 210     /**
 211      * Sets the base system identifier.
 212      *
 213      * @param baseSystemId The new base system identifier.
 214      */
 215     public void setBaseSystemId(String baseSystemId) {
 216         fBaseSystemId = baseSystemId;
 217     } // setBaseSystemId(String)
 218 
 219     /** Returns the base system identifier. */
 220     public String getBaseSystemId() {
 221         return fBaseSystemId;
 222     } // getBaseSystemId():String
 223 
 224     /**
 225      * Sets the byte stream. If the byte stream is not already opened
 226      * when this object is instantiated, then the code that opens the
 227      * stream should also set the byte stream on this object. Also, if
 228      * the encoding is auto-detected, then the encoding should also be
 229      * set on this object.
 230      *
 231      * @param byteStream The new byte stream.
 232      */
 233     public void setByteStream(InputStream byteStream) {
 234         fByteStream = byteStream;
 235     } // setByteStream(InputSource)
 236 
 237     /** Returns the byte stream. */
 238     public InputStream getByteStream() {
 239         return fByteStream;
 240     } // getByteStream():InputStream
 241 
 242     /**
 243      * Sets the character stream. If the character stream is not already
 244      * opened when this object is instantiated, then the code that opens
 245      * the stream should also set the character stream on this object.
 246      * Also, the encoding of the byte stream used by the reader should
 247      * also be set on this object, if known.
 248      *
 249      * @param charStream The new character stream.
 250      *
 251      * @see #setEncoding
 252      */
 253     public void setCharacterStream(Reader charStream) {
 254         fCharStream = charStream;
 255     } // setCharacterStream(Reader)
 256 
 257     /** Returns the character stream. */
 258     public Reader getCharacterStream() {
 259         return fCharStream;
 260     } // getCharacterStream():Reader
 261 
 262     /**
 263      * Sets the encoding of the stream.
 264      *
 265      * @param encoding The new encoding.
 266      */
 267     public void setEncoding(String encoding) {
 268         fEncoding = encoding;
 269     } // setEncoding(String)
 270 
 271     /** Returns the encoding of the stream, or null if not known. */
 272     public String getEncoding() {
 273         return fEncoding;
 274     } // getEncoding():String
 275 
 276     /**
 277      * Sets the flag to indicate whether this source is created by a resolver
 278      * @param createdByResolver the flag
 279      */
 280     public void setCreatedByResolver(boolean createdByResolver) {
 281         fIsCreatedByResolver = createdByResolver;
 282     }
 283     /**
 284      * Returns a boolean to indicate whether this source is created by a resolver.
 285      * @return true if the source is created by a resolver, false otherwise
 286      */
 287     public boolean isCreatedByResolver() {
 288         return fIsCreatedByResolver;
 289     }
 290 
 291 } // class XMLInputSource