📄 jahiatools.java
字号:
// $Id: JahiaTools.java,v 1.5 2002/12/20 14:15:54 shuber Exp $//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// JahiaTools//// 26.02.2001 NK added in Jahia.// 27.03.2001 AK changes in updatepropvalue().// 25.07.2001 SB added isValidURL()//package org.jahia.utils;import java.io.*;import java.util.*;import java.text.*;import java.net.*;import javax.servlet.*;import javax.servlet.http.*;import org.jahia.utils.keygenerator.JahiaKeyGen;/** * @author Jerome Tamiotti * * * Class Tools: * * # Debugging tools * # Date tools * # String tools * # Sql tools * # Files tools * # Comparison tools * # General Purpose tools * # Client Browser tools * */public class JahiaTools{ // authorized chars private static final char[] AUTHORIZED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789.-".toCharArray(); /************************************************************************** * Debugging Tools * * *************************************************************************/ //------------------------------------------------------------------------- /** * Method toConsole: print a debug message into server console * * @param localisation the current class name followed by the method name * @param msg the msg to print */ static public void toConsole(String localisation, String msg) { System.out.println(">> " + localisation + "(): " + msg); } //------------------------------------------------------------------------- /** * Method toConsole: print a debug message into server console * * @param msg the msg to print */ static public void toConsole(String msg) { System.out.println(">> " + msg); } /************************************************************************** * Dates Tools * * *************************************************************************/ // the mask used for date display private static String mask = "EEE, MMM d, yyyy"; //------------------------------------------------------------------------- /** * Method getDisplayedDate: return the date of today as a string * * @return the string value of today */ static public String getDisplayedDate() { Date today = new Date(); SimpleDateFormat formatter = new SimpleDateFormat(mask,Locale.US); String datenewformat = formatter.format(today); return datenewformat; } //------------------------------------------------------------------------- /** * Method getDisplayedDate: return the date represented by time as a string * * @return the string value of today */ static public String getDisplayedDate(long time) { Date today = new Date(time); SimpleDateFormat formatter = new SimpleDateFormat(mask,Locale.US); String datenewformat = formatter.format(today); return datenewformat; } //------------------------------------------------------------------------- /** * Method getDisplayedDate: return the date represented by time as a string * * @author AK * @return the string value of today */ static public String getDisplayedDate(String time) { Long tmpLong = new Long( time ); Date today = new Date(tmpLong.longValue()); SimpleDateFormat formatter = new SimpleDateFormat(mask,Locale.US); String datenewformat = formatter.format(today); return datenewformat; } //------------------------------------------------------------------------- /** * Method getCurrentDateInMs: return the date of today as a long value * * @return the long value of today */ static public long getCurrentDateInMs() { return System.currentTimeMillis(); } //------------------------------------------------------------------------- /** * Method getDateInMs: return a long value for the time represented by the * given year, month and day * @param year * @param month (1 to 12) * @param day * * @return the long value of time */ static public long getDateInMs(int year,int month,int day) { Calendar c = Calendar.getInstance(); // class calendar use monthes from 0 to 11, so decrement c.set(year,month-1,day); Date d = c.getTime(); return d.getTime(); } //------------------------------------------------------------------------- /** * Method getDayFromMs: from a date in ms from 1970, January 1st, return the day * @param time * * @return the day value */ static public int getDayFromMs(long time) { Calendar c = Calendar.getInstance(); c.setTime(new Date(time)); return c.get(c.DAY_OF_MONTH); } //------------------------------------------------------------------------- /** * Method getMonthFromMs: from a date in ms from 1970, January 1st, return the month * @param time * * @return the month value */ static public int getMonthFromMs(long time) { Calendar c = Calendar.getInstance(); c.setTime(new Date(time)); return c.get(c.MONTH); } //------------------------------------------------------------------------- /** * Method getYearFromMs: from a date in ms from 1970, January 1st, return the year * @param time * * @return the year value */ static public int getYearFromMs(long time) { Calendar c = Calendar.getInstance(); c.setTime(new Date(time)); return c.get(c.YEAR); } //------------------------------------------------------------------------- /** * Format a epoch time (string) to human readable date. * * @author AK */ public static String formatDateFromEpoch( String epochString ) { // get a human-readable data format long longTime = Long.parseLong(epochString); java.util.Date normalDate = new java.util.Date(longTime); return java.text.DateFormat.getDateTimeInstance(3,3).format(normalDate); } /************************************************************************** * String Tools * * *************************************************************************/ //------------------------------------------------------------------------- /** * Method replacePattern : replace a pattern in a text with another one * * @param str the text to alter * @param oldToken the token to replace * @param newToken the new text * * @return the altered text */ static public String replacePattern(String str, String oldToken, String newToken) { if (str==null){ return str; } StringBuffer result = new StringBuffer(str.length() + 100); int i = str.indexOf(oldToken); int startOfIndex = 0; while (i != -1) { result.append(str.substring(startOfIndex,i)); result.append(newToken); startOfIndex = i + oldToken.length(); i = str.indexOf(oldToken,startOfIndex); } result.append(str.substring(startOfIndex,str.length())); return result.toString(); } // if invert == 1 -> inverse oldTocken with newTocken static public String replacePattern(String str, String newToken, String oldToken, int invert ) { if (invert==0){ return replacePattern(str, newToken, oldToken); } else { // inverse arguments return replacePattern(str, oldToken, newToken); } } //------------------------------------------------------------------------- /** * Method getTokens : return an arrays of String tokens * * @param str the string to parse * @param sep the separator * @return an array of string values * @author NK */ static public String[] getTokens(String str, String sep) { if (str==null){ return null; } StringTokenizer st = new StringTokenizer(str,sep); String[] result = new String[st.countTokens()]; int count = 0; while ( st.hasMoreTokens() ){ result[count] = st.nextToken(); count++; } return result; } //------------------------------------------------------------------------- /** * Convert a String starting with the word "$context" into a real filesystem * path. This method is principally used by JahiaPrivateSettings and to * convert jahia.properties settings. * @author Alexandre Kraft * * @param convert The string to convert. * @param context The context used to get the real path. */ public static String convertContexted( String convert, ServletContext context ) { if(convert.substring(0,9).equals("$context/")) { convert = context.getRealPath( convert.substring(8, convert.length()) ); } return convert; } // end convertContexted //------------------------------------------------------------------------- /** * Convert a standard string to a property-compatible string. * @author Alexandre Kraft * * @param originalValue The string that you want to convert. * @return The string converted. */ public static String string2Property( String originalValue ) { StringBuffer convertedValue = new StringBuffer(); for(int i=0; i < originalValue.length(); i++) { if(originalValue.substring(i, i+1).equals(":")) { convertedValue.append( "\\:" ); } else if(originalValue.substring(i, i+1).equals("\\")) { convertedValue.append( "\\\\" ); } else { convertedValue.append( originalValue.substring(i, i+1) ); } } return convertedValue.toString(); } // end string2Property //------------------------------------------------------------------------- /** * Get each line from a string or a file and set it to an enumeration. * @author Alexandre Kraft
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -