1 18 19 package cowsultants.itracker.ejb.client.util; 20 21 import org.apache.oro.text.regex.*; 22 import org.apache.struts.util.*; 23 24 import java.util.*; 25 26 public class HTMLUtilities { 27 private static Perl5Matcher matcher = new Perl5Matcher(); 28 private static PatternCompiler compiler = new Perl5Compiler(); 29 private static Pattern pattern = null; 30 31 static { 32 try { 33 pattern = compiler.compile("<[\\w/].*?>", Perl5Compiler.CASE_INSENSITIVE_MASK); 34 } catch(MalformedPatternException mpe) { 35 Logger.logError("Invalid pattern in HTMLUtilities. " + mpe.getMessage()); 36 } 37 } 38 39 public static String removeQuotes(String input) { 40 StringBuffer sb = new StringBuffer (input.length()); 41 int len = input.length(); 42 char c; 43 44 for(int i = 0; i < len; i++) { 45 c = input.charAt(i); 46 if (c == '\'') { 47 sb.append("''"); 48 } else { 49 sb.append(c); 50 } 51 } 52 return sb.toString(); 53 } 54 55 public static String handleQuotes(String input) { 56 if(input == null || "".equals(input) || input.indexOf('"') == -1) { 57 return input; 58 } 59 60 StringBuffer buf = new StringBuffer (); 61 62 char[] chars = input.toCharArray(); 63 for(int i = 0; i < chars.length; i++) { 64 if(chars[i] == '"') { 65 buf.append("""); 66 } else { 67 buf.append(chars[i]); 68 } 69 } 70 71 return buf.toString(); 72 } 73 74 public static String escapeNewlines(String input) { 75 if(input == null || "".equals(input) || input.indexOf('\n') == -1) { 76 return input; 77 } 78 79 StringBuffer buf = new StringBuffer (); 80 char[] chars = input.toCharArray(); 81 for(int i = 0; i < chars.length; i++) { 82 if(chars[i] == '\r') { 83 continue; 84 } else if(chars[i] == '\n') { 85 buf.append("\\n"); 86 } else { 87 buf.append(chars[i]); 88 } 89 } 90 return buf.toString(); 91 } 92 93 public static String newlinesToBreaks(String input) { 94 if(input == null || "".equals(input) || input.indexOf('\n') == -1) { 95 return input; 96 } 97 98 StringBuffer buf = new StringBuffer (); 99 char[] chars = input.toCharArray(); 100 for(int i = 0; i < chars.length; i++) { 101 if(chars[i] == '\r') { 102 continue; 103 } else if(chars[i] == '\n') { 104 buf.append("<br>"); 105 } else { 106 buf.append(chars[i]); 107 } 108 } 109 return buf.toString(); 110 } 111 112 public static String removeMarkup(String input) { 113 String output = (input == null ? "" : input); 114 115 if(pattern != null && matcher != null && output != null && ! output.equals("")) { 116 output = Util.substitute(matcher, pattern, new Perl5Substitution(), output, Util.SUBSTITUTE_ALL); 117 } else { 118 Logger.logDebug("Failed removing markup. Pattern = " + pattern + " Output = " + output); 119 } 120 121 return output; 122 } 123 124 public static String escapeTags(String input) { 125 StringBuffer sb = new StringBuffer (input.length()); 126 int len = input.length(); 127 char c; 128 129 for(int i = 0; i < len; i++) { 130 c = input.charAt(i); 131 if (c == '"') { 132 sb.append("""); 133 } else if (c == '&') { 134 sb.append("&"); 135 } else if (c == '<') { 136 sb.append("<"); 137 } else if (c == '>') { 138 sb.append(">"); 139 } else { 140 int ci = 0xffff & c; 141 if (ci < 160 ) { 142 sb.append(c); 144 } else { 145 sb.append("&#"); 147 sb.append(new Integer (ci).toString()); 148 sb.append(';'); 149 } 150 } 151 } 152 153 return sb.toString(); 154 } 155 } 156 157 | Popular Tags |