1 18 package sync4j.exchange.util; 19 20 import java.util.Hashtable ; 21 22 29 class Entities { 30 31 private static final String [][] BASIC_ARRAY = { 32 {"quot", "34"}, {"amp", "38"}, {"lt", "60"}, {"gt", "62"}, {"apos", "39"}, }; 38 39 42 public static final Entities XML; 43 44 static { 45 XML = new Entities(); 46 XML.addEntities(BASIC_ARRAY); 47 } 48 49 static interface EntityMap { 50 void add(String name, int value); 51 52 String name(int value); 53 54 int value(String name); 55 } 56 57 static class PrimitiveEntityMap implements EntityMap { 58 private Hashtable mapNameToValue = new Hashtable (); 59 private Hashtable mapValueToName = new Hashtable (); 60 61 public void add(String name, int value) { 62 mapNameToValue.put(name, new Integer (value)); 63 mapValueToName.put(new Integer (value), name); 64 } 65 66 public String name(int value) { 67 return (String ) mapValueToName.get(new Integer (value)); 68 } 69 70 public int value(String name) { 71 Object value = mapNameToValue.get(name); 72 if (value == null) { 73 return -1; 74 } 75 return ((Integer ) value).intValue(); 76 } 77 } 78 79 static class LookupEntityMap extends PrimitiveEntityMap { 80 81 private String [] lookupTable; 82 private int LOOKUP_TABLE_SIZE = 256; 83 84 public String name(int value) { 85 86 if (value < LOOKUP_TABLE_SIZE) { 87 return lookupTable()[value]; 88 } 89 90 return super.name(value); 91 } 92 93 private String [] lookupTable() { 94 if (lookupTable == null) { 95 createLookupTable(); 96 } 97 return lookupTable; 98 } 99 100 private void createLookupTable() { 101 lookupTable = new String [LOOKUP_TABLE_SIZE]; 102 for (int i = 0, l = LOOKUP_TABLE_SIZE; i < l; ++i) { 103 lookupTable[i] = super.name(i); 104 } 105 } 106 } 107 108 EntityMap map = new Entities.LookupEntityMap(); 109 110 public void addEntities(String [][] entityArray) { 111 for (int i = 0; i < entityArray.length; ++i) { 112 addEntity(entityArray[i][0], Integer.parseInt(entityArray[i][1])); 113 } 114 } 115 116 public void addEntity(String name, int value) { 117 map.add(name, value); 118 } 119 120 public String entityName(int value) { 121 return map.name(value); 122 } 123 124 125 public int entityValue(String name) { 126 return map.value(name); 127 } 128 129 136 public String escape(String str) { 137 138 char ch = ' ' ; 139 140 String entityName = null ; 141 StringBuffer buf = null ; 142 143 int intValue = 0 ; 144 145 buf = new StringBuffer (str.length() * 2); 146 147 for (int i = 0, l = str.length(); i < l; ++i) { 148 149 ch = str.charAt(i); 150 entityName = this.entityName(ch); 151 152 if (entityName == null) { 153 154 if (ch > 0x7F) { 155 156 intValue = ch; 157 buf.append("&#"); 158 buf.append(intValue); 159 buf.append(';'); 160 161 } else { 162 buf.append(ch); 163 } 164 } else { 165 166 buf.append('&'); 167 buf.append(entityName); 168 buf.append(';'); 169 170 } 171 } 172 173 return buf.toString(); 174 } 175 176 182 public String unescape(String str) { 183 184 StringBuffer buf = null ; 185 String entityName = null ; 186 187 char ch = ' ' ; 188 char charAt1 = ' ' ; 189 190 int entityValue = 0 ; 191 192 buf = new StringBuffer (str.length()); 193 194 for (int i = 0, l = str.length(); i < l; ++i) { 195 196 ch = str.charAt(i); 197 198 if (ch == '&') { 199 200 int semi = str.indexOf(';', i + 1); 201 202 if (semi == -1) { 203 buf.append(ch); 204 continue; 205 } 206 207 entityName = str.substring(i + 1, semi); 208 209 if (entityName.charAt(0) == '#') { 210 charAt1 = entityName.charAt(1); 211 if (charAt1 == 'x' || charAt1=='X') { 212 entityValue = Integer.valueOf(entityName.substring(2), 16).intValue(); 213 } else { 214 entityValue = Integer.parseInt(entityName.substring(1)); 215 } 216 } else { 217 entityValue = this.entityValue(entityName); 218 } if (entityValue == -1) { 219 buf.append('&'); 220 buf.append(entityName); 221 buf.append(';'); 222 } else { 223 buf.append((char) (entityValue)); 224 } 225 226 i = semi; 227 228 } else { 229 230 buf.append(ch); 231 232 } 233 } 234 235 return buf.toString(); 236 } 237 238 } | Popular Tags |