Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Mrz 27 2008 21:46:26 CET
file stats: LOC: 258   Methods: 10
NCLOC: 111   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
StringUtility.java 50% 67,2% 70% 62,1%
coverage coverage
 1    /* $Id: StringUtility.java,v 1.1 2008/03/27 05:16:29 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2008, Michael Meyling <mime@qedeq.org>.
 6    *
 7    * "Hilbert II" is free software; you can redistribute
 8    * it and/or modify it under the terms of the GNU General Public
 9    * License as published by the Free Software Foundation; either
 10    * version 2 of the License, or (at your option) any later version.
 11    *
 12    * This program is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15    * GNU General Public License for more details.
 16    */
 17   
 18    package org.qedeq.kernel.utility;
 19   
 20    import java.io.ByteArrayOutputStream;
 21    import java.io.IOException;
 22    import java.io.UnsupportedEncodingException;
 23    import java.util.Properties;
 24   
 25   
 26    /**
 27    * A collection of useful static methods for strings.
 28    *
 29    * LATER mime 20070101: use StringBuilder instead of StringBuffer if working under JDK 1.5
 30    *
 31    * @version $Revision: 1.1 $
 32    * @author Michael Meyling
 33    */
 34    public final class StringUtility {
 35   
 36    /**
 37    * Constructor, should never be called.
 38    */
 39  0 private StringUtility() {
 40    // don't call me
 41    }
 42   
 43    /**
 44    * Replaces all occurrences of <code>search</code> in <code>text</code>
 45    * by <code>replace</code> and returns the result.
 46    *
 47    * @param text text to work on
 48    * @param search replace this text by <code>replace</code>
 49    * @param replace replacement for <code>search</code>
 50    * @return resulting string
 51    */
 52  30464 public static String replace(final String text,
 53    final String search, final String replace) {
 54   
 55  30464 final int len = search.length();
 56  30464 if (len == 0) {
 57  0 return text;
 58    }
 59  30464 final StringBuffer result = new StringBuffer();
 60  30464 int pos1 = 0;
 61  30464 int pos2;
 62  ? while (0 <= (pos2 = text.indexOf(search, pos1))) {
 63  13651 result.append(text.substring(pos1, pos2));
 64  13651 result.append(replace);
 65  13651 pos1 = pos2 + len;
 66    }
 67  30464 if (pos1 < text.length()) {
 68  29302 result.append(text.substring(pos1));
 69    }
 70  30464 return result.toString();
 71    }
 72   
 73   
 74    /**
 75    * Replaces all occurrences of <code>search</code> in <code>text</code>
 76    * by <code>replace</code> and returns the result.
 77    *
 78    * @param text text to work on
 79    * @param search replace this text by <code>replace</code>
 80    * @param replace replacement for <code>search</code>
 81    */
 82  30424 public static void replace(final StringBuffer text,
 83    final String search, final String replace) {
 84   
 85  30424 final String result = replace(text.toString(), search, replace);
 86  30424 text.setLength(0);
 87  30424 text.append(result);
 88    // LATER mime 20050205: check if the above could be replaced with:
 89    /*
 90    final StringBuffer result = new StringBuffer();
 91    int pos1 = 0;
 92    int pos2;
 93    final int len = search.length();
 94    while (0 <= (pos2 = text.indexOf(search, pos1))) {
 95    result.append(text.substring(pos1, pos2));
 96    result.append(replace);
 97    pos1 = pos2 + len;
 98    }
 99    if (pos1 < text.length()) {
 100    result.append(text.substring(pos1));
 101    }
 102    text.setLength(0);
 103    text.append(result);
 104    */
 105    }
 106   
 107    /**
 108    * Quotes a <code>String</code>. A a quote character &quot; is appended at the
 109    * beginning and the end of the <code>String</code>. If a quote character occurs
 110    * within the string it is replaced by two quotes.
 111    *
 112    * @param unquoted the unquoted <code>String</code>
 113    * @return quoted <code>String</code>
 114    * @throws NullPointerException if <code>unquoted == null</code>
 115    */
 116  127 public static String quote(final String unquoted) {
 117  127 String result = "\"";
 118   
 119  127 for (int i = 0; i < unquoted.length(); i++) {
 120  147 if (unquoted.charAt(i) == '\"') {
 121  0 result += "\"\"";
 122    } else {
 123  147 result += unquoted.charAt(i);
 124    }
 125    }
 126  127 result += '\"';
 127  127 return result;
 128    }
 129   
 130    /**
 131    * Tests if given <code>String</code> begins with a letter and contains
 132    * only letters and digits.
 133    *
 134    * @param text test this
 135    * @return is <code>text</code> only made of letters and digits and has
 136    * a leading letter?
 137    * @throws NullPointerException if <code>text == null</code>
 138    */
 139  0 public static boolean isLetterDigitString(final String text) {
 140  0 if (text.length() <= 0) {
 141  0 return false;
 142    }
 143  0 if (!Character.isLetter(text.charAt(0))) {
 144  0 return false;
 145    }
 146  0 for (int i = 1; i < text.length(); i++) {
 147  0 if (!Character.isLetterOrDigit(text.charAt(i))) {
 148  0 return false;
 149    }
 150    }
 151  0 return true;
 152    }
 153   
 154    /**
 155    * Get amount of spaces.
 156    *
 157    * @param length number of spaces
 158    * @return String contains exactly <code>number</code> spaces
 159    */
 160  78 public static StringBuffer getSpaces(final int length) {
 161  78 final StringBuffer buffer = new StringBuffer(length >= 0 ? length : 0);
 162  78 for (int i = 0; i < length; i++) {
 163  417 buffer.append(' ');
 164    }
 165  78 return buffer;
 166    }
 167   
 168    /**
 169    * Get non qualified class name.
 170    *
 171    * @param clazz Class.
 172    * @return Non qualified class name.
 173    */
 174  30222 public static String getClassName(final Class clazz) {
 175  30222 return clazz.getName().substring(clazz.getName().lastIndexOf('.') + 1);
 176    }
 177   
 178    /**
 179    * Search for first line followed by whitespace and delete this string within the whole
 180    * text.
 181    * <p>
 182    * For example the following text
 183    *<pre>
 184    * Do you know the muffin man,
 185    * The muffin man, the muffin man,
 186    * Do you know the muffin man,
 187    * Who lives on Drury Lane?
 188    *</pre>
 189    * will be converted into:
 190    *<pre>
 191    *Do you know the muffin man,
 192    *The muffin man, the muffin man,
 193    *Do you know the muffin man,
 194    *Who lives on Drury Lane?
 195    *</pre>
 196    *
 197    * @param buffer Work on this text.
 198    */
 199  1875 public static void deleteLineLeadingWhitespace(final StringBuffer buffer) {
 200  1875 int start = -1;
 201  ? while (0 <= (start = buffer.indexOf("\n", start + 1))) {
 202  429 if (start + 1 < buffer.length() && '\n' != buffer.charAt(start + 1)) {
 203  384 break;
 204    }
 205    }
 206  1875 if (start >= 0) {
 207  384 int next = start + 1;
 208  384 while (next < buffer.length() && Character.isWhitespace(buffer.charAt(next))
 209    && '\n' != buffer.charAt(next)) {
 210  5401 next++;
 211    }
 212  384 final String empty = buffer.substring(start, next);
 213  384 if (empty.length() > 0) {
 214  384 replace(buffer, empty, "\n");
 215    }
 216    }
 217    }
 218   
 219    /**
 220    * Return a String like it appears in an property file. Thus certain characters are escaped.
 221    *
 222    * @param value Escape this value.
 223    * @return Escaped form.
 224    */
 225  0 public static String escapeProperty(final String value) {
 226  0 Properties newprops = new Properties();
 227  0 newprops.put("key", value);
 228  0 ByteArrayOutputStream out = new ByteArrayOutputStream();
 229  0 try {
 230  0 newprops.store(out, null);
 231    } catch (IOException e) {
 232  0 throw new RuntimeException(e);
 233    }
 234  0 try {
 235  0 final String file = out.toString("ISO-8859-1");
 236  0 return file.substring(file.indexOf('\n') + 1 + "key=".length());
 237    } catch (UnsupportedEncodingException e) {
 238  0 throw new RuntimeException(e);
 239    }
 240    }
 241   
 242    /**
 243    * Return a String without XML specific encoding.
 244    *
 245    * @param value Data containing markup like &gt;.
 246    * @return Escaped form.
 247    */
 248  3560 public static String decodeXmlMarkup(final StringBuffer value) {
 249    // TODO mime 20080309: replace numerical entities starting with "&"
 250    // see http://www.w3.org/TR/2000/WD-xml-2e-20000814#dt-charref
 251  3560 replace(value, "&lt;", "<");
 252  3560 replace(value, "&gt;", ">");
 253  3560 replace(value, "&amp;", "&");
 254  3560 return value.toString();
 255    }
 256   
 257   
 258    }