Clover coverage report - JFox Service-Oriented Application Framework - 1.0-RC3
Coverage timestamp: 星期三 二月 15 2006 18:10:22 CST
file stats: LOC: 342   Methods: 29
NCLOC: 255   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
StringHelper.java 0% 0% 0% 0%
coverage
 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.2 $ $Date: 2006/02/15 08:45:46 $
 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  0
     public static String join(String seperator, String[] strings) {
 56  0
         int length = strings.length;
 57  0
         if (length == 0) {
 58  0
             return EMPTY_STRING;
 59   
         }
 60  0
         StringBuffer buf = new StringBuffer(length * strings[0].length())
 61   
                 .append(strings[0]);
 62  0
         for (int i = 1; i < length; i++) {
 63  0
             buf.append(seperator).append(strings[i]);
 64   
         }
 65  0
         return buf.toString();
 66   
     }
 67   
 
 68  0
     public static String join(String seperator, Iterator objects) {
 69  0
         StringBuffer buf = new StringBuffer();
 70  0
         if (objects.hasNext()) {
 71  0
             buf.append(objects.next());
 72   
         }
 73  0
         while (objects.hasNext()) {
 74  0
             buf.append(seperator).append(objects.next());
 75   
         }
 76  0
         return buf.toString();
 77   
     }
 78   
 
 79  0
     public static String[] add(String[] x, String sep, String[] y) {
 80  0
         String[] result = new String[x.length];
 81  0
         for (int i = 0; i < x.length; i++) {
 82  0
             result[i] = x[i] + sep + y[i];
 83   
         }
 84  0
         return result;
 85   
     }
 86   
 
 87  0
     public static String repeat(String string, int times) {
 88  0
         StringBuffer buf = new StringBuffer(string.length() * times);
 89  0
         for (int i = 0; i < times; i++) {
 90  0
             buf.append(string);
 91   
         }
 92  0
         return buf.toString();
 93   
     }
 94   
 
 95  0
     public static String replace(String template, String placeholder,
 96   
             String replacement) {
 97  0
         return replace(template, placeholder, replacement, false);
 98   
     }
 99   
 
 100  0
     public static String replace(String template, String placeholder,
 101   
             String replacement, boolean wholeWords) {
 102  0
         int loc = template.indexOf(placeholder);
 103  0
         if (loc < 0) {
 104  0
             return template;
 105   
         } else {
 106  0
             final boolean actuallyReplace = !wholeWords
 107   
                     || loc + placeholder.length() == template.length()
 108   
                     || !Character.isJavaIdentifierPart(template.charAt(loc
 109   
                             + placeholder.length()));
 110  0
             String actualReplacement = actuallyReplace ? replacement
 111   
                     : placeholder;
 112  0
             return new StringBuffer(template.substring(0, loc)).append(
 113   
                     actualReplacement).append(
 114   
                     replace(template.substring(loc + placeholder.length()),
 115   
                             placeholder, replacement, wholeWords)).toString();
 116   
         }
 117   
     }
 118   
 
 119  0
     public static String replaceOnce(String template, String placeholder,
 120   
             String replacement) {
 121  0
         int loc = template.indexOf(placeholder);
 122  0
         if (loc < 0) {
 123  0
             return template;
 124   
         } else {
 125  0
             return new StringBuffer(template.substring(0, loc)).append(
 126   
                     replacement).append(
 127   
                     template.substring(loc + placeholder.length())).toString();
 128   
         }
 129   
     }
 130   
 
 131  0
     public static String[] split(String seperators, String list) {
 132  0
         return split(seperators, list, false);
 133   
     }
 134   
 
 135  0
     public static String[] split(String seperators, String list, boolean include) {
 136  0
         StringTokenizer tokens = new StringTokenizer(list, seperators, include);
 137  0
         String[] result = new String[tokens.countTokens()];
 138  0
         int i = 0;
 139  0
         while (tokens.hasMoreTokens()) {
 140  0
             result[i++] = tokens.nextToken();
 141   
         }
 142  0
         return result;
 143   
     }
 144   
 
 145  0
     public static String unqualify(String qualifiedName) {
 146  0
         return unqualify(qualifiedName, ".");
 147   
     }
 148   
 
 149  0
     public static String unqualify(String qualifiedName, String seperator) {
 150  0
         return qualifiedName
 151   
                 .substring(qualifiedName.lastIndexOf(seperator) + 1);
 152   
     }
 153   
 
 154  0
     public static String qualifier(String qualifiedName) {
 155  0
         int loc = qualifiedName.lastIndexOf(".");
 156  0
         if (loc < 0) {
 157  0
             return EMPTY_STRING;
 158   
         } else {
 159  0
             return qualifiedName.substring(0, loc);
 160   
         }
 161   
     }
 162   
 
 163  0
     public static String[] suffix(String[] columns, String theSuffix) {
 164  0
         if (theSuffix == null) {
 165  0
             return columns;
 166   
         }
 167  0
         String[] qualified = new String[columns.length];
 168  0
         for (int i = 0; i < columns.length; i++) {
 169  0
             qualified[i] = suffix(columns[i], theSuffix);
 170   
         }
 171  0
         return qualified;
 172   
     }
 173   
 
 174  0
     public static String suffix(String name, String theSuffix) {
 175  0
         return (theSuffix == null) ? name : name + theSuffix;
 176   
     }
 177   
 
 178  0
     public static String[] prefix(String[] columns, String thePrefix) {
 179  0
         if (thePrefix == null) {
 180  0
             return columns;
 181   
         }
 182  0
         String[] qualified = new String[columns.length];
 183  0
         for (int i = 0; i < columns.length; i++) {
 184  0
             qualified[i] = thePrefix + columns[i];
 185   
         }
 186  0
         return qualified;
 187   
     }
 188   
 
 189  0
     public static String root(String qualifiedName) {
 190  0
         int loc = qualifiedName.indexOf(".");
 191  0
         return (loc < 0) ? qualifiedName : qualifiedName.substring(0, loc);
 192   
     }
 193   
 
 194  0
     public static boolean booleanValue(String tfString) {
 195  0
         String trimmed = tfString.trim().toLowerCase();
 196  0
         return trimmed.equals("true") || trimmed.equals("t");
 197   
     }
 198   
 
 199  0
     public static String toString(Object[] array) {
 200  0
         int len = array.length;
 201  0
         if (len == 0) {
 202  0
             return StringHelper.EMPTY_STRING;
 203   
         }
 204  0
         StringBuffer buf = new StringBuffer(len * 12);
 205  0
         for (int i = 0; i < len - 1; i++) {
 206  0
             buf.append(array[i]).append(StringHelper.COMMA_SPACE);
 207   
         }
 208  0
         return buf.append(array[len - 1]).toString();
 209   
     }
 210   
 
 211  0
     public static String[] multiply(String string, Iterator placeholders,
 212   
             Iterator replacements) {
 213  0
         String[] result = new String[] { string };
 214  0
         while (placeholders.hasNext()) {
 215  0
             result = multiply(result, (String) placeholders.next(),
 216   
                     (String[]) replacements.next());
 217   
         }
 218  0
         return result;
 219   
     }
 220   
 
 221  0
     private static String[] multiply(String[] strings, String placeholder,
 222   
             String[] replacements) {
 223  0
         String[] results = new String[replacements.length * strings.length];
 224  0
         int n = 0;
 225  0
         for (int i = 0; i < replacements.length; i++) {
 226  0
             for (int j = 0; j < strings.length; j++) {
 227  0
                 results[n++] = replaceOnce(strings[j], placeholder,
 228   
                         replacements[i]);
 229   
             }
 230   
         }
 231  0
         return results;
 232   
     }
 233   
 
 234   
     /*
 235   
      * public static String unQuote(String name) { return (
 236   
      * Dialect.QUOTE.indexOf( name.charAt(0) ) > -1 ) ? name.substring(1,
 237   
      * name.length()-1) : name; } public static void unQuoteInPlace(String[]
 238   
      * names) { for ( int i=0; i <names.length; i++ ) names[i] = unQuote(
 239   
      * names[i] ); } public static String[] unQuote(String[] names) { String[]
 240   
      * unquoted = new String[ names.length ]; for ( int i=0; i <names.length;
 241   
      * i++ ) unquoted[i] = unQuote( names[i] ); return unquoted; }
 242   
      */
 243   
 
 244  0
     public static int count(String string, char character) {
 245  0
         int n = 0;
 246  0
         for (int i = 0; i < string.length(); i++) {
 247  0
             if (string.charAt(i) == character) {
 248  0
                 n++;
 249   
             }
 250   
         }
 251  0
         return n;
 252   
     }
 253   
 
 254  0
     public static int countUnquoted(String string, char character) {
 255  0
         if (SINGLE_QUOTE == character) {
 256  0
             throw new IllegalArgumentException(
 257   
                     "Unquoted count of quotes is invalid");
 258   
         }
 259   
         // Impl note: takes advantage of the fact that an escpaed single quote
 260   
         // embedded within a quote-block can really be handled as two seperate
 261   
         // quote-blocks for the purposes of this method...
 262  0
         int countNum = 0;
 263  0
         int stringLength = string == null ? 0 : string.length();
 264  0
         boolean inQuote = false;
 265  0
         for (int indx = 0; indx < stringLength; indx++) {
 266  0
             if (inQuote) {
 267  0
                 if (SINGLE_QUOTE == string.charAt(indx)) {
 268  0
                     inQuote = false;
 269   
                 }
 270  0
             } else if (SINGLE_QUOTE == string.charAt(indx)) {
 271  0
                 inQuote = true;
 272  0
             } else if (string.charAt(indx) == character) {
 273  0
                 countNum ++;
 274   
             }
 275   
         }
 276  0
         return countNum;
 277   
     }
 278   
 
 279  0
     public static boolean isNotEmpty(String string) {
 280  0
         return string != null && string.length() > 0;
 281   
     }
 282   
 
 283  0
     public static String qualify(String thePrefix, String name) {
 284  0
         char first = name.charAt(0);
 285  0
         if (first == SINGLE_QUOTE || // a SQLstring literal
 286   
                 Character.isDigit(first) // a SQL numeric literal
 287   
 
 288   
         ) {
 289  0
             return name;
 290   
         } else {
 291  0
             return new StringBuffer(thePrefix.length() + name.length() + 1)
 292   
                     .append(thePrefix).append(DOT).append(name).toString();
 293   
         }
 294   
     }
 295   
 
 296  0
     public static String[] qualify(String thePrefix, String[] names) {
 297  0
         if (thePrefix == null) {
 298  0
             return names;
 299   
         }
 300  0
         int len = names.length;
 301  0
         String[] qualified = new String[len];
 302  0
         for (int i = 0; i < len; i++) {
 303  0
             qualified[i] = qualify(thePrefix, names[i]);
 304   
         }
 305  0
         return qualified;
 306   
     }
 307   
 
 308  0
     public static int firstIndexOfChar(String sqlString, String string,
 309   
             int startindex) {
 310  0
         int matchAt = -1;
 311  0
         for (int i = 0; i < string.length(); i++) {
 312  0
             int curMatch = sqlString.indexOf(string.charAt(i), startindex);
 313  0
             if (curMatch >= 0) {
 314  0
                 if (matchAt == -1) { // first time we find match!
 315  0
                     matchAt = curMatch;
 316   
                 } else {
 317  0
                     matchAt = Math.min(matchAt, curMatch);
 318   
                 }
 319   
             }
 320   
         }
 321  0
         return matchAt;
 322   
     }
 323   
 
 324  0
     public static String truncate(String string, int length) {
 325  0
         if (string.length() <= length) {
 326  0
             return string;
 327   
         } else {
 328  0
             return string.substring(0, length);
 329   
         }
 330   
     }
 331   
 
 332  0
     public static String toUpperCase(String str) {
 333  0
         return str == null ? null : str.toUpperCase();
 334   
     }
 335   
 
 336  0
     public static String toLowerCase(String str) {
 337  0
         return str == null ? null : str.toLowerCase();
 338   
     }
 339   
 
 340   
 }
 341   
 
 342