1 11 12 package org.eclipse.pde.internal.core.util; 13 14 import java.util.HashMap ; 15 16 20 public class PDEHTMLHelper { 21 22 public final static HashMap fgEntityLookup = new HashMap (6); 23 static { 24 fgEntityLookup.put("lt", "<"); fgEntityLookup.put("gt", ">"); fgEntityLookup.put("nbsp", " "); fgEntityLookup.put("amp", "&"); fgEntityLookup.put("apos", "'"); fgEntityLookup.put("quot", "\""); } 31 32 public static String stripTags(String html) { 33 if (html == null) { 34 return null; 35 } 36 int length = html.length(); 37 boolean write = true; 38 char oldChar = ' '; 39 StringBuffer sb = new StringBuffer (length); 40 41 boolean processingEntity = false; 42 StringBuffer entityBuffer = null; 43 44 for (int i = 0; i < length; i++) { 45 char curr = html.charAt(i); 46 47 if (curr == '&') { 49 processingEntity = true; 51 entityBuffer = new StringBuffer (); 52 continue; 53 } else if (processingEntity && (curr == ';')) { 54 processingEntity = false; 56 String entity = ((String )fgEntityLookup.get(entityBuffer.toString())); 58 if (entity == null) { 59 continue; 61 } 62 curr = entity.charAt(0); 64 } else if (processingEntity) { 65 entityBuffer.append(curr); 68 continue; 69 } 70 71 if (curr == '<') { 72 write = false; 73 } else if (curr == '>') { 74 write = true; 75 } else if (write && curr != '\r' && curr != '\n' && curr != '\t') { 76 if (!(curr == ' ') || !(oldChar == curr)) { sb.append(curr); 78 oldChar = curr; 79 } 80 } 81 } 82 if (isAllWhitespace(sb.toString())) { 83 return null; 84 } 85 return sb.toString(); 86 } 87 88 public static boolean isAllWhitespace(String string) { 89 if (string == null) { 90 return false; 91 } 92 char[] characters = string.toCharArray(); 93 for (int i = 0; i < characters.length; i++) { 94 if (!Character.isWhitespace(characters[i])) { 95 return false; 96 } 97 } 98 return true; 99 } 100 101 } 102 | Popular Tags |