1 8 9 package net.sourceforge.chaperon.common; 10 11 17 public class Decoder 18 { 19 public static final String STRING = "\""; 20 public static final String CHAR = "\'"; 21 public static final String CLASS = "[^- ]"; 22 public static final String REGEX = "+*?^$() "; 23 24 31 public static String toString(String string) 32 { 33 StringBuffer buffer = new StringBuffer (); 34 35 buffer.append("\""); 36 for (int i = 0; i<string.length(); i++) 37 buffer.append(decode(string.charAt(i), STRING)); 38 39 buffer.append("\""); 40 return buffer.toString(); 41 } 42 43 public static String toChar(char character) 44 { 45 return "'"+decode(character, CHAR)+"'"; 46 } 47 48 public static String toClass(char minimum, char maximum) 49 { 50 if (minimum==maximum) 51 return toChar(minimum); 52 else 53 { 54 StringBuffer buffer = new StringBuffer (); 55 buffer.append("["); 56 buffer.append(decode(minimum, CLASS)); 57 buffer.append("-"); 58 buffer.append(decode(maximum, CLASS)); 59 buffer.append("]"); 60 return buffer.toString(); 61 } 62 } 63 64 public static String toNegativeClass(char minimum, char maximum) 65 { 66 if (minimum==maximum) 67 { 68 StringBuffer buffer = new StringBuffer (); 69 buffer.append("[^"); 70 buffer.append(decode(minimum, CLASS)); 71 buffer.append("]"); 72 return buffer.toString(); 73 } 74 else 75 { 76 StringBuffer buffer = new StringBuffer (); 77 buffer.append("[^"); 78 buffer.append(decode(minimum, CLASS)); 79 buffer.append("-"); 80 buffer.append(decode(maximum, CLASS)); 81 buffer.append("]"); 82 return buffer.toString(); 83 } 84 } 85 86 public static String decode(char character, String masked) 87 { 88 if (character=='\n') 89 return "\\n"; 90 else if (character=='\t') 91 return "\\t"; 92 else if (character=='\r') 93 return "\\r"; 94 else if (character=='\\') 95 return "\\\\"; 96 else if ((character>=' ') && (character<='~') && (masked.indexOf(character)<0)) 97 return String.valueOf(character); 98 else if ((character>=' ') && (character<='~')) 99 return "\\"+character; 100 101 String hexstring = Integer.toHexString(character); 102 String zeros = "0000"; 103 104 return "\\u"+zeros.substring(hexstring.length())+hexstring; 105 } 106 107 public static String decode(String string, String masked) 108 { 109 StringBuffer buffer = new StringBuffer (); 110 111 for (int i = 0; i<string.length(); i++) 112 buffer.append(decode(string.charAt(i), masked)); 113 114 return buffer.toString(); 115 } 116 } 117 | Popular Tags |