1 23 24 package org.infoglue.cms.applications.common; 25 26 import java.net.URLEncoder ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Date ; 29 import java.util.Locale ; 30 31 import org.apache.log4j.Logger; 32 import org.infoglue.cms.util.CmsPropertyHandler; 33 34 public class VisualFormatter 35 { 36 private final static Logger logger = Logger.getLogger(VisualFormatter.class.getName()); 37 38 public VisualFormatter() 39 { 40 } 41 42 public String formatDate(Date date, String pattern) 43 { 44 if(date == null) 45 return ""; 46 47 SimpleDateFormat formatter = new SimpleDateFormat (pattern); 48 String dateString = formatter.format(date); 49 50 return dateString; 51 } 52 53 public String formatDate(Date date, Locale locale, String pattern) 54 { 55 if(date == null) 56 return ""; 57 58 SimpleDateFormat formatter = new SimpleDateFormat (pattern, locale); 60 String dateString = formatter.format(date); 61 62 return dateString; 63 } 64 65 public Date parseDate(String dateString, String pattern) 66 { 67 if(dateString == null) 68 return new Date (); 69 70 Date date = new Date (); 71 72 try 73 { 74 SimpleDateFormat formatter = new SimpleDateFormat (pattern); 76 date = formatter.parse(dateString); 77 } 78 catch(Exception e) 79 { 80 logger.info("Error parsing date:" + dateString); 81 } 82 83 return date; 84 } 85 86 public Date parseDate(String dateString, Locale locale, String pattern) 87 { 88 if(dateString == null) 89 return new Date (); 90 91 Date date = new Date (); 92 93 try 94 { 95 SimpleDateFormat formatter = new SimpleDateFormat (pattern, locale); 97 date = formatter.parse(dateString); 98 } 99 catch(Exception e) 100 { 101 logger.info("Error parsing date:" + dateString); 102 } 103 104 return date; 105 } 106 107 110 111 public final String escapeHTML(String s) 112 { 113 if(s == null) 114 return null; 115 116 StringBuffer sb = new StringBuffer (); 117 int n = s.length(); 118 for (int i = 0; i < n; i++) 119 { 120 char c = s.charAt(i); 121 switch (c) 122 { 123 case '<': sb.append("<"); break; 124 case '>': sb.append(">"); break; 125 case '&': sb.append("&"); break; 126 case '"': sb.append("""); break; 127 167 168 default: sb.append(c); break; 169 } 170 } 171 return sb.toString(); 172 } 173 174 175 180 181 public final String escapeHTMLforXMLService(String s) 182 { 183 if(s == null) 184 return null; 185 186 StringBuffer sb = new StringBuffer (); 187 int n = s.length(); 188 for (int i = 0; i < n; i++) 189 { 190 char c = s.charAt(i); 191 switch (c) 192 { 193 case '<': sb.append("<"); break; 194 case '>': sb.append(">"); break; 195 case '&': sb.append("&"); break; 196 case '"': sb.append("""); break; 197 case 'à': sb.append("à");break; 198 case 'À': sb.append("À");break; 199 case 'â': sb.append("â");break; 200 case 'Â': sb.append("Â");break; 201 case 'ä': sb.append("ä");break; 202 case 'Ä': sb.append("Ä");break; 203 case 'å': sb.append("å");break; 204 case 'Å': sb.append("Å");break; 205 case 'æ': sb.append("æ");break; 206 case 'Æ': sb.append("Æ");break; 207 case 'ç': sb.append("ç");break; 208 case 'Ç': sb.append("Ç");break; 209 case 'é': sb.append("é");break; 210 case 'É': sb.append("É");break; 211 case 'è': sb.append("è");break; 212 case 'ò': sb.append("ò");break; 213 case 'È': sb.append("È");break; 214 case 'ê': sb.append("ê");break; 215 case 'Ê': sb.append("Ê");break; 216 case 'ë': sb.append("ë");break; 217 case 'Ë': sb.append("Ë");break; 218 case 'ï': sb.append("ï");break; 219 case 'Ï': sb.append("Ï");break; 220 case 'ô': sb.append("ô");break; 221 case 'Ô': sb.append("Ô");break; 222 case 'ö': sb.append("ö");break; 223 case 'Ö': sb.append("Ö");break; 224 case 'ø': sb.append("ø");break; 225 case 'Ø': sb.append("Ø");break; 226 case 'ß': sb.append("ß");break; 227 case 'ù': sb.append("ù");break; 228 case 'Ù': sb.append("Ù");break; 229 case 'û': sb.append("û");break; 230 case 'Û': sb.append("Û");break; 231 case 'ü': sb.append("ü");break; 232 case 'Ü': sb.append("Ü");break; 233 case '®': sb.append("®");break; 234 case '©': sb.append("©");break; 235 case '€': sb.append("€"); break; 236 case '\'': sb.append("’"); break; 237 238 default: sb.append(c); break; 239 } 240 } 241 return sb.toString(); 242 } 243 244 public final String escapeExtendedHTML(String s) 245 { 246 if(s == null) 247 return null; 248 249 StringBuffer sb = new StringBuffer (); 250 int n = s.length(); 251 for (int i = 0; i < n; i++) 252 { 253 char c = s.charAt(i); 254 switch (c) 255 { 256 case '<': sb.append("<"); break; 257 case '>': sb.append(">"); break; 258 case '&': sb.append("&"); break; 259 case '"': sb.append("""); break; 260 300 case '\'': sb.append("’"); break; 301 302 default: sb.append(c); break; 303 } 304 } 305 return sb.toString(); 306 } 307 308 311 312 public final String escapeForJavascripts(String s) 313 { 314 if(s == null) 315 return null; 316 317 StringBuffer sb = new StringBuffer (); 318 int n = s.length(); 319 for (int i = 0; i < n; i++) 320 { 321 char c = s.charAt(i); 322 if(c == '\'') sb.append("\\'"); 323 else sb.append(c); 324 } 325 326 return sb.toString(); 327 } 328 329 332 333 public final String cleanForJavascriptStrings(String s) 334 { 335 if(s == null) 336 return null; 337 338 String lineSep = System.getProperty("line.separator"); 339 s = s.replaceAll(lineSep, "<br/>"); 340 s = s.replaceAll("\n", "<br/>"); 341 s = s.replaceAll("\r", "<br/>"); 342 343 StringBuffer sb = new StringBuffer (); 344 int n = s.length(); 345 for (int i = 0; i < n; i++) 346 { 347 char c = s.charAt(i); 348 switch (c) 349 { 350 case '\'': sb.append("\\'"); break; 351 case '"': sb.append("""); break; 352 353 default: sb.append(c); break; 354 } 355 } 356 357 return sb.toString(); 358 } 359 360 363 364 public final String escapeForAdvancedJavascripts(String s) 365 { 366 if(s == null) 367 return null; 368 369 StringBuffer sb = new StringBuffer (); 370 int n = s.length(); 371 for (int i = 0; i < n; i++) 372 { 373 char c = s.charAt(i); 374 switch (c) 375 { 376 case '\'': sb.append("\\'"); break; 377 case '"': sb.append("""); break; 378 default: sb.append(c); break; 379 } 380 } 381 382 return sb.toString(); 383 } 384 385 public final String replaceNonAscii(String s, char character) 386 { 387 if(s == null) 388 return null; 389 390 StringBuffer sb = new StringBuffer (); 391 int n = s.length(); 392 for (int i = 0; i < n; i++) 393 { 394 char c = s.charAt(i); 395 if(c < 128 && c > 32) 396 { 397 if(Character.isLetterOrDigit(c) || c == '-' || c == '_' || c == '.') 398 sb.append(c); 399 else 400 sb.append(character); 401 } 402 else 403 { 404 sb.append(character); 405 } 406 } 407 return sb.toString(); 408 } 409 410 413 414 public final String encode(String s) throws Exception 415 { 416 if(s == null) 417 return null; 418 419 return URLEncoder.encode(s, "UTF-8"); 420 } 421 422 425 426 public final String encodeURI(String s) throws Exception 427 { 428 if(s == null) 429 return null; 430 431 String encoding = CmsPropertyHandler.getURIEncoding(); 432 433 return URLEncoder.encode(s, encoding); 434 } 435 436 437 public final String encodeURI(String s, String encoding) throws Exception 438 { 439 if(s == null) 440 return null; 441 442 return URLEncoder.encode(s, encoding); 443 } 444 445 } | Popular Tags |