diff --git a/ccm-core/src/main/java/com/arsdigita/util/StringUtils.java b/ccm-core/src/main/java/com/arsdigita/util/StringUtils.java index 93fbfe6db..593fa342b 100644 --- a/ccm-core/src/main/java/com/arsdigita/util/StringUtils.java +++ b/ccm-core/src/main/java/com/arsdigita/util/StringUtils.java @@ -17,6 +17,7 @@ package com.arsdigita.util; import java.io.PrintWriter; import java.io.StringWriter; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; @@ -34,6 +35,8 @@ import org.apache.log4j.Logger; * * @author Bill Schneider */ +@SuppressWarnings({"PMD.GodClass", + "PMD.ExcessiveClassLength", "PMD.TooManyMethods", "PMD.CyclomaticComplexity"}) public final class StringUtils { 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 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(); while (iter.hasNext()) { final String block = (String) iter.next(); @@ -260,7 +263,7 @@ public final class StringUtils { if (S_LOG.isDebugEnabled()) { S_LOG.debug("Input {" + str + "}"); } - + // We're going to use the octal characters \u0001 and \u0002 for // escaping stuff, so we'd better make sure there aren't any // in the text. @@ -327,21 +330,6 @@ public final class StringUtils { 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 * (string)array of the items. sep is the separator character. Example: @@ -638,7 +626,8 @@ public final class StringUtils { .invoke(obj, new Object[0]); } catch (NoSuchMethodException e) { return obj.toString(); - } catch (Exception e) { + + } catch (IllegalAccessException | InvocationTargetException e) { throw new UncheckedWrapperException("Invoking asString() on an " + obj.getClass(), e); } } @@ -797,7 +786,7 @@ public final class StringUtils { * @param str the String * @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++) { if (!Character.isWhitespace(str.charAt(i))) { return str.substring(i); @@ -887,7 +876,7 @@ public final class StringUtils { 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)); if ("".equals(line)) { @@ -1027,11 +1016,11 @@ public final class StringUtils { } /** - * @throws NullPointerException if throwable is null + * @throws IllegalArgumentException if throwable is null */ public static String getStackTrace(final Throwable throwable) { if (throwable == null) { - throw new NullPointerException("throwable"); + throw new IllegalArgumentException(); } final StringWriter strw = new StringWriter(); diff --git a/ccm-core/src/test/java/com/arsdigita/util/StringUtilsTest.java b/ccm-core/src/test/java/com/arsdigita/util/StringUtilsTest.java index 09f803b54..c1f0c753c 100644 --- a/ccm-core/src/test/java/com/arsdigita/util/StringUtilsTest.java +++ b/ccm-core/src/test/java/com/arsdigita/util/StringUtilsTest.java @@ -554,7 +554,7 @@ public class StringUtilsTest { } @Test - public void testPlaceholders() { + public void testInterpolate() { String in = "foo ::bar:: wizz"; String expected_out = "foo eek wizz"; String actual_out = StringUtils.interpolate(in, "bar", "eek"); @@ -653,7 +653,7 @@ public class StringUtilsTest { assertEquals(errMsg, expected, found); } - @Test(expected = NullPointerException.class) + @Test(expected = IllegalArgumentException.class) public void testIfGetStackTraceFails() { StringUtils.getStackTrace(null); } @@ -679,42 +679,4 @@ public class StringUtilsTest { assertEquals(StringUtils.textToHtml("line1\n\nline2"), "line1

line2"); } -// @Test -// public void testInterpolateMap() { -// -// HashMap hm = new HashMap(); -// 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"); -// } }