1 28 29 package net.n3.nanoxml; 30 31 import java.io.Reader ; 32 import java.io.StringReader ; 33 import java.util.Hashtable ; 34 35 41 public class XMLEntityResolver implements IXMLEntityResolver 42 { 43 44 47 private Hashtable entities; 48 49 52 public XMLEntityResolver() 53 { 54 this.entities = new Hashtable (); 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 65 protected void finalize() throws Throwable 66 { 67 this.entities.clear(); 68 this.entities = null; 69 super.finalize(); 70 } 71 72 78 public void addInternalEntity(String name, String value) 79 { 80 if (!this.entities.containsKey(name)) 81 { 82 this.entities.put(name, value); 83 } 84 } 85 86 93 public void addExternalEntity(String name, String publicID, String systemID) 94 { 95 if (!this.entities.containsKey(name)) 96 { 97 this.entities.put(name, new String [] { publicID, systemID}); 98 } 99 } 100 101 109 public Reader getEntity(IXMLReader xmlReader, String name) throws XMLParseException 110 { 111 Object obj = this.entities.get(name); 112 113 if (obj == null) 114 { 115 return null; 116 } 117 else if (obj instanceof java.lang.String ) 118 { 119 return new StringReader ((String ) obj); 120 } 121 else 122 { 123 String [] id = (String []) obj; 124 return this.openExternalEntity(xmlReader, id[0], id[1]); 125 } 126 } 127 128 137 protected Reader openExternalEntity(IXMLReader xmlReader, String publicID, String systemID) 138 throws XMLParseException 139 { 140 try 141 { 142 return xmlReader.openStream(publicID, systemID); 143 } 144 catch (Exception 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 |