1 9 10 11 23 package JSX; 24 25 public class ParseUtilities { 26 public static boolean parseBoolean(String in) { 27 String s = in.trim(); 28 if (s.equalsIgnoreCase("false")) return false; 29 else if (s.equalsIgnoreCase("true")) return true; 30 else 31 throw new IllegalArgumentException ("Boolean must be 'true' or 'false'; not '" 32 +s+"')"); 33 } 35 public static float parseFloat(String in) { 36 String s = in.trim(); 37 if( s.equals("NaN") ) return Float.NaN; 38 else if( s.equals("Infinity") ) return Float.POSITIVE_INFINITY; 39 else if( s.equals("-Infinity") ) return Float.NEGATIVE_INFINITY; 40 else return Float.parseFloat(s); 41 } 42 43 public static double parseDouble(String s) { 44 if( s.equals("NaN") ) return Double.NaN; 45 else if( s.equals("Infinity") ) return Double.POSITIVE_INFINITY; 46 else if( s.equals("-Infinity") ) return Double.NEGATIVE_INFINITY; 47 else return Double.parseDouble(s); 48 } 49 50 65 public static String encodeXML(String in) { 66 StringBuffer out = new StringBuffer (in.length()); for (int i=0; i<in.length(); i++) { 70 char c = in.charAt(i); 71 switch (c) { 72 case '&': out.append("&"); break; 73 case '<': out.append("<"); break; 74 case '>': out.append(">"); break; 75 case '\'': out.append("'"); break; 76 case '"': out.append("""); break; 77 case '\\': 79 if (i+1==in.length()) { out.append("\\\\"); i++; } 88 else { 89 c = in.charAt(i+1); 90 if (c=='\\') { 91 out.append("\\\\\\\\"); i++; } else if (c=='u') { 94 out.append("\\\\u"); i++; } else if (c<=0x1F || (c>=0x80 && c<=0x9f)) { 97 out.append("\\\\"); } else { 99 out.append("\\"); } 101 } 102 break; case '\t': case '\n': case '\r': out.append(c); break; 104 default: if (c<=0x1F || (c>=0x80)) { out.append( "\\u" + Integer.toHexString(c+0x10000).substring(1) ); 109 } else { 111 out.append(c); 112 } 113 } 114 } 115 return out.toString(); } 117 118 119 123 public static String decodeXML_JSX2(String in) { 124 StringBuffer out = new StringBuffer (in.length()); int i = 0; 127 try { 128 for (i=0; i<in.length(); i++) { 129 char c = in.charAt(i); 130 142 if (c=='\\') { i++; 144 if (in.charAt(i)=='u') { i++; 146 out.append((char)Integer.parseInt(in.substring(i,i+4),16)); 147 i+=3; 148 } 149 else if (in.charAt(i)=='\\') { 150 out.append('\\'); i+=0; } else { out.append('\\'); i-=1; } 155 } 156 else { 157 out.append(c); 158 } 159 } 160 } catch (StringIndexOutOfBoundsException e) { 161 System.err.println( "Seems to be an incomplete escaped entity or \\unnnn: " 162 +in.substring(i) ); 163 } catch (IndexOutOfBoundsException e) { 164 System.err.println( "Seems to be an incomplete escaped entity: " 165 +in.substring(in.lastIndexOf('&')) ); 166 } catch (NumberFormatException e) { 167 System.err.println( "Seems to be a faulty \\unnnn escaped control char: " 168 +in.substring(i) ); 169 } 170 return out.toString(); 171 } 172 173 181 182 public static String decodeXML(String in) { 183 StringBuffer out = new StringBuffer (in.length()); int i = 0; 186 try { 187 for (i=0; i<in.length(); i++) { 188 char c = in.charAt(i); 189 if (c=='&') { i++; 191 if (in.startsWith("amp;", i)) {out.append('&'); i+=3;} else if (in.startsWith("lt;", i)) {out.append('<'); i+=2;} 193 else if (in.startsWith("gt;", i)) {out.append('>'); i+=2;} 194 else if (in.startsWith("apos;", i)) {out.append('\''); i+=4;} 195 else if (in.startsWith("quot;", i)) {out.append('"'); i+=4;} 196 else throw new IllegalArgumentException ("malformed XML: "+in.substring(i-1)); 197 } 198 else if (c=='\\') { i++; 200 if (in.charAt(i)=='u') { i++; 202 out.append((char)Integer.parseInt(in.substring(i,i+4),16)); 203 i+=3; 204 } 205 else if (in.charAt(i)=='\\') { 206 out.append('\\'); i+=0; } else { out.append('\\'); i-=1; } 211 } 212 else { 213 out.append(c); 214 } 215 } 216 } catch (StringIndexOutOfBoundsException e) { 217 System.err.println( "Seems to be an incomplete escaped entity or \\unnnn: " 218 +in.substring(i) ); 219 } catch (IndexOutOfBoundsException e) { 220 System.err.println( "Seems to be an incomplete escaped entity: " 221 +in.substring(in.lastIndexOf('&')) ); 222 } catch (NumberFormatException e) { 223 System.err.println( "Seems to be a faulty \\unnnn escaped control char: " 224 +in.substring(i) ); 225 } 226 return out.toString(); 227 } 228 229 static class Dollar { 230 public static void main(String a[]) { 231 if (a.length==0) 232 a = new String [] {"$a$a$"}; 233 for (int i=0; i<a.length; i++) { 234 String t; 235 System.err.println(a[i] + " -> " 236 + (t=escapeDollar(a[i])) + " -> " 237 + descapeDollar(t) ); 238 } 239 } 240 } 241 242 243 258 static final String DOLLAR = "_-"; 265 public static String escapeDollar(String in) { 269 StringBuffer out = new StringBuffer (in.length()); 270 for (int i=0; i<in.length(); i++) { char c; 272 if ((c=in.charAt(i))=='$') 273 out.append(DOLLAR); 274 else 275 out.append(c); 276 } 277 return out.toString(); 278 } 279 280 288 public static String descapeDollar(String in) { 289 StringBuffer out = new StringBuffer (in.length()); 290 int marker = 0; 291 while (true) { 292 int newmarker = in.indexOf(DOLLAR, marker); 293 if (newmarker==-1) 294 break; out.append(in.substring(marker, newmarker)+'$'); 296 marker = newmarker+DOLLAR.length(); 297 } 298 out.append(in.substring(marker)); return out.toString(); 300 } 301 302 303 304 307 static class Hex { 308 public static void main(String a[]) { 309 if (a.length>0) { 310 byte[] b = decodeHex(a[0]); 311 System.err.print( print(b) ); 312 System.err.print( encodeHex(b) ); 313 } 314 else { 315 byte[] b = new byte[0x200]; 316 byte j=0; 317 for (int i=0; i<b.length; i++) b[i] = j++; String t; 319 System.err.print(print(b) + " -> "); 320 System.err.print((t=encodeHex(b)) + " -> "); 321 System.err.print( print(decodeHex(t)) ); 322 System.err.println(); 323 } 324 } 325 } 326 public static String print(byte[] in) { 327 String out = ""; 328 for (int i=0; i<in.length; i++) { 329 out += in[i]+", "; } 331 return out; 332 } 333 334 344 public static String encodeHex(byte[] in) { 345 return encodeHex(in, true); 346 } 347 357 public static String encodeHex(byte[] in, boolean formatted) { 358 StringBuffer out = new StringBuffer (in.length*3); if (formatted) 360 out.append( EOL_SEP ); 361 for (int i=0; i<in.length;) { out.append( toHexChar((in[i]&0xF0)>>>4) ); out.append( toHexChar((in[i]&0x0F)>>>0) ); i++; 365 if (formatted) 366 { 367 if (i%256==0) 368 out.append( EOL_SEP+EOL_SEP ); 369 else if (i%16==0) 370 out.append( EOL_SEP ); 371 else if (i%4==0) 372 out.append( " " ); 373 else 374 out.append( ' ' ); 375 } 376 } 380 if (formatted) 381 out.append( EOL_SEP ); return out.toString(); 383 } 384 static final String EOL_SEP = System.getProperty("line.separator"); 385 387 static char toHexChar(int b) { 388 if (b>=10) 389 return (char)(b-10+'A'); 390 else 391 return (char)(b+'0'); 392 } 393 394 395 404 public static byte[] decodeHex(String in) { 405 byte[] out = new byte[in.length()/2]; 406 int j = 0; for (int i=0; i<in.length();) { 408 char a = in.charAt(i++); 409 if (Character.isWhitespace(a)) 410 continue; 411 out[j++] = (byte) 412 ( ((fromHexChar(a) &0x0F)<<4) | 413 (( fromHexChar(in.charAt(i++)) &0x0F)<<0) ); } 416 if ( j<(in.length()/2) ) { 417 byte[] trimOut = new byte[j]; 418 System.arraycopy(out, 0, trimOut, 0, j); 419 out = trimOut; 420 } 421 return out; 422 } 423 424 425 429 static int fromHexChar(char a) { 430 if (Character.isUpperCase(a)) 431 return (char)(a-'A'+10); 432 else if (Character.isLowerCase(a)) 433 return (char)(a-'a'+10); 434 else if (Character.isDigit(a)) 435 return (char)(a-'0'); 436 else throw new NumberFormatException ("'"+a+"' is not a valid hex character"); 437 } 438 439 } 440
| Popular Tags
|