View Javadoc

    1   /***
    2    * @(#)StringHelper.java
    3    *
    4    * JFoxSOAF, Service-Oriented Application Framework
    5    *
    6    * Copyright(c) JFoxSOAF Team
    7    *
    8    * Licensed under the GNU LGPL, Version 2.1 (the "License");
    9    * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   * http://www.gnu.org/copyleft/lesser.html
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   *
    20   * For more information, please visit:
    21   * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
    22   * http://www.huihoo.org/jfox/jfoxsoaf
    23   */
    24
    25  package org.huihoo.jfox.soaf.util.resource;
    26
    27  import java.util.Iterator;
    28  import java.util.StringTokenizer;
    29
    30  /***
    31   * String helper.
    32   *
    33   * @version $Revision: 1.1 $ $Date: 2005/05/22 06:50:55 $
    34   * @version Revision: 1.0
    35   */
    36
    37  public final class StringHelper {
    38
    39      public static final String EMPTY_STRING = "";
    40
    41      public static final char DOT = '.';
    42
    43      public static final char UNDERSCORE = '_';
    44
    45      public static final String COMMA_SPACE = ", ";
    46
    47      public static final String COMMA = ",";
    48
    49      public static final String OPEN_PAREN = "(";
    50
    51      public static final String CLOSE_PAREN = ")";
    52
    53      public static final char SINGLE_QUOTE = '\'';
    54
    55      public static String join(String seperator, String[] strings) {
    56          int length = strings.length;
    57          if (length == 0)
    58              return EMPTY_STRING;
    59          StringBuffer buf = new StringBuffer(length * strings[0].length())
    60                  .append(strings[0]);
    61          for (int i = 1; i < length; i++) {
    62              buf.append(seperator).append(strings[i]);
    63          }
    64          return buf.toString();
    65      }
    66
    67      public static String join(String seperator, Iterator objects) {
    68          StringBuffer buf = new StringBuffer();
    69          if (objects.hasNext())
    70              buf.append(objects.next());
    71          while (objects.hasNext()) {
    72              buf.append(seperator).append(objects.next());
    73          }
    74          return buf.toString();
    75      }
    76
    77      public static String[] add(String[] x, String sep, String[] y) {
    78          String[] result = new String[x.length];
    79          for (int i = 0; i < x.length; i++) {
    80              result[i] = x[i] + sep + y[i];
    81          }
    82          return result;
    83      }
    84
    85      public static String repeat(String string, int times) {
    86          StringBuffer buf = new StringBuffer(string.length() * times);
    87          for (int i = 0; i < times; i++)
    88              buf.append(string);
    89          return buf.toString();
    90      }
    91
    92      public static String replace(String template, String placeholder,
    93              String replacement) {
    94          return replace(template, placeholder, replacement, false);
    95      }
    96
    97      public static String replace(String template, String placeholder,
    98              String replacement, boolean wholeWords) {
    99          int loc = template.indexOf(placeholder);
    100         if (loc < 0) {
    101             return template;
    102         } else {
    103             final boolean actuallyReplace = !wholeWords
    104                     || loc + placeholder.length() == template.length()
    105                     || !Character.isJavaIdentifierPart(template.charAt(loc
    106                             + placeholder.length()));
    107             String actualReplacement = actuallyReplace ? replacement
    108                     : placeholder;
    109             return new StringBuffer(template.substring(0, loc)).append(
    110                     actualReplacement).append(
    111                     replace(template.substring(loc + placeholder.length()),
    112                             placeholder, replacement, wholeWords)).toString();
    113         }
    114     }
    115
    116     public static String replaceOnce(String template, String placeholder,
    117             String replacement) {
    118         int loc = template.indexOf(placeholder);
    119         if (loc < 0) {
    120             return template;
    121         } else {
    122             return new StringBuffer(template.substring(0, loc)).append(
    123                     replacement).append(
    124                     template.substring(loc + placeholder.length())).toString();
    125         }
    126     }
    127
    128     public static String[] split(String seperators, String list) {
    129         return split(seperators, list, false);
    130     }
    131
    132     public static String[] split(String seperators, String list, boolean include) {
    133         StringTokenizer tokens = new StringTokenizer(list, seperators, include);
    134         String[] result = new String[tokens.countTokens()];
    135         int i = 0;
    136         while (tokens.hasMoreTokens()) {
    137             result[i++] = tokens.nextToken();
    138         }
    139         return result;
    140     }
    141
    142     public static String unqualify(String qualifiedName) {
    143         return unqualify(qualifiedName, ".");
    144     }
    145
    146     public static String unqualify(String qualifiedName, String seperator) {
    147         return qualifiedName
    148                 .substring(qualifiedName.lastIndexOf(seperator) + 1);
    149     }
    150
    151     public static String qualifier(String qualifiedName) {
    152         int loc = qualifiedName.lastIndexOf(".");
    153         if (loc < 0) {
    154             return EMPTY_STRING;
    155         } else {
    156             return qualifiedName.substring(0, loc);
    157         }
    158     }
    159
    160     public static String[] suffix(String[] columns, String suffix) {
    161         if (suffix == null)
    162             return columns;
    163         String[] qualified = new String[columns.length];
    164         for (int i = 0; i < columns.length; i++) {
    165             qualified[i] = suffix(columns[i], suffix);
    166         }
    167         return qualified;
    168     }
    169
    170     public static String suffix(String name, String suffix) {
    171         return (suffix == null) ? name : name + suffix;
    172     }
    173
    174     public static String[] prefix(String[] columns, String prefix) {
    175         if (prefix == null)
    176             return columns;
    177         String[] qualified = new String[columns.length];
    178         for (int i = 0; i < columns.length; i++) {
    179             qualified[i] = prefix + columns[i];
    180         }
    181         return qualified;
    182     }
    183
    184     public static String root(String qualifiedName) {
    185         int loc = qualifiedName.indexOf(".");
    186         return (loc < 0) ? qualifiedName : qualifiedName.substring(0, loc);
    187     }
    188
    189     public static boolean booleanValue(String tfString) {
    190         String trimmed = tfString.trim().toLowerCase();
    191         return trimmed.equals("true") || trimmed.equals("t");
    192     }
    193
    194     public static String toString(Object[] array) {
    195         int len = array.length;
    196         if (len == 0)
    197             return StringHelper.EMPTY_STRING;
    198         StringBuffer buf = new StringBuffer(len * 12);
    199         for (int i = 0; i < len - 1; i++) {
    200             buf.append(array[i]).append(StringHelper.COMMA_SPACE);
    201         }
    202         return buf.append(array[len - 1]).toString();
    203     }
    204
    205     public static String[] multiply(String string, Iterator placeholders,
    206             Iterator replacements) {
    207         String[] result = new String[] { string };
    208         while (placeholders.hasNext()) {
    209             result = multiply(result, (String) placeholders.next(),
    210                     (String[]) replacements.next());
    211         }
    212         return result;
    213     }
    214
    215     private static String[] multiply(String[] strings, String placeholder,
    216             String[] replacements) {
    217         String[] results = new String[replacements.length * strings.length];
    218         int n = 0;
    219         for (int i = 0; i < replacements.length; i++) {
    220             for (int j = 0; j < strings.length; j++) {
    221                 results[n++] = replaceOnce(strings[j], placeholder,
    222                         replacements[i]);
    223             }
    224         }
    225         return results;
    226     }
    227
    228     /*
    229      * public static String unQuote(String name) { return (
    230      * Dialect.QUOTE.indexOf( name.charAt(0) ) > -1 ) ? name.substring(1,
    231      * name.length()-1) : name; } public static void unQuoteInPlace(String[]
    232      * names) { for ( int i=0; i <names.length; i++ ) names[i] = unQuote(
    233      * names[i] ); } public static String[] unQuote(String[] names) { String[]
    234      * unquoted = new String[ names.length ]; for ( int i=0; i <names.length;
    235      * i++ ) unquoted[i] = unQuote( names[i] ); return unquoted; }
    236      */
    237
    238     public static int count(String string, char character) {
    239         int n = 0;
    240         for (int i = 0; i < string.length(); i++) {
    241             if (string.charAt(i) == character)
    242                 n++;
    243         }
    244         return n;
    245     }
    246
    247     public static int countUnquoted(String string, char character) {
    248         if (SINGLE_QUOTE == character) {
    249             throw new IllegalArgumentException(
    250                     "Unquoted count of quotes is invalid");
    251         }
    252         // Impl note: takes advantage of the fact that an escpaed single quote
    253         // embedded within a quote-block can really be handled as two seperate
    254         // quote-blocks for the purposes of this method...
    255         int count = 0;
    256         int stringLength = string == null ? 0 : string.length();
    257         boolean inQuote = false;
    258         for (int indx = 0; indx < stringLength; indx++) {
    259             if (inQuote) {
    260                 if (SINGLE_QUOTE == string.charAt(indx)) {
    261                     inQuote = false;
    262                 }
    263             } else if (SINGLE_QUOTE == string.charAt(indx)) {
    264                 inQuote = true;
    265             } else if (string.charAt(indx) == character) {
    266                 count++;
    267             }
    268         }
    269         return count;
    270     }
    271
    272     public static boolean isNotEmpty(String string) {
    273         return string != null && string.length() > 0;
    274     }
    275
    276     public static String qualify(String prefix, String name) {
    277         char first = name.charAt(0);
    278         if (first == SINGLE_QUOTE || // a SQLstring literal
    279                 Character.isDigit(first) // a SQL numeric literal
    280
    281         ) {
    282             return name;
    283         } else {
    284             return new StringBuffer(prefix.length() + name.length() + 1)
    285                     .append(prefix).append(DOT).append(name).toString();
    286         }
    287     }
    288
    289     public static String[] qualify(String prefix, String[] names) {
    290         if (prefix == null)
    291             return names;
    292         int len = names.length;
    293         String[] qualified = new String[len];
    294         for (int i = 0; i < len; i++) {
    295             qualified[i] = qualify(prefix, names[i]);
    296         }
    297         return qualified;
    298     }
    299
    300     private StringHelper() { /* static methods only - hide constructor */
    301     }
    302
    303     public static int firstIndexOfChar(String sqlString, String string,
    304             int startindex) {
    305         int matchAt = -1;
    306         for (int i = 0; i < string.length(); i++) {
    307             int curMatch = sqlString.indexOf(string.charAt(i), startindex);
    308             if (curMatch >= 0) {
    309                 if (matchAt == -1) { // first time we find match!
    310                     matchAt = curMatch;
    311                 } else {
    312                     matchAt = Math.min(matchAt, curMatch);
    313                 }
    314             }
    315         }
    316         return matchAt;
    317     }
    318
    319     public static String truncate(String string, int length) {
    320         if (string.length() <= length) {
    321             return string;
    322         } else {
    323             return string.substring(0, length);
    324         }
    325     }
    326
    327     public static String toUpperCase(String str) {
    328         return str == null ? null : str.toUpperCase();
    329     }
    330
    331     public static String toLowerCase(String str) {
    332         return str == null ? null : str.toLowerCase();
    333     }
    334
    335 }
    336