CCM NG: StringUtils: last adjustments for PMD

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3542 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
konermann 2015-08-03 11:26:31 +00:00
parent 6d76f200d7
commit 1b6e722e56
2 changed files with 13 additions and 62 deletions

View File

@ -17,6 +17,7 @@ package com.arsdigita.util;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
@ -34,6 +35,8 @@ import org.apache.log4j.Logger;
* *
* @author Bill Schneider * @author Bill Schneider
*/ */
@SuppressWarnings({"PMD.GodClass",
"PMD.ExcessiveClassLength", "PMD.TooManyMethods", "PMD.CyclomaticComplexity"})
public final class StringUtils { public final class StringUtils {
private static final Logger S_LOG = Logger.getLogger(StringUtils.class); private static final Logger S_LOG = Logger.getLogger(StringUtils.class);
@ -198,7 +201,7 @@ public final class StringUtils {
final Pattern plus = Pattern.compile("^\\+"); final Pattern plus = Pattern.compile("^\\+");
final Pattern word = Pattern.compile(".*[a-zA-Z_0-9\\*\\+].*"); // \\w final Pattern word = Pattern.compile(".*[a-zA-Z_0-9\\*\\+].*"); // \\w
final StringBuffer html = new StringBuffer(""); final StringBuffer html = new StringBuffer(20);
final Iterator iter = blocks.iterator(); final Iterator iter = blocks.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
final String block = (String) iter.next(); final String block = (String) iter.next();
@ -327,21 +330,6 @@ public final class StringUtils {
return result; return result;
} }
/**
*
* @param subst
* @param pattern
* @param str
* @return
*/
private static String smartTextReplace(final String subst,
final String pattern,
final String str) {
final String result = str.replaceAll(pattern, subst);
return result;
}
/** /**
* Convert a string of items separated by a separator character to an * Convert a string of items separated by a separator character to an
* (string)array of the items. sep is the separator character. Example: * (string)array of the items. sep is the separator character. Example:
@ -638,7 +626,8 @@ public final class StringUtils {
.invoke(obj, new Object[0]); .invoke(obj, new Object[0]);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
return obj.toString(); return obj.toString();
} catch (Exception e) {
} catch (IllegalAccessException | InvocationTargetException e) {
throw new UncheckedWrapperException("Invoking asString() on an " + obj.getClass(), e); throw new UncheckedWrapperException("Invoking asString() on an " + obj.getClass(), e);
} }
} }
@ -797,7 +786,7 @@ public final class StringUtils {
* @param str the String * @param str the String
* @return the String without whitespaces * @return the String without whitespaces
*/ */
public final static String trimleft(final String str) { public static String trimleft(final String str) {
for (int i = 0; i < str.length(); i++) { for (int i = 0; i < str.length(); i++) {
if (!Character.isWhitespace(str.charAt(i))) { if (!Character.isWhitespace(str.charAt(i))) {
return str.substring(i); return str.substring(i);
@ -887,7 +876,7 @@ public final class StringUtils {
while (startOfLine < formattedInput.length()) { while (startOfLine < formattedInput.length()) {
final String line = formattedInput.substring(startOfLine, Math.min(formattedInput.length(), final String line = formattedInput.substring(startOfLine, Math.min(formattedInput.length(),
startOfLine + maxLength)); startOfLine + maxLength));
if ("".equals(line)) { if ("".equals(line)) {
@ -1027,11 +1016,11 @@ public final class StringUtils {
} }
/** /**
* @throws NullPointerException if <code>throwable</code> is null * @throws IllegalArgumentException if <code>throwable</code> is null
*/ */
public static String getStackTrace(final Throwable throwable) { public static String getStackTrace(final Throwable throwable) {
if (throwable == null) { if (throwable == null) {
throw new NullPointerException("throwable"); throw new IllegalArgumentException();
} }
final StringWriter strw = new StringWriter(); final StringWriter strw = new StringWriter();

View File

@ -554,7 +554,7 @@ public class StringUtilsTest {
} }
@Test @Test
public void testPlaceholders() { public void testInterpolate() {
String in = "foo ::bar:: wizz"; String in = "foo ::bar:: wizz";
String expected_out = "foo eek wizz"; String expected_out = "foo eek wizz";
String actual_out = StringUtils.interpolate(in, "bar", "eek"); String actual_out = StringUtils.interpolate(in, "bar", "eek");
@ -653,7 +653,7 @@ public class StringUtilsTest {
assertEquals(errMsg, expected, found); assertEquals(errMsg, expected, found);
} }
@Test(expected = NullPointerException.class) @Test(expected = IllegalArgumentException.class)
public void testIfGetStackTraceFails() { public void testIfGetStackTraceFails() {
StringUtils.getStackTrace(null); StringUtils.getStackTrace(null);
} }
@ -679,42 +679,4 @@ public class StringUtilsTest {
assertEquals(StringUtils.textToHtml("line1\n\nline2"), "line1<p>line2"); assertEquals(StringUtils.textToHtml("line1\n\nline2"), "line1<p>line2");
} }
// @Test
// public void testInterpolateMap() {
//
// HashMap<String, String> hm = new HashMap<String, String>();
// hm.put("Email", "email@adressse.de");
// hm.put("Forename", "Hans");
// hm.put("Surename", "im Glück");
// hm.put("foo-fighters", "bar");
// hm.put("pf.ad", "/files/blabla/");
// hm.put("keyword", "value");
// hm.put("key:word", "value");
// hm.put("::::keyword::", "value");
//
// String src1 = "this is his E-Mailadress: ::Email::";
// String src2 = "The name is ::Surename:: , ::Forename::";
// String src3 = "We'll meet at the ::foo-fighters::";
// String src4 = "blub ::pf.ad:: right?";
// String src5 = ":::keyword::: got extra colons";
// String src6 = ":::keyword:: got 3 colons at the start";
// String src7 = "::::keyword:: got 4 colons at start";
// String src8 = "::Email:: , ::Forename:: , ::keyword:: , ::foo-fighters::";
//
// assertEquals(StringUtils.interpolate(src1, hm), "this is his E-Mailadress: email@adressse.de");
// assertEquals(StringUtils.interpolate(src2, hm), "The name is im Glück , Hans");
// assertEquals(StringUtils.interpolate(src3, hm), "We'll meet at the bar");
// assertEquals(StringUtils.interpolate(src4, hm), "blub /files/blabla/ right?");
// assertEquals(StringUtils.interpolate(src8, hm), "email@adressse.de , Hans , value , bar");
//// assertEquals(StringUtils.interpolate(src5, hm), "value got extra colons");
//// assertEquals(StringUtils.interpolate(src6, hm), "value got 3 colons at start");
//// assertEquals(StringUtils.interpolate(src7, hm), "value got 4 colons at start");
//// assertEquals("key with two words transforms to one word", StringUtils.interpolate(src5, hm) );
// }
//
// @Test
// public void testInterpolate() {
// String src = "this is a ::sample:: text";
// assertEquals(StringUtils.interpolate(src, "sample", "value"), "this is a value text");
// }
} }