1 21 package org.lobobrowser.util; 22 23 import java.io.*; 24 import java.security.*; 25 import java.util.ArrayList ; 26 27 30 public class Strings 31 { 32 private static final MessageDigest MESSAGE_DIGEST; 33 public static final String [] EMPTY_ARRAY = new String [0]; 34 35 static { 36 MessageDigest md; 37 try { 38 md = MessageDigest.getInstance("MD5"); 39 } catch(NoSuchAlgorithmException err) { 40 throw new IllegalStateException (); 41 } 42 MESSAGE_DIGEST = md; 43 } 44 45 private Strings() {} 46 47 public static int compareVersions(String version1, String version2) { 48 return version1.compareTo(version2); 49 } 50 51 public static boolean isBlank(String text) { 52 return text == null || "".equals(text); 53 } 54 55 public static int countLines(String text) 56 { 57 int startIdx = 0; 58 int lineCount = 1; 59 for(;;) 60 { 61 int lbIdx = text.indexOf('\n', startIdx); 62 if(lbIdx == -1) 63 { 64 break; 65 } 66 lineCount++; 67 startIdx = lbIdx + 1; 68 } 69 return lineCount; 70 } 71 72 public static boolean isJavaIdentifier(String id) 73 { 74 if(id == null) 75 { 76 return false; 77 } 78 int len = id.length(); 79 if(len == 0) 80 { 81 return false; 82 } 83 if(!Character.isJavaIdentifierStart(id.charAt(0))) 84 { 85 return false; 86 } 87 for(int i = 1; i < len; i++) 88 { 89 if(!Character.isJavaIdentifierPart(id.charAt(i))) 90 { 91 return false; 92 } 93 } 94 return true; 95 } 96 97 public static String getTextFromStream(InputStream in) throws IOException { 98 Reader reader = new InputStreamReader(in, "UTF-8"); 99 char[] buffer = new char[256]; 100 int numRead; 101 int offset = 0; 102 while((numRead = reader.read(buffer, offset, buffer.length - offset)) != -1) { 103 offset += numRead; 104 if(offset >= (buffer.length * 2) / 3) { 105 char[] newBuffer = new char[buffer.length * 2]; 106 System.arraycopy(buffer, 0, newBuffer, 0, offset); 107 buffer = newBuffer; 108 } 109 } 110 return new String (buffer, 0, offset); 111 } 112 113 public static String getJavaStringLiteral(String text) { 114 StringBuffer buf = new StringBuffer (); 115 buf.append('"'); 116 int len = text.length(); 117 for(int i = 0; i < len; i++) { 118 char ch = text.charAt(i); 119 switch(ch) { 120 case '\\': 121 buf.append("\\\\"); 122 break; 123 case '\n': 124 buf.append("\\n"); 125 break; 126 case '\r': 127 buf.append("\\r"); 128 break; 129 case '\t': 130 buf.append("\\t"); 131 break; 132 case '"': 133 buf.append("\\\""); 134 break; 135 default: 136 buf.append(ch); 137 break; 138 } 139 } 140 buf.append('"'); 141 return buf.toString(); 142 } 143 144 public static String getJavaIdentifier(String candidateID) { 145 int len = candidateID.length(); 146 StringBuffer buf = new StringBuffer (); 147 for(int i = 0; i < len; i++) 148 { 149 char ch = candidateID.charAt(i); 150 boolean good = i == 0 ? Character.isJavaIdentifierStart(ch) : Character.isJavaIdentifierPart(ch); 151 if(good) 152 { 153 buf.append(ch); 154 } 155 else { 156 buf.append('_'); 157 } 158 } 159 return buf.toString(); 160 } 161 162 private static final String HEX_CHARS = "0123456789ABCDEF"; 163 164 public static String getMD5(String source) throws UnsupportedEncodingException { 165 byte[] bytes = source.getBytes("UTF8"); 166 byte[] result; 167 synchronized(MESSAGE_DIGEST) { 168 MESSAGE_DIGEST.update(bytes); 169 result = MESSAGE_DIGEST.digest(); 170 } 171 char[] resChars = new char[32]; 172 int len = result.length; 173 for(int i = 0; i < len; i++) { 174 byte b = result[i]; 175 int lo4 = b & 0x0F; 176 int hi4 = (b & 0xF0) >> 4; 177 resChars[i*2] = HEX_CHARS.charAt(hi4); 178 resChars[i*2 + 1] = HEX_CHARS.charAt(lo4); 179 } 180 return new String (resChars); 181 } 182 183 public static String getHash32(String source) throws UnsupportedEncodingException { 184 String md5 = getMD5(source); 185 return md5.substring(0, 8); 186 } 187 188 public static String getHash64(String source) throws UnsupportedEncodingException { 189 String md5 = getMD5(source); 190 return md5.substring(0, 16); 191 } 192 193 public static int countChars(String text, char ch) { 194 int len = text.length(); 195 int count = 0; 196 for(int i = 0; i < len; i++) { 197 if(ch == text.charAt(i)) { 198 count++; 199 } 200 } 201 return count; 202 } 203 204 239 public static String unquote(String text) { 240 if(text.startsWith("\"") && text.endsWith("\"")) { 241 return text.substring(1, text.length() - 2); 242 } 243 return text; 244 } 245 246 public static String [] split(String phrase) { 247 int length = phrase.length(); 248 ArrayList wordList = new ArrayList (); 249 StringBuffer word = null; 250 for(int i = 0; i < length; i++) { 251 char ch = phrase.charAt(i); 252 switch(ch) { 253 case ' ': 254 case '\t': 255 case '\r': 256 case '\n': 257 if(word != null) { 258 wordList.add(word.toString()); 259 word = null; 260 } 261 break; 262 default: 263 if(word == null) { 264 word = new StringBuffer (); 265 } 266 word.append(ch); 267 } 268 } 269 if(word != null) { 270 wordList.add(word.toString()); 271 } 272 return (String []) wordList.toArray(EMPTY_ARRAY); 273 } 274 275 public static String truncate(String text, int maxLength) { 276 if(text == null) { 277 return null; 278 } 279 if(text.length() <= maxLength) { 280 return text; 281 } 282 return text.substring(0, Math.max(maxLength - 3, 0)) + "..."; 283 } 284 285 public static String strictHtmlEncode(String rawText) { 286 StringBuffer output = new StringBuffer (); 287 int length = rawText.length(); 288 for(int i = 0; i < length; i++) { 289 char ch = rawText.charAt(i); 290 switch(ch) { 291 case '&': 292 output.append("&"); 293 break; 294 case '"': 295 output.append("""); 296 break; 297 case '<': 298 output.append("<"); 299 break; 300 case '>': 301 output.append(">"); 302 break; 303 default: 304 output.append(ch); 305 } 306 } 307 return output.toString(); 308 } 309 310 public static String trimForAlphaNumDash(String rawText) { 311 int length = rawText.length(); 312 for(int i = 0; i < length; i++) { 313 char ch = rawText.charAt(i); 314 if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '-') { 315 continue; 316 } 317 return rawText.substring(0, i); 318 } 319 return rawText; 320 } 321 } 322 | Popular Tags |