1 11 package org.eclipse.pde.internal.ui.nls; 12 13 public class StringHelper { 14 15 protected static String preparePropertiesString(String s, char[] newLine) { 16 if (s == null) 17 return null; 18 int length = s.length(); 19 int nlLength = newLine.length; 20 StringBuffer sb = new StringBuffer (length + nlLength); 21 for (int i = 0; i < length; i++) { 22 char c = s.charAt(i); 23 if (i + nlLength < length) { 24 boolean notNewLine = false; 25 for (int j = 0; j < nlLength; j++) 26 if (s.charAt(i + j) != newLine[j]) 27 notNewLine = true; 28 if (!notNewLine) 29 sb.append("\\"); } 31 sb.append(c); 32 } 33 return sb.toString(); 34 } 35 36 protected static String unwindEscapeChars(String s) { 37 if (s != null) { 38 int length = s.length(); 39 StringBuffer sb = new StringBuffer (length); 40 for (int i = 0; i < length; i++) { 41 char c = s.charAt(i); 42 sb.append(getUnwoundString(c)); 43 } 44 return sb.toString(); 45 } 46 return null; 47 } 48 49 protected static String getUnwoundString(char c) { 50 switch (c) { 51 case '\b' : 52 return "\\b"; case '\t' : 54 return "\\t"; case '\n' : 56 return "\\n"; case '\f' : 58 return "\\f"; case '\r' : 60 return "\\r"; case '\\' : 62 return "\\\\"; } 64 return String.valueOf(c); 65 } 66 67 protected static String windEscapeChars(String s) { 68 if (s == null) 69 return null; 70 71 char aChar; 72 int len= s.length(); 73 StringBuffer outBuffer= new StringBuffer (len); 74 75 for (int x= 0; x < len;) { 76 aChar= s.charAt(x++); 77 if (aChar == '\\') { 78 aChar= s.charAt(x++); 79 if (aChar == 'u') { 80 int value= 0; 82 for (int i= 0; i < 4; i++) { 83 aChar= s.charAt(x++); 84 switch (aChar) { 85 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': 86 value= (value << 4) + aChar - '0'; 87 break; 88 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 89 value= (value << 4) + 10 + aChar - 'a'; 90 break; 91 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': 92 value= (value << 4) + 10 + aChar - 'A'; 93 break; 94 default: 95 throw new IllegalArgumentException ("Malformed \\uxxxx encoding."); } 97 } 98 outBuffer.append((char) value); 99 } else { 100 if (aChar == 't') { 101 outBuffer.append('\t'); 102 } else { 103 if (aChar == 'r') { 104 outBuffer.append('\r'); 105 } else { 106 if (aChar == 'n') { 107 outBuffer.append('\n'); 108 } else { 109 if (aChar == 'f') { 110 outBuffer.append('\f'); 111 } else { 112 outBuffer.append(aChar); 113 } 114 } 115 } 116 } 117 } 118 } else 119 outBuffer.append(aChar); 120 } 121 return outBuffer.toString(); 122 } 123 124 protected static boolean isValidLocalization(String name) { 125 if (name.length() <= 0) { 126 return false; 127 } 128 for (int i = 0; i < name.length(); i++) { 129 char c = name.charAt(i); 130 if ((c < 'A' || 'Z' < c) && (c < 'a' || 'z' < c) 131 && (c < '0' || '9' < c) && c != '_' && c != '-' && c != '/') { 132 return false; 133 } 134 } 135 return true; 136 } 137 } 138 | Popular Tags |