1
/* XMLEntityResolver.java NanoXML/Java
4
* $Date: 2002/01/04 21:03:29 $
5
* $Name: RELEASE_2_2_1 $
7
* This file is part of NanoXML 2 for Java.
8
* Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
10
* This software is provided 'as-is', without any express or implied warranty.
11
* In no event will the authors be held liable for any damages arising from the
12
* use of this software.
14
* Permission is granted to anyone to use this software for any purpose,
15
* including commercial applications, and to alter it and redistribute it
16
* freely, subject to the following restrictions:
18
* 1. The origin of this software must not be misrepresented; you must not
19
* claim that you wrote the original software. If you use this software in
20
* a product, an acknowledgment in the product documentation would be
21
* appreciated but is not required.
23
* 2. Altered source versions must be plainly marked as such, and must not be
24
* misrepresented as being the original software.
26
* 3. This notice may not be removed or altered from any source distribution.
29
package processing.xml;
32
import java.util.Hashtable;
33
import java.io.Reader;
34
import java.io.StringReader;
38
* An XMLEntityResolver resolves entities.
40
* @author Marc De Scheemaecker
41
* @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $
43
public class XMLEntityResolver
49
private Hashtable<String, Object> entities;
53
* Initializes the resolver.
55
public XMLEntityResolver()
57
this.entities = new Hashtable<String, Object>();
58
this.entities.put("amp", "&");
59
this.entities.put("quot", """);
60
this.entities.put("apos", "'");
61
this.entities.put("lt", "<");
62
this.entities.put("gt", ">");
67
* Cleans up the object when it's destroyed.
69
protected void finalize()
72
this.entities.clear();
79
* Adds an internal entity.
81
* @param name the name of the entity.
82
* @param value the value of the entity.
84
public void addInternalEntity(String name,
87
if (! this.entities.containsKey(name)) {
88
this.entities.put(name, value);
94
* Adds an external entity.
96
* @param name the name of the entity.
97
* @param publicID the public ID of the entity, which may be null.
98
* @param systemID the system ID of the entity.
100
public void addExternalEntity(String name,
104
if (! this.entities.containsKey(name)) {
105
this.entities.put(name, new String[] { publicID, systemID } );
111
* Returns a Java reader containing the value of an entity.
113
* @param xmlReader the current XML reader
114
* @param name the name of the entity.
116
* @return the reader, or null if the entity could not be resolved.
118
public Reader getEntity(StdXMLReader xmlReader,
120
throws XMLParseException
122
Object obj = this.entities.get(name);
126
} else if (obj instanceof java.lang.String) {
127
return new StringReader((String)obj);
129
String[] id = (String[]) obj;
130
return this.openExternalEntity(xmlReader, id[0], id[1]);
136
* Returns true if an entity is external.
138
* @param name the name of the entity.
140
public boolean isExternalEntity(String name)
142
Object obj = this.entities.get(name);
143
return ! (obj instanceof java.lang.String);
148
* Opens an external entity.
150
* @param xmlReader the current XML reader
151
* @param publicID the public ID, which may be null
152
* @param systemID the system ID
154
* @return the reader, or null if the reader could not be created/opened
156
protected Reader openExternalEntity(StdXMLReader xmlReader,
159
throws XMLParseException
161
String parentSystemID = xmlReader.getSystemID();
164
return xmlReader.openStream(publicID, systemID);
165
} catch (Exception e) {
166
throw new XMLParseException(parentSystemID,
167
xmlReader.getLineNr(),
168
"Could not open external entity "
169
+ "at system ID: " + systemID);