From 9102b422bf606cca54e8015f029c9e2223c6f301 Mon Sep 17 00:00:00 2001 From: Romain Pelisse Date: Fri, 23 Sep 2011 17:55:34 +0000 Subject: [PATCH] apply patch 3084292: character reference in xml report - thanks to Seko git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7319 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 1 + .../sourceforge/pmd/util/StringUtilTest.java | 4 +- .../net/sourceforge/pmd/util/StringUtil.java | 143 ++++++++---------- 3 files changed, 69 insertions(+), 79 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 36005f9488..db79143b88 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -439,6 +439,7 @@ The RuleSet XML Schema is located in the source at: etc/ruleset_2_0_0.xsd The RuleSet DTD is located in the source at: etc/ruleset_2_0_0.dtd Improved include/exclude pattern matching performance for ends-with type patterns. Modify (and hopefully fixed) CPD algorithm thanks to a patch from Juan Jesús García de Soria. +Fixed character reference in xml report - thanks to Seko New Java rules: diff --git a/pmd/regress/test/net/sourceforge/pmd/util/StringUtilTest.java b/pmd/regress/test/net/sourceforge/pmd/util/StringUtilTest.java index 9899e3fd39..58e48d4d0d 100644 --- a/pmd/regress/test/net/sourceforge/pmd/util/StringUtilTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/util/StringUtilTest.java @@ -37,7 +37,7 @@ public class StringUtilTest { /** * Usually you would set the system property "net.sourceforge.pmd.supportUTF8" to either "no" or "yes", to * switch UTF8 support. - * + * * e.g. * System.setProperty("net.sourceforge.pmd.supportUTF8","yes"); */ @@ -46,7 +46,7 @@ public class StringUtilTest { StringBuilder sb = new StringBuilder(); String test = "é"; StringUtil.appendXmlEscaped(sb, test, false); - assertEquals("é", sb.toString()); + assertEquals("é", sb.toString()); } @Test diff --git a/pmd/src/net/sourceforge/pmd/util/StringUtil.java b/pmd/src/net/sourceforge/pmd/util/StringUtil.java index ae527ca4df..9fbae1760a 100644 --- a/pmd/src/net/sourceforge/pmd/util/StringUtil.java +++ b/pmd/src/net/sourceforge/pmd/util/StringUtil.java @@ -9,118 +9,111 @@ import java.util.List; /** * A number of String-specific utility methods for use by PMD or its IDE plugins. - * + * * @author br */ public final class StringUtil { public static final String[] EMPTY_STRINGS = new String[0]; private static final boolean SUPPORTS_UTF8 = System.getProperty("net.sourceforge.pmd.supportUTF8", "no").equals("yes"); - private static final String[] ENTITIES; - static { - ENTITIES = new String[256 - 126]; - for (int i = 126; i <= 255; i++) { - ENTITIES[i - 126] = "&#" + i + ';'; - } - } private StringUtil() {} - + /** * Return whether the non-null text arg starts with any of the prefix - * values. - * + * values. + * * @param text * @param prefixes * @return */ public static boolean startsWithAny(String text, String... prefixes) { - + for (String prefix : prefixes) { if (text.startsWith(prefix)) return true; } - + return false; } - + /** * Returns whether the non-null text arg matches any of the test values. - * + * * @param text * @param tests * @return */ public static boolean isAnyOf(String text, String... tests) { - + for (String test : tests) { if (text.equals(test)) return true; } - + return false; } - + /** * Checks for the existence of any of the listed prefixes on the * non-null text and removes them. - * + * * @param text * @param prefixes * @return */ public static String withoutPrefixes(String text, String... prefixes) { - + for (String prefix : prefixes) { if (text.startsWith(prefix)) { return text.substring(prefix.length()); } } - + return text; } - + /** * Returns true if the value arg is either null, empty, or full of whitespace characters. * More efficient that calling (string).trim().length() == 0 - * + * * @param value * @return true if the value is empty, false otherwise. */ public static boolean isEmpty(String value) { - + if (value == null || "".equals(value)) { return true; } - + for (int i=0; i', ">"); } - + /** - * + * * @param buf * @param src * @param supportUTF8 override the default setting, whether special characters should be replaced * with entities (false) or should be included as is (true). - * + * * @see #appendXmlEscaped(StringBuffer, String) - * + * * TODO - unify the method above with the one below - * + * * public to support unit testing - make this package private, once the unit test classes are in the same package. */ public static void appendXmlEscaped(StringBuilder buf, String src, boolean supportUTF8) { @@ -195,11 +188,7 @@ public final class StringUtil { c = src.charAt(i); if (c > '~') {// 126 if (!supportUTF8) { - if (c <= 255) { - buf.append(ENTITIES[c - 126]); - } else { - buf.append("&u").append(Integer.toHexString(c)).append(';'); - } + buf.append("&#x").append(Integer.toHexString(c)).append(';'); } else { buf.append(c); } @@ -233,7 +222,7 @@ public final class StringUtil { if (source == null || source.length() == 0) { return EMPTY_STRINGS; } - + int delimiterCount = 0; int length = source.length(); char[] chars = source.toCharArray(); @@ -264,16 +253,16 @@ public final class StringUtil { return results; } - + /** * Much more efficient than StringTokenizer. - * + * * @param str String * @param separator char * @return String[] */ public static String[] substringsOf(String str, String separator) { - + if (str == null || str.length() == 0) { return EMPTY_STRINGS; } @@ -294,8 +283,8 @@ public final class StringUtil { list.add(str.substring(currPos)); return list.toArray(new String[list.size()]); } - - + + /** * Copies the elements returned by the iterator onto the string buffer * each delimited by the separator. @@ -305,11 +294,11 @@ public final class StringUtil { * @param separator String */ public static void asStringOn(StringBuffer sb, Iterator iter, String separator) { - + if (!iter.hasNext()) { return; } - + sb.append(iter.next()); - + while (iter.hasNext()) { sb.append(separator); sb.append(iter.next()); @@ -317,46 +306,46 @@ public final class StringUtil { } /** * Return the length of the shortest string in the array. - * If the collection is empty or any one of them is + * If the collection is empty or any one of them is * null then it returns 0. - * + * * @param strings String[] * @return int */ public static int lengthOfShortestIn(String[] strings) { - + if (CollectionUtil.isEmpty(strings)) { return 0; } - + int minLength = Integer.MAX_VALUE; - + for (int i=0; i