1 11 package org.eclipse.osgi.util; 12 13 import java.util.Locale ; 14 15 30 public class TextProcessor { 31 32 36 private static final String DOT = "."; 38 41 private static final String COLON = ":"; 43 46 private static final String FILE_SEP_FSLASH = "/"; 48 51 private static final String FILE_SEP_BSLASH = "\\"; 53 56 private static final String delimiterString = DOT + COLON + FILE_SEP_FSLASH 57 + FILE_SEP_BSLASH; 58 59 private static final char LRM = '\u200e'; 61 62 private static final char LRE = '\u202a'; 64 65 private static final char PDF = '\u202c'; 67 68 private static boolean isBidi = false; 70 71 private static boolean isSupportedPlatform = false; 73 74 private static final int INDEX_NOT_SET = 999999999; 76 77 static { 78 Locale locale = Locale.getDefault(); 79 String lang = locale.getLanguage(); 80 81 if ("iw".equals(lang) || "he".equals(lang) || "ar".equals(lang) || "fa".equals(lang) || "ur".equals(lang)) isBidi = true; 83 84 String osName = System.getProperty("os.name").toLowerCase(); if (osName.startsWith("windows") || osName.startsWith("linux")) { 87 isSupportedPlatform = true; 89 } 90 } 91 92 104 public static String process(String text) { 105 return process(text, getDefaultDelimiters()); 106 } 107 108 157 public static String process(String str, String delimiter) { 158 if (str == null || str.length() <= 1 || !isSupportedPlatform || !isBidi) 159 return str; 160 161 if (str.charAt(0) == LRE && str.charAt(str.length() - 1) == PDF) { 163 return str; 164 } 165 166 boolean isStringBidi = false; 168 boolean isLastRTL = false; 170 int delimIndex = INDEX_NOT_SET; 172 173 delimiter = delimiter == null ? getDefaultDelimiters() : delimiter; 174 175 StringBuffer target = new StringBuffer (); 176 target.append(LRE); 177 char ch; 178 179 for (int i = 0, n = str.length(); i < n; i++) { 180 ch = str.charAt(i); 181 if (delimiter.indexOf(ch) != -1) { 182 if (isLastRTL) { 184 delimIndex = target.length(); 185 } 186 } else if (Character.isDigit(ch)) { 187 if (delimIndex != INDEX_NOT_SET) { 188 target.insert(delimIndex, LRM); 191 delimIndex = INDEX_NOT_SET; 192 isLastRTL = false; 193 } 194 } else if (Character.isLetter(ch)) { 195 if (isRTL(ch)) { 196 isStringBidi = true; 197 if (delimIndex != INDEX_NOT_SET) { 198 target.insert(delimIndex, LRM); 201 delimIndex = INDEX_NOT_SET; 202 } 203 isLastRTL = true; 204 } else { 205 delimIndex = INDEX_NOT_SET; 207 isLastRTL = false; 208 } 209 } 210 target.append(ch); 211 } 212 221 if (isStringBidi || !Character.isLetter(str.charAt(0)) 222 || isNeutral(str.charAt(str.length() - 1))) { 223 target.append(PDF); 224 return target.toString(); 225 } 226 return str; 228 } 229 230 241 public static String deprocess(String str){ 242 if (str == null || str.length() <= 1 || !isSupportedPlatform || !isBidi) 244 return str; 245 246 StringBuffer buf = new StringBuffer (); 247 for (int i = 0; i < str.length(); i++){ 248 char c = str.charAt(i); 249 switch(c){ 250 case LRE: continue; 251 case PDF: continue; 252 case LRM: continue; 253 default: 254 buf.append(c); 255 } 256 } 257 258 return buf.toString(); 259 } 260 261 267 public static String getDefaultDelimiters() { 268 return delimiterString; 269 } 270 271 274 private static boolean isRTL(char c) { 275 285 return (((c >= 0x05d0) && (c <= 0x07b1)) || ((c >= 0xfb1d) && (c <= 0xfefc))); 286 } 287 288 291 private static boolean isNeutral(char c) { 292 return !(Character.isDigit(c) || Character.isLetter(c)); 293 } 294 295 298 private TextProcessor() { 299 } 301 } 302 | Popular Tags |