Clover coverage report - JBind Project
Coverage timestamp: Fr Mai 28 2004 11:17:36 CEST
file stats: LOC: 231   Methods: 13
NCLOC: 157   Classes: 1
This license of Clover is provided to support the development of JBind only. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
StringUtil.java 56,8% 69,1% 61,5% 64,9%
 1   
 /*
 2   
  * JBind
 3   
  *
 4   
  * Copyright (c) by Stefan Wachter. All rights reserved.
 5   
  *
 6   
  * Usage, modification, and redistribution is subject to license terms that are
 7   
  * available at 'http://www.jbind.org'. The JBind license is like the
 8   
  * 'Apache Software License V 1.1'.
 9   
  */
 10   
 package org.jbind.xml.base;
 11   
 
 12   
 import java.util.Enumeration;
 13   
 import java.util.StringTokenizer;
 14   
 
 15   
 import org.jbind.util.other.StrBuffer;
 16   
 
 17   
 public class StringUtil {
 18   
 
 19  0
   private StringUtil() {}
 20   
 
 21   
   /**
 22   
    * Checks if the specified string is a valid NCName.
 23   
    */
 24  5765
   public static boolean isValidNCName(String aString) {
 25  5765
     boolean res = false;
 26  5765
     if ((null != aString) && (aString.length() > 0)) {
 27  5762
       char c = aString.charAt(0);
 28  5762
       res = CharacterClasses.isLetter(c) || ('_' == c);
 29  5762
       for (int i = aString.length(); res && (--i >= 1); ) {
 30  31867
         c = aString.charAt(i);
 31  31867
         res = CharacterClasses.hasMask(c, NCMASK) || (c == '.') || (c == '-') || (c == '_');
 32   
       }
 33   
     }
 34  5765
     return res;
 35   
   }
 36   
 
 37   
   private static final byte NCMASK = CharacterClasses.LETTER | CharacterClasses.DIGIT | CharacterClasses.COMBINING_CHAR | CharacterClasses.EXTENDER;
 38   
 
 39   
   /**
 40   
    * Checks if the specified string is a valid namespace prefix.
 41   
    */
 42  0
   public static boolean isValidNamespacePrefix(String aString) {
 43  0
     return isValidNCName(aString);
 44   
   }
 45   
 
 46   
   /**
 47   
    * Checks if the specified string is a valid QName.
 48   
    */
 49  2038
   public static boolean isValidQName(String aQName) {
 50  2038
     boolean res = aQName != null;
 51  2038
     if (res) {
 52  2038
       int idx = aQName.indexOf(':');
 53  2038
       if (idx >= 0) {
 54  1346
         res = isValidNCName(aQName.substring(0, idx)) && isValidNCName(aQName.substring(idx + 1));
 55   
       } else {
 56  692
         res = isValidNCName(aQName);
 57   
       }
 58   
     }
 59  2038
     return res;
 60   
   }
 61   
 
 62  0
   public static boolean isValidName(String aName) {
 63  0
     boolean res = (aName != null) && (aName.length() > 0);
 64  0
     if (res) {
 65  0
       char c = aName.charAt(0);
 66  0
       res = CharacterClasses.isLetter(c) || (c == '_') || (c == ':');
 67  0
       for (int i = aName.length(); res && (--i > 0); ) {
 68  0
         c = aName.charAt(i);
 69  0
         res = CharacterClasses.hasMask(c, NCMASK) || (c == '.') || (c == '-') || (c == '_') || (c == ':');
 70   
       }
 71   
     }
 72  0
     return res;
 73   
   }
 74   
 
 75  2986
   public static boolean isWhiteSpace(String aString) {
 76  2986
     boolean res = true;
 77  2986
     if (null != aString) {
 78  2986
       for (int i = aString.length(); --i >= 0; ) {
 79  17651
         char c = aString.charAt(i);
 80  17651
         if ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r')) {
 81  17646
           continue;
 82   
         }
 83  5
         res = false;
 84  5
         break;
 85   
       }
 86   
     }
 87  2986
     return res;
 88   
   }
 89   
 
 90  13
   public static boolean containsWhiteSpace(String aString) {
 91  13
     boolean res = null == aString;
 92  13
     for (int i = aString.length(); !res && (--i >= 0); ) {
 93  106
       char c = aString.charAt(i);
 94  106
       res = ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
 95   
     }
 96  13
     return res;
 97   
   }
 98   
 
 99   
   /**
 100   
    * Converts a string into a valid xml representation by escaping the reserved
 101   
    * characters.
 102   
    *
 103   
    * @param aString The string to convert. <i>(required)</i>
 104   
    * @return The converted string. <i>(required)</i>
 105   
    */
 106  60
   public static String toXml(String aString) {
 107  60
     int l = aString.length();
 108  60
     StrBuffer sb = new StrBuffer((int)(l * 1.1));
 109  60
     for (int i = 0; i < l; i++) {
 110  477
       char c = aString.charAt(i);
 111  477
       switch (c) {
 112   
         case '&':
 113  0
           sb.append("&amp;");
 114  0
           break;
 115   
         case '<':
 116  1
           sb.append("&lt;");
 117  1
           break;
 118   
         case '>':
 119  1
           sb.append("&gt;");
 120  1
           break;
 121   
         case '\'':
 122  4
           sb.append("&apos;");
 123  4
           break;
 124   
         case '"':
 125  0
           sb.append("&quot;");
 126  0
           break;
 127   
         default:
 128  471
           sb.append(c);
 129   
       }
 130   
     }
 131  60
     return sb.toString();
 132   
   }
 133   
 
 134   
   /**
 135   
    * Checks if the specified string is a valid java identifier.
 136   
    *
 137   
    * @param aString <i>(required)</i>.
 138   
    * @return Returns <code>true</code> iff the specified string is a valid java
 139   
    * identifier.
 140   
    */
 141  0
   public static boolean isJavaIdentifier(String aString) {
 142  0
     boolean valid = (aString != null) && (aString.length() > 0);
 143  0
     if (valid) {
 144  0
       char c = aString.charAt(0);
 145  0
       valid = Character.isJavaIdentifierStart(c);
 146  0
       for (int i = aString.length(); valid && (--i >= 1); ) {
 147  0
         valid = Character.isJavaIdentifierPart(c);
 148   
       }
 149   
     }
 150  0
     return valid;
 151   
   }
 152   
 
 153   
   /**
 154   
    * Checks if the specified string is a valid name of a java package.
 155   
    *
 156   
    * @param aString <i>(optional)</i>. A value of <code>null</code> specifies the
 157   
    * default package.
 158   
    * @return Returns <code>true</code> if the specified string is a valid name of
 159   
    * a java package.
 160   
    */
 161  0
   public static boolean isJavaPackage(String aString) {
 162  0
     boolean valid = aString == null;
 163  0
     if (!valid) {
 164  0
       valid = true;
 165  0
       for (Enumeration e = new StringTokenizer(aString, ".");
 166  0
               valid && e.hasMoreElements(); ) {
 167  0
         String s = (String)e.nextElement();
 168  0
         valid = isJavaIdentifier(s);
 169   
       }
 170   
     }
 171  0
     return valid;
 172   
   }
 173   
 
 174   
   /**
 175   
    * Converts the specified string into a valid Java identifier. All illegal
 176   
    * characters are replaced by underscores.
 177   
    *
 178   
    * @param aString <i>(required)</i>. The string must contain at least one
 179   
    * character.
 180   
    * @return <i>(required)</i>.
 181   
    */
 182  11605
   public static String toJavaIdentifier(String aString) {
 183  11605
     StringBuffer res = new StringBuffer();
 184  11605
     int idx = 0;
 185  11605
     char c = aString.charAt(idx);
 186  11605
     if (Character.isJavaIdentifierStart(c)) {
 187  11605
       res.append(c);
 188  11605
       idx++;
 189  0
     } else if (Character.isJavaIdentifierPart(c)) {
 190  0
       res.append('_');
 191   
     }
 192  11605
     while (idx < aString.length()) {
 193  95489
       c = aString.charAt(idx++);
 194  95489
       res.append(Character.isJavaIdentifierPart(c) ? c : '_');
 195   
     }
 196  11605
     return res.toString();
 197   
   }
 198   
 
 199   
   /**
 200   
    * Converts the specified string into a valid Java package name.
 201   
    * A <code>null</code> indicates the default package.
 202   
    *
 203   
    * @param aString <i>(optional)</i>.
 204   
    * @return <i>(optional)</i>.
 205   
    */
 206  3670
   public static String toJavaPackage(String aString) {
 207  3670
     String res = null;
 208  3670
     if (null != aString) {
 209  2922
       StringBuffer sb = new StringBuffer();
 210  2922
       for (Enumeration e = new StringTokenizer(aString, ".");
 211  6670
               e.hasMoreElements(); ) {
 212  3748
         String s = (String)e.nextElement();
 213  3748
         sb.append(toJavaIdentifier(s)).append(e.hasMoreElements() ? "." : "");
 214   
       }
 215  2922
       if (sb.length() > 0) {
 216  2922
         res = sb.toString();
 217   
       }
 218   
     }
 219  3670
     return res;
 220   
   }
 221   
 
 222  15691
   public static String firstUpper(String aString) {
 223  15691
     StrBuffer sb = new StrBuffer(aString);
 224  15691
     if (sb.length() > 0) {
 225  15691
       sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
 226   
     }
 227  15691
     return sb.toString();
 228   
   }
 229   
 
 230   
 }
 231