KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > n3 > nanoxml > XMLEntityResolver


1 /* XMLEntityResolver.java NanoXML/Java
2  *
3  * $Revision: 1421 $
4  * $Date: 2006-03-12 17:32:32 +0100 (Sun, 12 Mar 2006) $
5  * $Name$
6  *
7  * This file is part of NanoXML 2 for Java.
8  * Copyright (C) 2001 Marc De Scheemaecker, All Rights Reserved.
9  *
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.
13  *
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:
17  *
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.
22  *
23  * 2. Altered source versions must be plainly marked as such, and must not be
24  * misrepresented as being the original software.
25  *
26  * 3. This notice may not be removed or altered from any source distribution.
27  */

28
29 package net.n3.nanoxml;
30
31 import java.io.Reader JavaDoc;
32 import java.io.StringReader JavaDoc;
33 import java.util.Hashtable JavaDoc;
34
35 /**
36  * An XMLEntityResolver resolves entities.
37  *
38  * @author Marc De Scheemaecker
39  * @version $Name$, $Revision: 1421 $
40  */

41 public class XMLEntityResolver implements IXMLEntityResolver
42 {
43
44     /**
45      * The entities.
46      */

47     private Hashtable JavaDoc entities;
48
49     /**
50      * Initializes the resolver.
51      */

52     public XMLEntityResolver()
53     {
54         this.entities = new Hashtable JavaDoc();
55         this.entities.put("amp", "&");
56         this.entities.put("quot", """);
57         this.entities.put("apos", "'");
58         this.entities.put("lt", "<");
59         this.entities.put("gt", ">");
60     }
61
62     /**
63      * Cleans up the object when it's destroyed.
64      */

65     protected void finalize() throws Throwable JavaDoc
66     {
67         this.entities.clear();
68         this.entities = null;
69         super.finalize();
70     }
71
72     /**
73      * Adds an internal entity.
74      *
75      * @param name the name of the entity.
76      * @param value the value of the entity.
77      */

78     public void addInternalEntity(String JavaDoc name, String JavaDoc value)
79     {
80         if (!this.entities.containsKey(name))
81         {
82             this.entities.put(name, value);
83         }
84     }
85
86     /**
87      * Adds an external entity.
88      *
89      * @param name the name of the entity.
90      * @param publicID the public ID of the entity, which may be null.
91      * @param systemID the system ID of the entity.
92      */

93     public void addExternalEntity(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
94     {
95         if (!this.entities.containsKey(name))
96         {
97             this.entities.put(name, new String JavaDoc[] { publicID, systemID});
98         }
99     }
100
101     /**
102      * Returns a Java reader containing the value of an entity.
103      *
104      * @param xmlReader the current XML reader
105      * @param name the name of the entity.
106      *
107      * @return the reader, or null if the entity could not be resolved.
108      */

109     public Reader JavaDoc getEntity(IXMLReader xmlReader, String JavaDoc name) throws XMLParseException
110     {
111         Object JavaDoc obj = this.entities.get(name);
112
113         if (obj == null)
114         {
115             return null;
116         }
117         else if (obj instanceof java.lang.String JavaDoc)
118         {
119             return new StringReader JavaDoc((String JavaDoc) obj);
120         }
121         else
122         {
123             String JavaDoc[] id = (String JavaDoc[]) obj;
124             return this.openExternalEntity(xmlReader, id[0], id[1]);
125         }
126     }
127
128     /**
129      * Opens an external entity.
130      *
131      * @param xmlReader the current XML reader
132      * @param publicID the public ID, which may be null
133      * @param systemID the system ID
134      *
135      * @return the reader, or null if the reader could not be created/opened
136      */

137     protected Reader JavaDoc openExternalEntity(IXMLReader xmlReader, String JavaDoc publicID, String JavaDoc systemID)
138             throws XMLParseException
139     {
140         try
141         {
142             return xmlReader.openStream(publicID, systemID);
143         }
144         catch (Exception JavaDoc e)
145         {
146             throw new XMLParseException(xmlReader.getSystemID(), xmlReader.getLineNr(),
147                     "Could not open external entity " + "at system ID: " + systemID);
148         }
149     }
150
151 }
152
Popular Tags