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

📄 textutil.java

📁 wiki建站资源 java编写的 很好用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*     JSPWiki - a JSP-based WikiWiki clone.    Copyright (C) 2001-2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU Lesser General Public License as published by    the Free Software Foundation; either version 2.1 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU Lesser General Public License for more details.    You should have received a copy of the GNU Lesser 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 com.ecyrd.jspwiki;import java.io.UnsupportedEncodingException;import java.util.Properties;/** *  Contains a number of static utility methods. */public class TextUtil{    static final String   HEX_DIGITS = "0123456789ABCDEF";    /**     *  java.net.URLEncoder.encode() method in JDK < 1.4 is buggy.  This duplicates     *  its functionality.     */    protected static String urlEncode( byte[] rs )    {        StringBuffer result = new StringBuffer();        // Does the URLEncoding.  We could use the java.net one, but        // it does not eat byte[]s.        for( int i = 0; i < rs.length; i++ )        {            char c = (char) rs[i];            switch( c )            {              case '_':              case '.':              case '*':              case '-':              case '/':                result.append( c );                break;              case ' ':                result.append( '+' );                break;              default:                if( (c >= 'a' && c <= 'z') ||                    (c >= 'A' && c <= 'Z') ||                    (c >= '0' && c <= '9') )                {                                        result.append( c );                }                else                {                    result.append( '%' );                    result.append( HEX_DIGITS.charAt( (c & 0xF0) >> 4 ) );                    result.append( HEX_DIGITS.charAt( c & 0x0F ) );                }            }        } // for        return result.toString();    }    /**     *  URL encoder does not handle all characters correctly.     *  See <A HREF="http://developer.java.sun.com/developer/bugParade/bugs/4257115.html">     *  Bug parade, bug #4257115</A> for more information.     *  <P>     *  Thanks to CJB for this fix.     */     protected static String urlDecode( byte[] bytes, String encoding )        throws UnsupportedEncodingException,               IllegalArgumentException    {        if(bytes == null)         {            return null;        }        byte[] decodeBytes   = new byte[bytes.length];        int decodedByteCount = 0;        try         {            for( int count = 0; count < bytes.length; count++ )             {                switch( bytes[count] )                 {                  case '+':                    decodeBytes[decodedByteCount++] = (byte) ' ';                    break ;                  case '%':                    decodeBytes[decodedByteCount++] = (byte)((HEX_DIGITS.indexOf(bytes[++count]) << 4) +                                                             (HEX_DIGITS.indexOf(bytes[++count])) );                    break ;                  default:                    decodeBytes[decodedByteCount++] = bytes[count] ;                }            }        }        catch (IndexOutOfBoundsException ae)         {            throw new IllegalArgumentException( "Malformed UTF-8 string?" );        }        String processedPageName = null ;        try         {            processedPageName = new String(decodeBytes, 0, decodedByteCount, encoding) ;        }         catch (UnsupportedEncodingException e)         {            throw new UnsupportedEncodingException( "UTF-8 encoding not supported on this platform" );        }        return(processedPageName.toString());    }    /**     *  As java.net.URLEncoder class, but this does it in UTF8 character set.     */    public static String urlEncodeUTF8( String text )    {        byte[] rs = {};        try        {            rs = text.getBytes("UTF-8");            return urlEncode( rs );        }        catch( UnsupportedEncodingException e )        {            throw new InternalWikiException("UTF-8 not supported!?!");        }    }    /**     *  As java.net.URLDecoder class, but for UTF-8 strings.     */    public static String urlDecodeUTF8( String utf8 )    {        String rs = null;        try        {            rs = urlDecode( utf8.getBytes("ISO-8859-1"), "UTF-8" );        }        catch( UnsupportedEncodingException e )        {            throw new InternalWikiException("UTF-8 or ISO-8859-1 not supported!?!");        }        return rs;    }    /**     * Provides encoded version of string depending on encoding.     * Encoding may be UTF-8 or ISO-8859-1 (default).     *     * <p>This implementation is the same as in      * FileSystemProvider.mangleName().     */    public static String urlEncode( String data, String encoding )    {        // Presumably, the same caveats apply as in FileSystemProvider.        // Don't see why it would be horribly kludgy, though.         if( "UTF-8".equals( encoding ) )            return( TextUtil.urlEncodeUTF8( data ) );        else        {            try            {                return( TextUtil.urlEncode( data.getBytes(encoding) ) );            }            catch (UnsupportedEncodingException uee)            {                throw new InternalWikiException("Could not encode String into" + encoding);            }        }    }    /**     * Provides decoded version of string depending on encoding.     * Encoding may be UTF-8 or ISO-8859-1 (default).     *     * <p>This implementation is the same as in      * FileSystemProvider.unmangleName().     */    public static String urlDecode( String data, String encoding )        throws UnsupportedEncodingException,               IllegalArgumentException    {        // Presumably, the same caveats apply as in FileSystemProvider.        // Don't see why it would be horribly kludgy, though.         if( "UTF-8".equals( encoding ) )            return( TextUtil.urlDecodeUTF8( data ) );        else        {            try            {                return( TextUtil.urlDecode( data.getBytes(encoding), encoding ) );            }            catch (UnsupportedEncodingException uee)            {                throw new InternalWikiException("Could not decode String into" + encoding);            }        }    }    /**     *  Replaces the relevant entities inside the String.     *  All &amp; &gt;, &lt;, and &quot; are replaced by their     *  respective names.     *     *  @since 1.6.1     */    public static String replaceEntities( String src )    {        src = replaceString( src, "&", "&amp;" );        src = replaceString( src, "<", "&lt;" );        src = replaceString( src, ">", "&gt;" );        src = replaceString( src, "\"", "&quot;" );        return src;    }    /**     *  Replaces a string with an other string.     *     *  @param orig Original string.  Null is safe.     *  @param src  The string to find.     *  @param dest The string to replace <I>src</I> with.     */    public static String replaceString( String orig, String src, String dest )    {        if( orig == null ) return null;        StringBuffer res = new StringBuffer();        int start, end = 0, last = 0;        while( (start = orig.indexOf(src,end)) != -1 )        {            res.append( orig.substring( last, start ) );            res.append( dest );            end  = start+src.length();            last = start+src.length();        }        res.append( orig.substring( end ) );        return res.toString();    }    /**     *  Replaces a part of a string with a new String.     *     *  @param start Where in the original string the replacing should start.     *  @param end Where the replacing should end.     *  @param orig Original string.  Null is safe.     *  @param text The new text to insert into the string.     */    public static String replaceString( String orig, int start, int end, String text )    {        if( orig == null ) return null;        StringBuffer buf = new StringBuffer(orig);        buf.replace( start, end, text );        return buf.toString();    }    /**     *  Parses an integer parameter, returning a default value     *  if the value is null or a non-number.     */    public static int parseIntParameter( String value, int defvalue )    {        int val = defvalue;                try        {            val = Integer.parseInt( value );        }        catch( Exception e ) {}                    return val;    }    /**     *  Gets an integer-valued property from a standard Properties     *  list.  If the value does not exist, or is a non-integer, returns defVal.     *     *  @since 2.1.48.     */    public static int getIntegerProperty( Properties props,                                           String key,                                          int defVal )    {        String val = props.getProperty( key );        return parseIntParameter( val, defVal );    }    /**     *  Gets a boolean property from a standard Properties list.     *  Returns the default value, in case the key has not been set.     *  <P>     *  The possible values for the property are "true"/"false", "yes"/"no", or     *  "on"/"off".  Any value not recognized is always defined as "false".     *     *  @param props   A list of properties to search.     *  @param key     The property key.     *  @param defval  The default value to return.     *     *  @return True, if the property "key" was set to "true", "on", or "yes".     *     *  @since 2.0.11     */    public static boolean getBooleanProperty( Properties props,                                               String key,                                               boolean defval )

⌨️ 快捷键说明

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