1 11 12 package org.eclipse.pde.internal.core.util; 13 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.regex.Matcher ; 18 import java.util.regex.Pattern ; 19 20 24 public class PDETextHelper { 25 26 public static final String F_DOTS = "..."; 28 32 public static String truncateAndTrailOffText(String text, int limit) { 33 String trimmed = text.trim(); 34 int dotsLength = F_DOTS.length(); 35 int trimmedLength = trimmed.length(); 36 int limitWithDots = limit - dotsLength; 37 38 if (limit >= trimmedLength) { 39 return trimmed; 40 } 41 if (limit <= dotsLength) { 43 return ""; } 45 return trimmed.substring(0, limitWithDots) + F_DOTS; 47 } 48 49 53 public static boolean isDefined(String text) { 54 if ((text == null) || 55 (text.length() == 0)) { 56 return false; 57 } 58 return true; 59 } 60 61 65 public static boolean isDefinedAfterTrim(String text) { 66 if (text == null) { 67 return false; 68 } 69 String trimmedText = text.trim(); 70 if (trimmedText.length() == 0) { 71 return false; 72 } 73 return true; 74 } 75 76 82 public static String translateReadText(String text) { 83 if (text == null) { 85 return ""; } 87 String result = ""; String inputText = text.trim(); 90 int length = inputText.length(); 91 char previousChar = ' '; 92 StringBuffer buffer = new StringBuffer (length); 93 for (int i = 0; i < length; i++) { 95 char currentChar = inputText.charAt(i); 96 97 if ((currentChar == '\r') || (currentChar == '\n') || 98 (currentChar == '\t')) { 99 currentChar = ' '; 101 } 102 103 if (currentChar == ' ') { 104 if (previousChar != ' ') { 106 buffer.append(currentChar); 107 previousChar = currentChar; 108 } 109 } else { 110 buffer.append(currentChar); 111 previousChar = currentChar; 112 } 113 } 114 result = buffer.toString(); 115 if (PDEHTMLHelper.isAllWhitespace(result)) { 116 return ""; } 118 return result; 119 } 120 121 126 public static String translateWriteText(String text, HashMap substituteChars) { 127 return translateWriteText(text, null, substituteChars); 128 } 129 130 136 public static String translateWriteText(String text, HashSet tagExceptions, 137 HashMap substituteChars) { 138 if (text == null) { 140 return ""; } 142 boolean processTagExceptions = false; 144 int scanLimit = 0; 145 if ((tagExceptions != null) && 146 (tagExceptions.isEmpty() == false)) { 147 processTagExceptions = true; 148 scanLimit = determineMaxLength(tagExceptions); 150 } 151 boolean processSubstituteChars = false; 153 if ((substituteChars != null) && 154 (substituteChars.isEmpty() == false)) { 155 processSubstituteChars = true; 156 } 157 StringBuffer buffer = new StringBuffer (text.length()); 159 for (IntegerPointer index = new IntegerPointer(0); 161 index.getInteger() < text.length(); 162 index.increment()) { 163 char currentChar = text.charAt(index.getInteger()); 165 boolean processed = false; 166 if ((processed == false) && 170 (processTagExceptions == true)) { 171 processed = processTagExceptions(currentChar, substituteChars, 172 tagExceptions, buffer, scanLimit, text, index); 173 } 174 if ((processed == false) && 179 (processSubstituteChars == true)) { 180 processed = processSubstituteChars(currentChar, substituteChars, 181 buffer); 182 } 183 if (processed == false) { 186 buffer.append(currentChar); 187 } 188 } 189 return buffer.toString(); 191 } 192 193 199 private static boolean processSubstituteChars(char currentChar, 200 HashMap substituteChars, StringBuffer buffer) { 201 Character character = new Character (currentChar); 202 if (substituteChars.containsKey(character)) { 203 String value = (String )substituteChars.get(character); 204 if (isDefined(value)) { 205 buffer.append(value); 207 } 208 return true; 210 } 211 return false; 212 } 213 214 223 private static boolean processTagExceptions(char currentChar, 224 HashMap substituteChars, HashSet tagExceptions, StringBuffer buffer, int scanLimit, 225 String text, IntegerPointer index) { 226 if (currentChar == '<') { 229 int limit = text.length() + index.getInteger() + 2; 233 StringBuffer parsedText = new StringBuffer (); 235 for (int j = index.getInteger() + 1; j < limit; j++) { 237 char futureChar = text.charAt(j); 238 if (futureChar == '>') { 239 String futureBuffer = parsedText.toString(); 244 if (isValidTagException(tagExceptions, futureBuffer)) { 245 processTagExceptionCharacters(substituteChars, buffer, futureBuffer); 248 index.setInteger(j); 251 return true; 252 } 253 return false; 254 } 255 parsedText.append(futureChar); 257 } 258 } 259 return false; 260 } 261 262 267 private static void processTagExceptionCharacters(HashMap substituteChars, StringBuffer buffer, String text) { 268 String tagName = getTagName(text); 270 boolean trailingSlash = text.endsWith("/"); String attributeList = text.substring(tagName.length()); 275 if ((isValidTagAttributeList(attributeList) == false)) { 277 buffer.append('<'); 278 buffer.append(tagName); 279 if (trailingSlash) { 282 buffer.append('/'); 283 } 284 buffer.append('>'); 285 return; 286 } else if (attributeList.length() == 0) { 287 buffer.append('<'); 290 buffer.append(tagName); 291 buffer.append('>'); 292 return; 293 } 294 boolean inQuote = false; 295 buffer.append('<'); 297 for (int i = 0; i < text.length(); i++) { 301 boolean processed = false; 302 char currentChar = text.charAt(i); 303 boolean onQuote = (currentChar == '"'); 304 if (onQuote) { 306 if (inQuote) { 307 inQuote = false; 309 } else { 310 inQuote = true; 312 } 313 } 314 if (inQuote && !onQuote) { 317 processed = processSubstituteChars(currentChar, substituteChars, 318 buffer); 319 } 320 if (processed == false) { 323 buffer.append(currentChar); 324 } 325 } 326 buffer.append('>'); 328 } 329 330 335 private static boolean isValidTagException(HashSet tagExceptions, String buffer) { 336 String tagName = getTagName(buffer); 342 if (tagExceptions.contains(tagName)) { 344 return true; 345 } 346 return false; 347 } 348 349 353 private static boolean isValidTagAttributeList(String text) { 354 361 String patternString = "^([\\s]+[A-Za-z0-9_:\\-\\.]+[\\s]?=[\\s]?\".+?\")*[\\s]*[/]?$"; Pattern pattern = Pattern.compile(patternString); 364 Matcher matcher = pattern.matcher(text); 365 return matcher.find(); 367 } 368 369 373 private static String getTagName(String buffer) { 374 StringBuffer tagName = new StringBuffer (); 379 for (int i = 0; i < buffer.length(); i++) { 382 char character = buffer.charAt(i); 383 if (Character.isWhitespace(character)) { 384 break; 385 } 386 tagName.append(character); 387 } 388 return tagName.toString(); 389 } 390 391 395 private static int determineMaxLength(HashSet set) { 396 Iterator iterator = set.iterator(); 397 int maxLength = -1; 398 while (iterator.hasNext()) { 399 String object = (String )iterator.next(); 401 if (object.length() > maxLength) { 402 maxLength = object.length(); 403 } 404 } 405 return maxLength; 406 } 407 408 412 private static class IntegerPointer { 413 414 private int fInteger; 415 416 419 public IntegerPointer(int integer) { 420 fInteger = integer; 421 } 422 423 426 public int getInteger() { 427 return fInteger; 428 } 429 430 433 public void setInteger(int integer) { 434 fInteger = integer; 435 } 436 437 440 public void increment() { 441 fInteger++; 442 } 443 } 444 445 } 446 | Popular Tags |