001: /*
002: * $Id: StreamSource.java,v 1.6 2002/02/08 18:51:10 db Exp $
003: * Copyright (C) 2001 Andrew Selkirk
004: * Copyright (C) 2001 David Brownell
005: *
006: * This file is part of GNU JAXP, a library.
007: *
008: * GNU JAXP is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNU JAXP is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: *
022: * As a special exception, if you link this library with other files to
023: * produce an executable, this library does not by itself cause the
024: * resulting executable to be covered by the GNU General Public License.
025: * This exception does not however invalidate any other reasons why the
026: * executable file might be covered by the GNU General Public License.
027: */
028: package javax.xml.transform.stream;
029:
030: import java.io.InputStream;
031: import java.io.Reader;
032: import java.io.File;
033: import java.io.IOException;
034: import javax.xml.transform.Source;
035:
036: /**
037: * Stream Source
038: * @author Andrew Selkirk
039: * @version 1.0
040: */
041: public class StreamSource implements Source {
042:
043: //-------------------------------------------------------------
044: // Variables --------------------------------------------------
045: //-------------------------------------------------------------
046:
047: public static final String FEATURE = "http://javax.xml.transform.stream.StreamSource/feature";
048:
049: private String publicId = null;
050: private String systemId = null;
051: private InputStream inputStream = null;
052: private Reader reader = null;
053:
054: //-------------------------------------------------------------
055: // Initialization ---------------------------------------------
056: //-------------------------------------------------------------
057:
058: public StreamSource() {
059: }
060:
061: public StreamSource(File file) {
062: setSystemId(file);
063: }
064:
065: public StreamSource(InputStream stream) {
066: this .inputStream = stream;
067: }
068:
069: public StreamSource(InputStream stream, String systemID) {
070: this .inputStream = stream;
071: this .systemId = systemID;
072: }
073:
074: public StreamSource(Reader reader) {
075: this .reader = reader;
076: }
077:
078: public StreamSource(Reader reader, String systemID) {
079: this .reader = reader;
080: this .systemId = systemID;
081: }
082:
083: public StreamSource(String systemID) {
084: this .systemId = systemID;
085: }
086:
087: //-------------------------------------------------------------
088: // Methods ----------------------------------------------------
089: //-------------------------------------------------------------
090:
091: public InputStream getInputStream() {
092: return inputStream;
093: }
094:
095: public String getPublicId() {
096: return publicId;
097: }
098:
099: public Reader getReader() {
100: return reader;
101: }
102:
103: public String getSystemId() {
104: return systemId;
105: }
106:
107: public void setInputStream(InputStream stream) {
108: this .inputStream = stream;
109: }
110:
111: public void setPublicId(String publicID) {
112: this .publicId = publicID;
113: }
114:
115: public void setReader(Reader reader) {
116: this .reader = reader;
117: }
118:
119: public void setSystemId(File file) {
120: try {
121: this .systemId = fileToURL(file).toString();
122: } catch (IOException e) {
123: // can't happen
124: throw new RuntimeException(e.getMessage());
125: }
126: }
127:
128: public void setSystemId(String systemID) {
129: this .systemId = systemID;
130: }
131:
132: // we don't demand jdk 1.2 File.toURL() in the runtime
133: // keep in sync with gnu.xml.util.Resolver
134: // and javax.xml.parsers.DocumentBuilder
135: static String fileToURL(File f) throws IOException {
136: String temp;
137:
138: // FIXME: getAbsolutePath() seems buggy; I'm seeing components
139: // like "/foo/../" which are clearly not "absolute"
140: // and should have been resolved with the filesystem.
141:
142: // Substituting "/" would be wrong, "foo" may have been
143: // symlinked ... the URL code will make that change
144: // later, so that things can get _really_ broken!
145:
146: temp = f.getAbsolutePath();
147:
148: if (File.separatorChar != '/')
149: temp = temp.replace(File.separatorChar, '/');
150: if (!temp.startsWith("/"))
151: temp = "/" + temp;
152: if (!temp.endsWith("/") && f.isDirectory())
153: temp = temp + "/";
154: return "file:" + temp;
155: }
156: }
|