Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / net / n3 / nanoxml / XMLEntityResolver.java @ 15280

History | View | Annotate | Download (4.73 KB)

1
/* XMLEntityResolver.java                                          NanoXML/Java
2
 *
3
 * $Revision: 1.1 $
4
 * $Date: 2006/06/14 07:29:07 $
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

    
32
import java.io.Reader;
33
import java.io.StringReader;
34
import java.util.Hashtable;
35

    
36

    
37
/**
38
 * An XMLEntityResolver resolves entities.
39
 *
40
 * @author Marc De Scheemaecker
41
 * @version $Name:  $, $Revision: 1.1 $
42
 */
43
public class XMLEntityResolver
44
    implements IXMLEntityResolver
45
{
46
    
47
    /**
48
     * The entities.
49
     */
50
    private Hashtable entities;
51
    
52

    
53
    /**
54
     * Initializes the resolver.
55
     */
56
    public XMLEntityResolver()
57
    {
58
        this.entities = new Hashtable();
59
        this.entities.put("amp", "&");
60
        this.entities.put("quot", """);
61
        this.entities.put("apos", "'");
62
        this.entities.put("lt", "<");
63
        this.entities.put("gt", ">");
64
    }
65
    
66
    
67
    /**
68
     * Cleans up the object when it's destroyed.
69
     */
70
    protected void finalize()
71
        throws Throwable
72
    {
73
        this.entities.clear();
74
        this.entities = null;
75
        super.finalize();
76
    }
77
    
78
    
79
    /**
80
     * Adds an internal entity.
81
     *
82
     * @param name the name of the entity.
83
     * @param value the value of the entity.
84
     */
85
    public void addInternalEntity(String name,
86
                                  String value)
87
    {
88
        if (! this.entities.containsKey(name)) {
89
            this.entities.put(name, value);
90
        }
91
    }
92
    
93
    
94
    /**
95
     * Adds an external entity.
96
     *
97
     * @param name the name of the entity.
98
     * @param publicID the public ID of the entity, which may be null.
99
     * @param systemID the system ID of the entity.
100
     */
101
    public void addExternalEntity(String name,
102
                                  String publicID,
103
                                  String systemID)
104
    {
105
        if (! this.entities.containsKey(name)) {
106
            this.entities.put(name, new String[] { publicID, systemID } );
107
        }
108
    }
109
    
110
    
111
    /**
112
     * Returns a Java reader containing the value of an entity.
113
     *
114
     * @param xmlReader the current XML reader
115
     * @param name the name of the entity.
116
     *
117
     * @return the reader, or null if the entity could not be resolved.
118
     */
119
    public Reader getEntity(IXMLReader xmlReader,
120
                            String     name)
121
        throws XMLParseException
122
    {
123
        Object obj = this.entities.get(name);
124
        
125
        if (obj == null) {
126
            return null;
127
        } else if (obj instanceof java.lang.String) {
128
            return new StringReader((String)obj);
129
        } else {
130
            String[] id = (String[]) obj;
131
            return this.openExternalEntity(xmlReader, id[0], id[1]);
132
        }
133
    }
134
    
135
    
136
    /**
137
     * Opens an external entity.
138
     *
139
     * @param xmlReader the current XML reader
140
     * @param publicID the public ID, which may be null
141
     * @param systemID the system ID
142
     *
143
     * @return the reader, or null if the reader could not be created/opened
144
     */
145
    protected Reader openExternalEntity(IXMLReader xmlReader,
146
                                        String     publicID,
147
                                        String     systemID)
148
        throws XMLParseException
149
    {
150
        try {
151
            return xmlReader.openStream(publicID, systemID);
152
        } catch (Exception e) {
153
            throw new XMLParseException(xmlReader.getSystemID(),
154
                                        xmlReader.getLineNr(),
155
                                        "Could not open external entity "
156
                                        + "at system ID: " + systemID);
157
        }
158
    }
159
    
160
}