001: /* XMLEntityResolver.java NanoXML/Java
002: *
003: * $Revision: 2056 $
004: * $Date: 2008-02-25 00:29:28 -0800 (Mon, 25 Feb 2008) $
005: * $Name$
006: *
007: * This file is part of NanoXML 2 for Java.
008: * Copyright (C) 2001 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;
030:
031: import java.io.Reader;
032: import java.io.StringReader;
033: import java.util.Hashtable;
034:
035: /**
036: * An XMLEntityResolver resolves entities.
037: *
038: * @author Marc De Scheemaecker
039: * @version $Name$, $Revision: 2056 $
040: */
041: public class XMLEntityResolver implements IXMLEntityResolver {
042:
043: /**
044: * The entities.
045: */
046: private Hashtable<String, Object> entities;
047:
048: /**
049: * Initializes the resolver.
050: */
051: public XMLEntityResolver() {
052: this .entities = new Hashtable<String, Object>();
053: this .entities.put("amp", "&#38;");
054: this .entities.put("quot", "&#34;");
055: this .entities.put("apos", "&#39;");
056: this .entities.put("lt", "&#60;");
057: this .entities.put("gt", "&#62;");
058: }
059:
060: /**
061: * Cleans up the object when it's destroyed.
062: */
063: protected void finalize() throws Throwable {
064: this .entities.clear();
065: this .entities = null;
066: super .finalize();
067: }
068:
069: /**
070: * Adds an internal entity.
071: *
072: * @param name the name of the entity.
073: * @param value the value of the entity.
074: */
075: public void addInternalEntity(String name, String value) {
076: if (!this .entities.containsKey(name)) {
077: this .entities.put(name, value);
078: }
079: }
080:
081: /**
082: * Adds an external entity.
083: *
084: * @param name the name of the entity.
085: * @param publicID the public ID of the entity, which may be null.
086: * @param systemID the system ID of the entity.
087: */
088: public void addExternalEntity(String name, String publicID,
089: String systemID) {
090: if (!this .entities.containsKey(name)) {
091: this .entities
092: .put(name, new String[] { publicID, systemID });
093: }
094: }
095:
096: /**
097: * Returns a Java reader containing the value of an entity.
098: *
099: * @param xmlReader the current XML reader
100: * @param name the name of the entity.
101: *
102: * @return the reader, or null if the entity could not be resolved.
103: */
104: public Reader getEntity(IXMLReader xmlReader, String name)
105: throws XMLParseException {
106: Object obj = this .entities.get(name);
107:
108: if (obj == null) {
109: return null;
110: } else if (obj instanceof java.lang.String) {
111: return new StringReader((String) obj);
112: } else {
113: String[] id = (String[]) obj;
114: return this .openExternalEntity(xmlReader, id[0], id[1]);
115: }
116: }
117:
118: /**
119: * Opens an external entity.
120: *
121: * @param xmlReader the current XML reader
122: * @param publicID the public ID, which may be null
123: * @param systemID the system ID
124: *
125: * @return the reader, or null if the reader could not be created/opened
126: */
127: protected Reader openExternalEntity(IXMLReader xmlReader,
128: String publicID, String systemID) throws XMLParseException {
129: try {
130: return xmlReader.openStream(publicID, systemID);
131: } catch (Exception e) {
132: throw new XMLParseException(xmlReader.getSystemID(),
133: xmlReader.getLineNr(),
134: "Could not open external entity "
135: + "at system ID: " + systemID);
136: }
137: }
138:
139: }
|