001: /*
002: * $Id: DocumentEntity.java,v 1.7 2004/07/08 08:03:04 yuvalo Exp $
003: *
004: * (C) Copyright 2002-2004 by Yuval Oren. All rights reserved.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package com.bluecast.xml;
020:
021: import java.io.*;
022: import org.xml.sax.*;
023: import com.bluecast.util.*;
024: import java.net.*;
025:
026: public class DocumentEntity implements Entity {
027: private boolean isOpen = false;
028: private URL url = null;
029: private String sysID = null;
030: private InputSource source = null;
031: static private URL defaultContext;
032: private boolean isStandalone = false;
033: private XMLStreamReader streamReader = null;
034: private XMLReaderReader readerReader = null;
035: private XMLInputReader activeReader = null;
036:
037: static {
038: try {
039: defaultContext = new URL("file", null, ".");
040: } catch (IOException e) {
041: defaultContext = null;
042: }
043: }
044:
045: public DocumentEntity() {
046: }
047:
048: public DocumentEntity(String sysID) throws IOException {
049: reset(sysID);
050: }
051:
052: public DocumentEntity(InputSource source) throws IOException {
053: reset(source);
054: }
055:
056: public boolean isOpen() {
057: return isOpen;
058: }
059:
060: public void open() throws IOException, RecursionException {
061: String encoding = null;
062:
063: // Were we given an InputSource?
064: if (source != null) {
065: // Try first to get a Reader
066: Reader sourceReader = source.getCharacterStream();
067: if (sourceReader != null) {
068: isOpen = true;
069: if (readerReader == null)
070: readerReader = new XMLReaderReader();
071:
072: readerReader.reset(sourceReader, true);
073: isStandalone = readerReader.isXMLStandalone();
074: activeReader = readerReader;
075:
076: return;
077: }
078: // Next try to get an InputStream
079: InputStream in = source.getByteStream();
080: if (in != null) {
081: if (streamReader == null)
082: streamReader = new XMLStreamReader();
083: streamReader.reset(in, source.getEncoding(), true);
084: isOpen = true;
085: isStandalone = streamReader.isXMLStandalone();
086: activeReader = streamReader;
087: return;
088: }
089: // Otherwise use the system ID
090: url = new URL(defaultContext, source.getSystemId());
091: sysID = url.toString();
092: encoding = source.getEncoding();
093: }
094:
095: if (streamReader == null)
096: streamReader = new XMLStreamReader();
097: streamReader.reset(url.openStream(), encoding, true);
098: isStandalone = streamReader.isXMLStandalone();
099: activeReader = streamReader;
100: isOpen = true;
101: }
102:
103: public String getDeclaredEncoding() {
104: return activeReader.getXMLDeclaredEncoding();
105: }
106:
107: public boolean isStandaloneDeclared() {
108: return activeReader.isXMLStandaloneDeclared();
109: }
110:
111: public String getXMLVersion() {
112: return activeReader.getXMLVersion();
113: }
114:
115: public void reset(String sysID) throws IOException {
116: close();
117: isStandalone = false;
118: this .source = null;
119: try {
120: this .url = new URL(defaultContext, sysID);
121: } catch (MalformedURLException e) {
122: this .url = new File(sysID).toURL();
123: }
124:
125: this .sysID = url.toString();
126: }
127:
128: public void reset(InputSource source) throws IOException {
129: close();
130: isStandalone = false;
131: this .source = source;
132: sysID = source.getSystemId();
133: if (sysID != null) {
134: try {
135: url = new URL(defaultContext, sysID);
136: } catch (MalformedURLException e) {
137: url = new File(sysID).toURL();
138: }
139:
140: this .sysID = url.toString();
141: }
142: }
143:
144: public void close() throws IOException {
145: if (!isOpen)
146: return;
147: source = null;
148: activeReader.close();
149: activeReader = null;
150: isOpen = false;
151: }
152:
153: public String getPublicID() {
154: return null;
155: }
156:
157: public String getSystemID() {
158: return sysID;
159: }
160:
161: public boolean isStandalone() {
162: return isStandalone;
163: }
164:
165: public void setStandalone(boolean standalone) {
166: isStandalone = standalone;
167: }
168:
169: public boolean isInternal() {
170: return false;
171: }
172:
173: public boolean isParsed() {
174: return true;
175: }
176:
177: public Reader getReader() {
178: return activeReader;
179: }
180:
181: public String stringValue() {
182: throw new UnsupportedOperationException();
183: }
184:
185: public char[] charArrayValue() {
186: throw new UnsupportedOperationException();
187: }
188: }
|