001: /*
002: * $Id: DocumentBuilder.java,v 1.8 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:
029: package javax.xml.parsers;
030:
031: // Imports
032: import java.io.File;
033: import java.io.FileInputStream;
034: import java.io.InputStream;
035: import java.io.IOException; // import java.net.*;
036: import org.w3c.dom.DOMImplementation;
037: import org.w3c.dom.Document;
038: import org.xml.sax.InputSource;
039: import org.xml.sax.EntityResolver;
040: import org.xml.sax.ErrorHandler;
041: import org.xml.sax.InputSource;
042: import org.xml.sax.SAXException;
043:
044: /**
045: * Uses an XML parser to construct a DOM document.
046: * @author Andrew Selkirk, David Brownell
047: * @version $Id: DocumentBuilder.java,v 1.8 2002/02/08 18:51:10 db Exp $
048: */
049: public abstract class DocumentBuilder {
050: /** Only subclasses may use the constructor. */
051: protected DocumentBuilder() {
052: }
053:
054: //-------------------------------------------------------------
055: // Methods ----------------------------------------------------
056: //-------------------------------------------------------------
057:
058: public abstract DOMImplementation getDOMImplementation();
059:
060: public abstract boolean isNamespaceAware();
061:
062: public abstract boolean isValidating();
063:
064: public abstract Document newDocument();
065:
066: // we don't demand jdk 1.2 File.toURL() in the runtime
067: // keep in sync with gnu.xml.util.Resolver
068: // and javax.xml.transform.stream.StreamSource
069: static String fileToURL(File f) throws IOException {
070: String temp;
071:
072: // FIXME: getAbsolutePath() seems buggy; I'm seeing components
073: // like "/foo/../" which are clearly not "absolute"
074: // and should have been resolved with the filesystem.
075:
076: // Substituting "/" would be wrong, "foo" may have been
077: // symlinked ... the URL code will make that change
078: // later, so that things can get _really_ broken!
079:
080: temp = f.getAbsolutePath();
081:
082: if (File.separatorChar != '/')
083: temp = temp.replace(File.separatorChar, '/');
084: if (!temp.startsWith("/"))
085: temp = "/" + temp;
086: if (!temp.endsWith("/") && f.isDirectory())
087: temp = temp + "/";
088: return "file:" + temp;
089: }
090:
091: /**
092: * Constructs an InputSource from the file, and invokes parse ().
093: * The InputSource includes the URI for the file.
094: */
095: public Document parse(File file) throws SAXException, IOException {
096: InputSource source;
097:
098: source = new InputSource(fileToURL(file));
099: source.setByteStream(new FileInputStream(file));
100: return parse(source);
101: }
102:
103: public abstract Document parse(InputSource source)
104: throws SAXException, IOException;
105:
106: /**
107: * Avoid using this call; provide the system ID wherever possible.
108: * System IDs are essential when parsers resolve relative URIs,
109: * or provide diagnostics.
110: */
111: public Document parse(InputStream stream) throws SAXException,
112: IOException {
113: return parse(new InputSource(stream));
114: } // parse()
115:
116: public Document parse(InputStream stream, String systemID)
117: throws SAXException, IOException {
118:
119: // Variables
120: InputSource source;
121:
122: // Create Source
123: source = new InputSource(stream);
124: source.setSystemId(systemID);
125:
126: // Parse Input Source
127: return parse(source);
128:
129: } // parse()
130:
131: public Document parse(String uri) throws SAXException, IOException {
132: return parse(new InputSource(uri));
133: } // parse()
134:
135: public abstract void setEntityResolver(EntityResolver resolver);
136:
137: public abstract void setErrorHandler(ErrorHandler handler);
138: }
|