⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utilities.java

📁 这个weblogging 设计得比较精巧
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package org.roller.util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.security.MessageDigest;import java.util.Date;import java.util.NoSuchElementException;import java.util.StringTokenizer;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.commons.lang.StringEscapeUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * General purpose utilities.  *  * <pre> * Includes TextToHTML methods dondated by Erik Thauvin - "Donated to the  * Roller Weblogger project for publication under the terms of the Roller  * Software License.  * Copyright (C) 2002-2003 by Erik C. Thauvin (erik@thauvin.net). * All rights reserved. * </pre> *  * @author David M Johnson * @author Lance Lavandowska * @author Matt Raible (added encryption methods) */public class Utilities{    /** The <code>Log</code> instance for this class. */    private static Log mLogger = LogFactory.getLog(Utilities.class);        /** Pattern for matching HTML links */    private static Pattern mLinkPattern =         Pattern.compile("<a href=.*?>", Pattern.CASE_INSENSITIVE);        /**     * Utility methods for calling StringUtils since it cannot be      * instantiated and Utilties can.     */    public static boolean isNotEmpty(String str)    {        return StringUtils.isNotEmpty(str);    }        //------------------------------------------------------------------------    /** Strip jsessionid off of a URL */    public static String stripJsessionId( String url )    {        // Strip off jsessionid found in referer URL        int startPos = url.indexOf(";jsessionid=");        if ( startPos != -1 )        {            int endPos = url.indexOf("?",startPos);            if ( endPos == -1 )            {                url = url.substring(0,startPos);               }            else            {                url = url.substring(0,startPos)                    + url.substring(endPos,url.length());               }        }        return url;    }    //------------------------------------------------------------------------    /**     * Escape, but do not replace HTML.     * The default behaviour is to escape ampersands.     */    public static String escapeHTML(String s)    {        return escapeHTML(s, true);    }    //------------------------------------------------------------------------    /**     * Escape, but do not replace HTML.     * @param escapseAmpersand Optionally escape     * ampersands (&amp;).     */    public static String escapeHTML(String s, boolean escapeAmpersand)    {        // got to do amp's first so we don't double escape        if (escapeAmpersand)        {            s = stringReplace(s, "&", "&amp;");        }        s = stringReplace(s, "&nbsp;", " ");        s = stringReplace(s, "\"", "&quot;");        s = stringReplace(s, "<", "&lt;");        s = stringReplace(s, ">", "&gt;");        return s;    }    //------------------------------------------------------------------------    /**     * Remove occurences of html, defined as any text     * between the characters "&lt;" and "&gt;".  Replace     * any HTML tags with a space.     */    public static String removeHTML(String str)    {        return removeHTML(str, true);    }        /**     * Remove occurences of html, defined as any text     * between the characters "&lt;" and "&gt;".      * Optionally replace HTML tags with a space.     *      * @param str     * @param addSpace     * @return     */    public static String removeHTML(String str, boolean addSpace)    {        if (str == null) return "";        StringBuffer ret = new StringBuffer(str.length());        int start = 0;        int beginTag = str.indexOf("<");        int endTag = 0;        if (beginTag == -1)            return str;        while (beginTag >= start)        {            if (beginTag > 0)            {                ret.append(str.substring(start, beginTag));                                // replace each tag with a space (looks better)                if (addSpace) ret.append(" ");            }            endTag = str.indexOf(">", beginTag);                        // if endTag found move "cursor" forward            if (endTag > -1)            {                start = endTag + 1;                beginTag = str.indexOf("<", start);            }            // if no endTag found, get rest of str and break            else            {                ret.append(str.substring(beginTag));                break;            }        }        // append everything after the last endTag        if (endTag > -1 && endTag + 1 < str.length())        {            ret.append(str.substring(endTag + 1));        }        return ret.toString().trim();    }    //------------------------------------------------------------------------    /** Run both removeHTML and escapeHTML on a string.     * @param s String to be run through removeHTML and escapeHTML.     * @return String with HTML removed and HTML special characters escaped.     */    public static String removeAndEscapeHTML( String s )    {        if ( s==null ) return "";        else return Utilities.escapeHTML( Utilities.removeHTML(s) );    }    //------------------------------------------------------------------------    /**     * Autoformat.     */    public static String autoformat(String s)    {        String ret = StringUtils.replace(s, "\n", "<br />");        return ret;    }        //------------------------------------------------------------------------    /**     * Format date in ISO-8601 format.     */    public static String formatIso8601Date(Date d)    {        return DateUtil.formatIso8601(d);    }    //------------------------------------------------------------------------    /**     * Format date in ISO-8601 format.     */    public static String formatIso8601Day(Date d)    {        return DateUtil.formatIso8601Day(d);    }    //------------------------------------------------------------------------    /**     * Return a date in RFC-822 format.     */    public static String formatRfc822Date(Date date)     {        return DateUtil.formatRfc822(date);    }    //------------------------------------------------------------------------    /**     * Return a date in RFC-822 format.     */    public static String format8charsDate(Date date)     {        return DateUtil.format8chars(date);    }	//------------------------------------------------------------------------	/**	 * Replaces occurences of non-alphanumeric characters with an underscore.	 */	public static String replaceNonAlphanumeric(String str)	{		return replaceNonAlphanumeric(str, '_');	}	//------------------------------------------------------------------------	/**	 * Replaces occurences of non-alphanumeric characters with a	 * supplied char.	 */	public static String replaceNonAlphanumeric(String str, char subst)	{		StringBuffer ret = new StringBuffer(str.length());		char[] testChars = str.toCharArray();		for (int i = 0; i < testChars.length; i++)		{			if (Character.isLetterOrDigit(testChars[i]))			{				ret.append(testChars[i]);			}			else			{				ret.append( subst );			}		}		return ret.toString();	}    //------------------------------------------------------------------------    /**     * Remove occurences of non-alphanumeric characters.     */    public static String removeNonAlphanumeric(String str)    {        StringBuffer ret = new StringBuffer(str.length());        char[] testChars = str.toCharArray();        for (int i = 0; i < testChars.length; i++)        {            // MR: Allow periods in page links            if (Character.isLetterOrDigit(testChars[i]) ||                testChars[i] == '.')            {                ret.append(testChars[i]);            }        }        return ret.toString();    }    //------------------------------------------------------------------------    /**     * @param pathArray     * @return     */    public static String stringArrayToString(String[] stringArray, String delim)    {        String ret = "";        for (int i = 0; i < stringArray.length; i++)        {            if (ret.length() > 0)                ret = ret + delim + stringArray[i];            else                ret = stringArray[i];        }        return ret;    }        //------------------------------------------------------------------------    /**     * Replace occurrences of str1 in string str with str2      */    public static String stringReplace(String str, String str1, String str2)    {        String ret = StringUtils.replace(str,str1,str2);        return ret;    }    //------------------------------------------------------------------------    /**     * Replace occurrences of str1 in string str with str2     * @param str String to operate on      * @param str1 String to be replaced     * @param str2 String to be used as replacement     * @param maxCount Number of times to replace, 0 for all     */    public static String stringReplace(        String str,        String str1,        String str2,        int maxCount)    {        String ret = StringUtils.replace(str,str1,str2,maxCount);        return ret;    }    //--------------------------------------------------------------------------    /** Convert string to string array. */    public static String[] stringToStringArray(String instr, String delim)        throws NoSuchElementException, NumberFormatException    {        StringTokenizer toker = new StringTokenizer(instr, delim);        String stringArray[] = new String[toker.countTokens()];        int i = 0;        while (toker.hasMoreTokens())        {            stringArray[i++] = toker.nextToken();        }        return stringArray;    }    //--------------------------------------------------------------------------    /** Convert string to integer array. */    public static int[] stringToIntArray(String instr, String delim)        throws NoSuchElementException, NumberFormatException    {        StringTokenizer toker = new StringTokenizer(instr, delim);        int intArray[] = new int[toker.countTokens()];        int i = 0;        while (toker.hasMoreTokens())        {            String sInt = toker.nextToken();            int nInt = Integer.parseInt(sInt);            intArray[i++] = new Integer(nInt).intValue();        }        return intArray;    }    //-------------------------------------------------------------------    /** Convert integer array to a string. */    public static String intArrayToString(int[] intArray)    {        String ret = "";        for (int i = 0; i < intArray.length; i++)        {            if (ret.length() > 0)                ret = ret + "," + Integer.toString(intArray[i]);            else                ret = Integer.toString(intArray[i]);        }        return ret;    }    //------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -