Java tutorial
/* =============================================================================== * * Part of the InfoGlue Content Management Platform (www.infoglue.org) * * =============================================================================== * * Copyright (C) * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 2, as published by the * Free Software Foundation. See the file LICENSE.html for more information. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple * Place, Suite 330 / Boston, MA 02111-1307 / USA. * * =============================================================================== */ package org.infoglue.cms.applications.common; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.Properties; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringEscapeUtils; import org.apache.log4j.Logger; import org.infoglue.cms.util.CmsPropertyHandler; import org.infoglue.deliver.util.CacheController; public class VisualFormatter { private final static Logger logger = Logger.getLogger(VisualFormatter.class.getName()); public VisualFormatter() { } public String formatDate(Date date, String pattern) { if (date == null) return ""; SimpleDateFormat formatter = new SimpleDateFormat(pattern); String dateString = formatter.format(date); return dateString; } public String formatDate(Date date, Locale locale, String pattern) { if (date == null) return ""; // Format the current time. SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale); String dateString = formatter.format(date); return dateString; } public Date parseDate(String dateString, String pattern) { if (dateString == null) return new Date(); Date date = new Date(); try { // Format the current time. SimpleDateFormat formatter = new SimpleDateFormat(pattern); date = formatter.parse(dateString); } catch (Exception e) { logger.info("Error parsing date:" + dateString); } return date; } public Date parseDate(String dateString, Locale locale, String pattern) { if (dateString == null) return new Date(); Date date = new Date(); try { // Format the current time. SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale); date = formatter.parse(dateString); } catch (Exception e) { logger.info("Error parsing date:" + dateString); } return date; } /** * This method converts all non-standard characters to html-equivalents. */ public final String escapeHTML(String s) { if (s == null) return null; StringBuffer sb = new StringBuffer(); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); switch (c) { case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '&': sb.append("&"); break; case '"': sb.append("""); break; /* case '': sb.append("à");break; case '': sb.append("À");break; case '': sb.append("â");break; case '': sb.append("Â");break; case '': sb.append("ä");break; case '': sb.append("Ä");break; case '': sb.append("å");break; case '': sb.append("Å");break; case '': sb.append("æ");break; case '': sb.append("Æ");break; case '': sb.append("ç");break; case '': sb.append("Ç");break; case '': sb.append("é");break; case '': sb.append("É");break; case '': sb.append("è");break; case '': sb.append("È");break; case '': sb.append("ê");break; case '': sb.append("Ê");break; case '': sb.append("ë");break; case '': sb.append("Ë");break; case '': sb.append("ï");break; case '': sb.append("Ï");break; case '': sb.append("ô");break; case '': sb.append("Ô");break; case '': sb.append("ö");break; case '': sb.append("Ö");break; case '': sb.append("ø");break; case '': sb.append("Ø");break; case '': sb.append("ß");break; case '': sb.append("ù");break; case '': sb.append("Ù");break; case '': sb.append("û");break; case '': sb.append("Û");break; case '': sb.append("ü");break; case '': sb.append("Ü");break; case '': sb.append("®");break; case '': sb.append("©");break; case '': sb.append("€"); break; */ default: sb.append(c); break; } } return sb.toString(); } /** * * Temporary method, please do not use. (SS, 2004-12-13) * @deprecated */ public final String escapeHTMLforXMLService(String s) { if (s == null) return null; StringBuffer sb = new StringBuffer(); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); switch (c) { case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '&': sb.append("&"); break; case '"': sb.append("""); break; case '': sb.append("à"); break; case '': sb.append("À"); break; case '': sb.append("â"); break; case '': sb.append("Â"); break; case '': sb.append("ä"); break; case '': sb.append("Ä"); break; case '': sb.append("å"); break; case '': sb.append("Å"); break; case '': sb.append("æ"); break; case '': sb.append("Æ"); break; case '': sb.append("ç"); break; case '': sb.append("Ç"); break; case '': sb.append("é"); break; case '': sb.append("É"); break; case '': sb.append("è"); break; case '': sb.append("ò"); break; case '': sb.append("È"); break; case '': sb.append("ê"); break; case '': sb.append("Ê"); break; case '': sb.append("ë"); break; case '': sb.append("Ë"); break; case '': sb.append("ï"); break; case '': sb.append("Ï"); break; case '': sb.append("ô"); break; case '': sb.append("Ô"); break; case '': sb.append("ö"); break; case '': sb.append("Ö"); break; case '': sb.append("ø"); break; case '': sb.append("Ø"); break; case '': sb.append("ß"); break; case '': sb.append("ù"); break; case '': sb.append("Ù"); break; case '': sb.append("û"); break; case '': sb.append("Û"); break; case '': sb.append("ü"); break; case '': sb.append("Ü"); break; case '': sb.append("®"); break; case '': sb.append("©"); break; case '': sb.append("€"); break; case '\'': sb.append("’"); break; default: sb.append(c); break; } } return sb.toString(); } public final String escapeExtendedHTML(String s) { if (s == null) return null; StringBuffer sb = new StringBuffer(); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); switch (c) { case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '&': sb.append("&"); break; case '"': sb.append("""); break; /* case '': sb.append("à");break; case '': sb.append("À");break; case '': sb.append("â");break; case '': sb.append("Â");break; case '': sb.append("ä");break; case '': sb.append("Ä");break; case '': sb.append("å");break; case '': sb.append("Å");break; case '': sb.append("æ");break; case '': sb.append("Æ");break; case '': sb.append("ç");break; case '': sb.append("Ç");break; case '': sb.append("é");break; case '': sb.append("É");break; case '': sb.append("è");break; case '': sb.append("È");break; case '': sb.append("ê");break; case '': sb.append("Ê");break; case '': sb.append("ë");break; case '': sb.append("Ë");break; case '': sb.append("ï");break; case '': sb.append("Ï");break; case '': sb.append("ô");break; case '': sb.append("Ô");break; case '': sb.append("ö");break; case '': sb.append("Ö");break; case '': sb.append("ø");break; case '': sb.append("Ø");break; case '': sb.append("ß");break; case '': sb.append("ù");break; case '': sb.append("Ù");break; case '': sb.append("û");break; case '': sb.append("Û");break; case '': sb.append("ü");break; case '': sb.append("Ü");break; case '': sb.append("®");break; case '': sb.append("©");break; case '': sb.append("€"); break; */ case '\'': sb.append("’"); break; default: sb.append(c); break; } } return sb.toString(); } /** * This method converts all non-standard characters to html-equivalents. */ public final String escapeForJavascripts(String s) { if (s == null) return null; StringBuffer sb = new StringBuffer(); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); if (c == '\'') sb.append("\\'"); else sb.append(c); } return sb.toString(); } /** * This method converts all non-standard characters to html-equivalents. */ public final String cleanForJavascriptStrings(String s) { if (s == null) return null; String lineSep = System.getProperty("line.separator"); s = s.replaceAll(lineSep, "<br/>"); s = s.replaceAll("\n", "<br/>"); s = s.replaceAll("\r", "<br/>"); StringBuffer sb = new StringBuffer(); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); switch (c) { case '\'': sb.append("\\'"); break; case '"': sb.append("""); break; default: sb.append(c); break; } } return sb.toString(); } public final String cleanForJavascriptStrings(String s, String lineReplaceString) { if (s == null) return null; String lineSep = System.getProperty("line.separator"); if (lineSep != null) s = s.replaceAll(lineSep, lineReplaceString); s = s.replaceAll("\n", lineReplaceString); s = s.replaceAll("\r", lineReplaceString); StringBuffer sb = new StringBuffer(); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); switch (c) { case '\'': sb.append("\\'"); break; case '"': sb.append("""); break; default: sb.append(c); break; } } return sb.toString(); } /** * This method converts all non-standard characters to html-equivalents. */ public final String escapeForAdvancedJavascripts(String s) { if (s == null) return null; StringBuffer sb = new StringBuffer(); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); switch (c) { case '\'': sb.append("\\'"); break; case '"': sb.append("""); break; default: sb.append(c); break; } } return sb.toString(); } public final String replaceNonAscii(String s, char character) { if (s == null) return null; StringBuffer sb = new StringBuffer(); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); if (c < 128 && c > 32) { if (Character.isLetterOrDigit(c) || c == '-' || c == '_' || c == '.') sb.append(c); else sb.append(character); } else { sb.append(character); } } return sb.toString(); } public final String replaceNonAscii(String s, String character) { if (s == null) return null; StringBuffer sb = new StringBuffer(); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); if (c < 128 && c > 32) { if (Character.isLetterOrDigit(c) || c == '-' || c == '_' || c == '.') sb.append(c); else sb.append(character); } else { sb.append(character); } } return sb.toString(); } /** * This method replaces all non-ascii-characters with a corresponding one defined in the system properties-object. * If not defined there it replaces the char with the default character. * @param s * @param defaultCharacter * @return */ public final String replaceNiceURINonAsciiWithSpecifiedChars(String s, String defaultCharacter) { if (s == null) return null; boolean toLowerCase = CmsPropertyHandler.getNiceURIUseLowerCase(); Properties properties = CmsPropertyHandler.getCharacterReplacingMapping(); StringBuffer sb = new StringBuffer(); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); if (c < 128 && c > 32) { if (Character.isLetterOrDigit(c) || c == '-' || c == '_' || c == '.') sb.append(c); else { String replaceChar = properties.getProperty("" + c); if (replaceChar != null && !replaceChar.equals("")) sb.append(replaceChar); else if (defaultCharacter != null && !defaultCharacter.equalsIgnoreCase("none")) sb.append(defaultCharacter); } } else { String replaceChar = properties.getProperty("" + c); if (replaceChar != null && !replaceChar.equals("")) sb.append(replaceChar); else if (defaultCharacter != null && !defaultCharacter.equalsIgnoreCase("none")) sb.append(defaultCharacter); } } return (toLowerCase ? sb.toString().toLowerCase() : sb.toString()); } /** * This method replaces all non-ascii-characters with a corresponding one defined in the system properties-object. * If not defined there it replaces the char with the default character. * @param s * @param defaultCharacter * @return */ public final String replaceNonAsciiWithNumericEntity(String s) { if (s == null) return null; return StringEscapeUtils.escapeHtml(s); } /** * This method converts all non-standard characters to html-equivalents. */ public final String encode(String s) throws Exception { if (s == null) return null; String encodedString = (String) CacheController.getCachedObjectFromAdvancedCache("encodedStringsCache", s); if (encodedString == null) { encodedString = URLEncoder.encode(s, "UTF-8"); CacheController.cacheObjectInAdvancedCache("encodedStringsCache", s, encodedString, null, false); } return encodedString; } public final String encodeBase64(String s) throws Exception { if (s == null) return null; return Base64.encodeBase64URLSafeString(s.getBytes("utf-8")); } /** * This method converts all non-standard characters to html-equivalents. */ public final String encodeURI(String s) throws Exception { if (s == null) return null; String encoding = CmsPropertyHandler.getURIEncoding(); String encodedString = (String) CacheController.getCachedObjectFromAdvancedCache("encodedStringsCache", s); if (encodedString == null) { encodedString = URLEncoder.encode(s, encoding); CacheController.cacheObjectInAdvancedCache("encodedStringsCache", s, encodedString, null, false); } return encodedString; } public final String encodeURI(String s, String encoding) throws Exception { if (s == null) return null; String encodedString = (String) CacheController.getCachedObjectFromAdvancedCache("encodedStringsCache", s); if (encodedString == null) { encodedString = URLEncoder.encode(s, encoding); CacheController.cacheObjectInAdvancedCache("encodedStringsCache", s, encodedString, null, false); } return encodedString; } public String formatFileSize(Object fileSizeObject) { if (fileSizeObject == null) return ""; String fileSizeString = ""; Long fileSize = null; if (fileSizeObject instanceof String) fileSize = new Long((String) fileSizeObject); else if (fileSizeObject instanceof Integer) fileSize = new Long((Integer) fileSizeObject); else if (fileSizeObject instanceof Long) fileSize = (Long) fileSizeObject; if (fileSize.longValue() >= 1000000000000L) fileSizeString = "" + fileSize / (1000 * 1000 * 1000 * 1000) + " TB"; if (fileSize.longValue() >= 1000000000) fileSizeString = "" + fileSize / (1000 * 1000 * 1000) + " GB"; else if (fileSize.longValue() >= 1000000) fileSizeString = "" + fileSize / (1000 * 1000) + " MB"; else if (fileSize.longValue() >= 1000) fileSizeString = "" + fileSize / 1000 + " KB"; else fileSizeString = "" + fileSize + " Bytes"; return fileSizeString; } }