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

📄 textutil.java

📁 JSPWiki,100%Java开发的一套完整WIKI程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */    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 )    {        String val = props.getProperty( key );        if( val == null ) return defval;        return isPositive( val );    }    /**     *  Returns true, if the string "val" denotes a positive string.  Allowed     *  values are "yes", "on", and "true".  Comparison is case-insignificant.     *  Null values are safe.     *     *  @param val Value to check.     *  @return True, if val is "true", "on", or "yes"; otherwise false.     *     *  @since 2.0.26     */    public static boolean isPositive( String val )    {        if( val == null ) return false;        return ( val.equalsIgnoreCase("true") || val.equalsIgnoreCase("on") ||                 val.equalsIgnoreCase("yes") );    }    /**     *  Makes sure that the POSTed data is conforms to certain rules.  These     *  rules are:     *  <UL>     *  <LI>The data always ends with a newline (some browsers, such     *      as NS4.x series, does not send a newline at the end, which makes     *      the diffs a bit strange sometimes.     *  <LI>The CR/LF/CRLF mess is normalized to plain CRLF.     *  </UL>     *     *  The reason why we're using CRLF is that most browser already     *  return CRLF since that is the closest thing to a HTTP standard.     */    public static String normalizePostData( String postData )    {        StringBuffer sb = new StringBuffer();        for( int i = 0; i < postData.length(); i++ )        {            switch( postData.charAt(i) )            {              case 0x0a: // LF, UNIX                sb.append( "\r\n" );                break;              case 0x0d: // CR, either Mac or MSDOS                sb.append( "\r\n" );                // If it's MSDOS, skip the LF so that we don't add it again.                if( i < postData.length()-1 && postData.charAt(i+1) == 0x0a )                {                    i++;                }                break;              default:                sb.append( postData.charAt(i) );                break;            }        }        if( sb.length() < 2 || !sb.substring( sb.length()-2 ).equals("\r\n") )        {            sb.append( "\r\n" );        }        return sb.toString();    }    private static final int EOI   = 0;    private static final int LOWER = 1;    private static final int UPPER = 2;    private static final int DIGIT = 3;    private static final int OTHER = 4;    private static int getCharKind(int c)    {        if (c==-1)        {            return EOI;        }        char ch = (char) c;        if (Character.isLowerCase(ch))            return LOWER;        else if (Character.isUpperCase(ch))            return UPPER;        else if (Character.isDigit(ch))            return DIGIT;        else            return OTHER;    }    /**     *  Adds spaces in suitable locations of the input string.  This is     *  used to transform a WikiName into a more readable format.     *     *  @param s String to be beautified.     *  @return A beautified string.     */    public static String beautifyString( String s )    {        StringBuffer result = new StringBuffer();        if( s == null || s.length() == 0 ) return "";        int cur     = s.charAt(0);        int curKind = getCharKind(cur);        int prevKind = LOWER;        int nextKind = -1;        int next = -1;        int nextPos = 1;        while( curKind != EOI )        {            next = (nextPos < s.length()) ? s.charAt(nextPos++) : -1;            nextKind = getCharKind( next );            if( (prevKind == UPPER) && (curKind == UPPER) && (nextKind == LOWER) )            {                result.append(' ');                result.append((char) cur);            }            else            {                result.append((char) cur);                if( ( (curKind == UPPER) && (nextKind == DIGIT) )                    || ( (curKind == LOWER) && ((nextKind == DIGIT) || (nextKind == UPPER)) )                    || ( (curKind == DIGIT) && ((nextKind == UPPER) || (nextKind == LOWER)) ))                {                    result.append(' ');                }            }            prevKind = curKind;            cur      = next;            curKind  = nextKind;        }        return result.toString();    }    /**     *  Creates a Property object based on an array which contains alternatively     *  a key and a value.  It is useful for generating default mappings.     *  For example:     *  <pre>     *     String[] properties = { "jspwiki.property1", "value1",     *                             "jspwiki.property2", "value2 };     *     *     Properties props = TextUtil.createPropertes( values );     *     *     System.out.println( props.getProperty("jspwiki.property1") );     *  </pre>     *  would output "value1".     *     *  @param values Alternating key and value pairs.     *  @return Property object     *  @see java.util.Property     *  @throws IllegalArgumentException, if the property array is missing     *          a value for a key.     *  @since 2.2.     */    public static Properties createProperties( String[] values )        throws IllegalArgumentException    {        if( values.length % 2 != 0 )            throw new IllegalArgumentException( "One value is missing.");        Properties props = new Properties();        for( int i = 0; i < values.length; i += 2 )        {            props.setProperty( values[i], values[i+1] );        }        return props;    }    /**     *  Counts the number of sections (separated with "----") from the page.     *     *  @param pagedata The WikiText to parse.     *  @return int Number of counted sections.     *  @since 2.1.86.     */    public static int countSections( String pagedata )    {        int tags  = 0;        int start = 0;        while( (start = pagedata.indexOf("----",start)) != -1 )        {            tags++;            start+=4; // Skip this "----"        }        //        // The first section does not get the "----"        //        return pagedata.length() > 0 ? tags+1 : 0;    }    /**     *  Gets the given section (separated with "----") from the page text.     *  Note that the first section is always #1.  If a page has no section markers,     *  them there is only a single section, #1.     *     *  @param pagedata WikiText to parse.     *  @param section  Which section to get.     *  @return String  The section.     *  @throws IllegalArgumentException If the page does not contain this many sections.     *  @since 2.1.86.     */    public static String getSection( String pagedata, int section )        throws IllegalArgumentException    {        int tags  = 0;        int start = 0;        int previous = 0;        while( (start = pagedata.indexOf("----",start)) != -1 )        {            if( ++tags == section )            {                return pagedata.substring( previous, start );            }            start += 4; // Skip this "----"            previous = start;        }        if( ++tags == section )        {            return pagedata.substring( previous );        }        throw new IllegalArgumentException("There is no section no. "+section+" on the page.");    }    /**     *  A simple routine which just repeates the arguments.  This is useful     *  for creating something like a line or something.     *     *  @param what String to repeat     *  @param times How many times to repeat the string.     *  @return Guess what?     *  @since 2.1.98.     */    public static String repeatString( String what, int times )    {        StringBuffer sb = new StringBuffer();        for( int i = 0; i < times; i++ )        {            sb.append( what );        }        return sb.toString();    }}

⌨️ 快捷键说明

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