1 package org.tigris.scarab.util; 2 3 48 49 import java.util.Iterator ; 50 import java.util.List ; 51 52 import org.apache.commons.lang.StringUtils; 53 import org.apache.oro.text.perl.Perl5Util; 54 import org.apache.turbine.RunData; 55 import org.tigris.scarab.om.Module; 56 57 63 public class ScarabUtil 64 { 65 private static final String REGEX_URL = 66 "s%\\b(?:[hH][tT]{2}[pP]|[fF][tT][pP]):[^ \\t\\n<>\"]+[\\w/]*%<a HREF=\"$0\">$0</a>%g"; 67 private static final String REGEX_MAILTO = 68 "s%\\b(?:([mM][aA][iI][lL][tT][oO])):([^ \\t\\n<>\"]+[\\w/])*%<a HREF=\"$0\">$2</a>%g"; 69 private static final String REGEX_NEWLINETOBR = 70 "s%\\n%<br />%g"; 71 72 private static Perl5Util perlUtil = new Perl5Util(); 73 74 75 85 public static String findValue(RunData runData, String name) 86 { 87 String [] possibilities = runData.getParameters().getStrings(name); 88 if (possibilities != null) 89 { 90 for (int i = 0; i < possibilities.length; i++) 91 { 92 if (StringUtils.isNotEmpty(possibilities[i])) 93 { 94 return possibilities[i]; 95 } 96 } 97 } 98 return null; 99 } 100 101 107 public static String linkifyText(String input, ScarabLink link, Module module) 108 throws Exception 109 { 110 StringBuffer sb = new StringBuffer (input.length() * 2); 111 String output = ReferenceInsertionFilter.filter(input); 113 output = perlUtil.substitute(REGEX_NEWLINETOBR,output); 114 output = perlUtil.substitute(REGEX_MAILTO,output); 115 output = perlUtil.substitute(REGEX_URL,output); 116 List result = IssueIdParser.tokenizeText(module, output); 117 for (Iterator itr = result.iterator(); itr.hasNext();) 118 { 119 Object tmp = itr.next(); 120 if (tmp instanceof String ) 121 { 122 sb.append(tmp); 123 } 124 else 125 { 126 List tmpList = (List )tmp; 127 link.addPathInfo("id", (String )tmpList.get(1)); 128 link.setLabel((String )tmpList.get(0)); 129 String bar = link.setAlternateText((String )tmpList.get(0)).toString(); 130 sb.append(bar); 131 } 132 } 133 return sb.toString(); 134 } 135 136 139 public static final boolean contains(Object [] array, Object obj) 140 { 141 boolean contains = false; 142 if (array != null && array.length > 0) 143 { 144 for (int i = 0; i < array.length; i++) 145 { 146 Object element = array[i]; 147 if (obj.equals(element)) 148 { 149 contains = true; 150 break; 151 } 152 } 153 } 154 return contains; 155 } 156 157 169 public static final String urlEncode(String in) 170 { 171 if (in == null) 172 { 173 return null; 174 } 175 176 if (in.length() == 0) 177 { 178 return ""; 179 } 180 181 StringBuffer out = new StringBuffer (in.length()); 182 char[] chars = in.toCharArray(); 183 184 for (int i = 0; i < chars.length; i++) 185 { 186 char c = chars[i]; 187 188 if (c < 128 && safe[c]) 189 { 190 out.append(c); 191 } 192 else if (c == ' ') 193 { 194 out.append('+'); 195 } 196 else 197 { 198 return out 201 .append(urlEncodeSlow(in.substring(i).getBytes())) 202 .toString(); 203 } 204 } 205 return out.toString(); 206 } 207 208 218 private static final String urlEncodeSlow(byte[] bytes) 219 { 220 StringBuffer out = new StringBuffer (bytes.length * 2); 221 222 for (int i = 0; i < bytes.length; i++) 223 { 224 char c = (char)bytes[i]; 225 226 if (c < 128 && safe[c]) 227 { 228 out.append(c); 229 } 230 else if (c == ' ') 231 { 232 out.append('+'); 233 } 234 else 235 { 236 byte toEscape = bytes[i]; 237 out.append('%'); 238 int low = (toEscape & 0x0f); 239 int high = ((toEscape & 0xf0) >> 4); 240 out.append(HEXADECIMAL[high]); 241 out.append(HEXADECIMAL[low]); 242 } 243 } 244 return out.toString(); 245 } 246 247 250 private static final char[] HEXADECIMAL = 251 { 252 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 253 'A', 'B', 'C', 'D', 'E', 'F' 254 }; 255 256 260 private static boolean[] safe = new boolean[ 128 ]; 261 262 263 static 264 { 265 for (int i = 'a'; i <= 'z'; i++) 266 { 267 safe[ i ] = true; 268 } 269 for (int i = 'A'; i <= 'Z'; i++) 270 { 271 safe[ i ] = true; 272 } 273 for (int i = '0'; i <= '9'; i++) 274 { 275 safe[ i ] = true; 276 } 277 278 safe['-'] = true; 279 safe['_'] = true; 280 safe['.'] = true; 281 safe['!'] = true; 282 safe['~'] = true; 283 safe['*'] = true; 284 safe['\''] = true; 285 safe['('] = true; 286 safe[')'] = true; 287 } 288 } 289 | Popular Tags |