001: /* SAXEntityResolver.java NanoXML/SAX
002: *
003: * $Revision: 1.4 $
004: * $Date: 2002/01/04 21:03:28 $
005: * $Name: RELEASE_2_2_1 $
006: *
007: * This file is part of the SAX adapter for NanoXML 2 for Java.
008: * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
009: *
010: * This software is provided 'as-is', without any express or implied warranty.
011: * In no event will the authors be held liable for any damages arising from the
012: * use of this software.
013: *
014: * Permission is granted to anyone to use this software for any purpose,
015: * including commercial applications, and to alter it and redistribute it
016: * freely, subject to the following restrictions:
017: *
018: * 1. The origin of this software must not be misrepresented; you must not
019: * claim that you wrote the original software. If you use this software in
020: * a product, an acknowledgment in the product documentation would be
021: * appreciated but is not required.
022: *
023: * 2. Altered source versions must be plainly marked as such, and must not be
024: * misrepresented as being the original software.
025: *
026: * 3. This notice may not be removed or altered from any source distribution.
027: */
028:
029: package net.n3.nanoxml.sax;
030:
031: import java.io.InputStream;
032: import java.io.InputStreamReader;
033: import java.io.Reader;
034: import java.net.URL;
035: import net.n3.nanoxml.XMLEntityResolver;
036: import net.n3.nanoxml.IXMLReader;
037: import org.xml.sax.EntityResolver;
038: import org.xml.sax.InputSource;
039:
040: /**
041: * SAXEntityResolver is a subclass of XMLEntityResolver that supports the
042: * SAX EntityResolver listener.
043: *
044: * @see net.n3.nanoxml.IXMLEntityResolver
045: *
046: * @author Marc De Scheemaecker
047: * @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $
048: */
049: public class SAXEntityResolver extends XMLEntityResolver {
050:
051: /**
052: * The SAX EntityResolver listener.
053: */
054: private EntityResolver saxEntityResolver;
055:
056: /**
057: * Creates the resolver.
058: */
059: public SAXEntityResolver() {
060: this .saxEntityResolver = null;
061: }
062:
063: /**
064: * Cleans up the object when it's destroyed.
065: */
066: protected void finalize() throws Throwable {
067: this .saxEntityResolver = null;
068: super .finalize();
069: }
070:
071: /**
072: * Sets the SAX EntityResolver listener.
073: *
074: * @param resolver the entity resolver
075: */
076: public void setEntityResolver(EntityResolver resolver) {
077: this .saxEntityResolver = resolver;
078: }
079:
080: /**
081: * Opens an external entity.
082: *
083: * @param xmlReader the current XML reader
084: * @param publicID the public ID, which may be null
085: * @param systemID the system ID
086: *
087: * @return the reader, or null if the reader could not be created/opened
088: */
089: protected Reader openExternalEntity(IXMLReader xmlReader,
090: String publicID, String systemID) {
091: try {
092: URL url = new URL(xmlReader.getSystemID());
093: url = new URL(url, systemID);
094:
095: if (this .saxEntityResolver != null) {
096: InputSource source = this .saxEntityResolver
097: .resolveEntity(publicID, url.toString());
098:
099: if (source != null) {
100: Reader reader = source.getCharacterStream();
101:
102: if (reader != null) {
103: return reader;
104: }
105:
106: InputStream stream = source.getByteStream();
107:
108: if (stream == null) {
109: publicID = source.getPublicId();
110: systemID = source.getSystemId();
111: } else {
112: String encoding = source.getEncoding();
113:
114: if (encoding != null) {
115: return new InputStreamReader(stream,
116: encoding);
117: } else { // if encoding == null
118: return new InputStreamReader(stream);
119: }
120: }
121: }
122: }
123:
124: return super .openExternalEntity(xmlReader, publicID,
125: systemID);
126: } catch (Exception e) {
127: return null;
128: }
129: }
130:
131: }
|