~ubuntu-branches/ubuntu/precise/arduino/precise

« back to all changes in this revision

Viewing changes to src/processing/xml/XMLEntityResolver.java

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-04-13 22:32:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100413223224-jduxnd0xxnkkda02
Tags: upstream-0018+dfsg
ImportĀ upstreamĀ versionĀ 0018+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* XMLEntityResolver.java                                          NanoXML/Java
 
2
 *
 
3
 * $Revision: 1.4 $
 
4
 * $Date: 2002/01/04 21:03:29 $
 
5
 * $Name: RELEASE_2_2_1 $
 
6
 *
 
7
 * This file is part of NanoXML 2 for Java.
 
8
 * Copyright (C) 2000-2002 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 processing.xml;
 
30
 
 
31
 
 
32
import java.util.Hashtable;
 
33
import java.io.Reader;
 
34
import java.io.StringReader;
 
35
 
 
36
 
 
37
/**
 
38
 * An XMLEntityResolver resolves entities.
 
39
 *
 
40
 * @author Marc De Scheemaecker
 
41
 * @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $
 
42
 */
 
43
public class XMLEntityResolver 
 
44
{
 
45
 
 
46
   /**
 
47
    * The entities.
 
48
    */
 
49
   private Hashtable<String, Object> entities;
 
50
 
 
51
 
 
52
   /**
 
53
    * Initializes the resolver.
 
54
    */
 
55
   public XMLEntityResolver()
 
56
   {
 
57
      this.entities = new Hashtable<String, Object>();
 
58
      this.entities.put("amp", "&#38;");
 
59
      this.entities.put("quot", "&#34;");
 
60
      this.entities.put("apos", "&#39;");
 
61
      this.entities.put("lt", "&#60;");
 
62
      this.entities.put("gt", "&#62;");
 
63
   }
 
64
 
 
65
 
 
66
   /**
 
67
    * Cleans up the object when it's destroyed.
 
68
    */
 
69
   protected void finalize()
 
70
      throws Throwable
 
71
   {
 
72
      this.entities.clear();
 
73
      this.entities = null;
 
74
      super.finalize();
 
75
   }
 
76
 
 
77
 
 
78
   /**
 
79
    * Adds an internal entity.
 
80
    *
 
81
    * @param name the name of the entity.
 
82
    * @param value the value of the entity.
 
83
    */
 
84
   public void addInternalEntity(String name,
 
85
                                 String value)
 
86
   {
 
87
      if (! this.entities.containsKey(name)) {
 
88
         this.entities.put(name, value);
 
89
      }
 
90
   }
 
91
 
 
92
 
 
93
   /**
 
94
    * Adds an external entity.
 
95
    *
 
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.
 
99
    */
 
100
   public void addExternalEntity(String name,
 
101
                                 String publicID,
 
102
                                 String systemID)
 
103
   {
 
104
      if (! this.entities.containsKey(name)) {
 
105
         this.entities.put(name, new String[] { publicID, systemID } );
 
106
      }
 
107
   }
 
108
 
 
109
 
 
110
   /**
 
111
    * Returns a Java reader containing the value of an entity.
 
112
    *
 
113
    * @param xmlReader the current XML reader
 
114
    * @param name the name of the entity.
 
115
    *
 
116
    * @return the reader, or null if the entity could not be resolved.
 
117
    */
 
118
   public Reader getEntity(StdXMLReader xmlReader,
 
119
                           String     name)
 
120
      throws XMLParseException
 
121
   {
 
122
      Object obj = this.entities.get(name);
 
123
 
 
124
      if (obj == null) {
 
125
         return null;
 
126
      } else if (obj instanceof java.lang.String) {
 
127
         return new StringReader((String)obj);
 
128
      } else {
 
129
         String[] id = (String[]) obj;
 
130
         return this.openExternalEntity(xmlReader, id[0], id[1]);
 
131
      }
 
132
   }
 
133
 
 
134
 
 
135
   /**
 
136
    * Returns true if an entity is external.
 
137
    *
 
138
    * @param name the name of the entity.
 
139
    */
 
140
   public boolean isExternalEntity(String name)
 
141
   {
 
142
      Object obj = this.entities.get(name);
 
143
      return ! (obj instanceof java.lang.String);
 
144
   }
 
145
 
 
146
 
 
147
   /**
 
148
    * Opens an external entity.
 
149
    *
 
150
    * @param xmlReader the current XML reader
 
151
    * @param publicID the public ID, which may be null
 
152
    * @param systemID the system ID
 
153
    *
 
154
    * @return the reader, or null if the reader could not be created/opened
 
155
    */
 
156
   protected Reader openExternalEntity(StdXMLReader xmlReader,
 
157
                                       String     publicID,
 
158
                                       String     systemID)
 
159
      throws XMLParseException
 
160
   {
 
161
      String parentSystemID = xmlReader.getSystemID();
 
162
 
 
163
      try {
 
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);
 
170
      }
 
171
   }
 
172
 
 
173
}