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 5a8c50ef6..a76640ef3 100644
--- a/ccm-core/src/main/java/com/arsdigita/util/StringUtils.java
+++ b/ccm-core/src/main/java/com/arsdigita/util/StringUtils.java
@@ -18,7 +18,6 @@ package com.arsdigita.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
@@ -28,16 +27,6 @@ import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.apache.oro.text.perl.Perl5Util;
-import org.apache.oro.text.regex.MalformedPatternException;
-import org.apache.oro.text.regex.MatchResult;
-//import org.apache.oro.text.regex.Pattern;
-import org.apache.oro.text.regex.PatternMatcherInput;
-import org.apache.oro.text.regex.Perl5Compiler;
-import org.apache.oro.text.regex.Perl5Matcher;
-import org.apache.oro.text.regex.Substitution;
-import org.apache.oro.text.regex.Util;
-
import org.apache.log4j.Logger;
/**
@@ -49,8 +38,6 @@ public class StringUtils {
private static final Logger s_log = Logger.getLogger(StringUtils.class);
- private static Perl5Util s_re = new Perl5Util();
-
public static final String NEW_LINE = System.getProperty("line.separator");
private StringUtils() {
@@ -60,14 +47,14 @@ public class StringUtils {
/**
* Tests if a string is empty.
*
- * @param s A string to test
- * @return true if s is null or empty; otherwise
+ * @param str A string to test
+ * @return true if str is null or empty; otherwise
* false
*
*/
- public static boolean emptyString(String s) {
- if (s != null) {
- return s.isEmpty();
+ public static boolean emptyString(final String str) {
+ if (str != null) {
+ return str.isEmpty();
}
return true;
}
@@ -75,38 +62,41 @@ public class StringUtils {
/**
* Tests if a string is empty.
*
- * @param o A string to test
+ * @param obj A string to test
* @return true if o is null or empty; otherwise
* false
*/
- public static boolean emptyString(Object o) {
- boolean expr
- = (o == null || (o instanceof String && ((String) o).length() == 0));
+ public static boolean emptyString(final Object obj) {
+ final boolean expr
+ = (obj == null || (obj instanceof String && ((String) obj).length() == 0));
return expr;
}
/**
* If the String is null, returns an empty string. Otherwise, returns the
* string unaltered
+ *
+ * @param str
+ * @return empty or unalterd string
*/
- public static String nullToEmptyString(String s) {
- return (s == null) ? "" : s;
+ public static String nullToEmptyString(final String str) {
+ return (str == null) ? "" : str;
}
/**
* Escapes some "special" characters in HTML text (ampersand, angle
* brackets, quote).
*
- * @param s The plain-text string to quote
+ * @param str The plain-text string to quote
* @return The string with special characters escaped.
*/
- public static String quoteHtml(String s) {
- if (s != null) {
- s = s.replaceAll("&", "&");
- s = s.replaceAll("\"", """);
- s = s.replaceAll("<", "<");
- s = s.replaceAll(">", ">");
- return s;
+ public static String quoteHtml(String str) {
+ if (str != null) {
+ str = str.replaceAll("&", "&");
+ str = str.replaceAll("\"", """);
+ str = str.replaceAll("<", "<");
+ str = str.replaceAll(">", ">");
+ return str;
} else {
return "";
}
@@ -116,19 +106,19 @@ public class StringUtils {
* Takes a plaintext string, and returns an HTML string that, when rendered
* by a web browser, will appear as the original input string
*
- * @param s The input plaintext string
+ * @param str The input plaintext string
* @return A HTML string with blank lines coverted to
<p>and * ampersands/angle brackets escaped. */ - public static String textToHtml(String s) { - s = quoteHtml(s); - s = s.replaceAll("\r\n\r\n", "
"); - s = s.replaceAll("\n\n", "
"); - s = s.replaceAll("\r\r", "
");
- s = s.replaceAll("\r\n", "
");
- s = s.replaceAll("\n", "
");
- s = s.replaceAll("\r", "
");
- return s;
+ public static String textToHtml(String str) {
+ str = quoteHtml(str);
+ str = str.replaceAll("\r\n\r\n", "
"); + str = str.replaceAll("\n\n", "
"); + str = str.replaceAll("\r\r", "
");
+ str = str.replaceAll("\r\n", "
");
+ str = str.replaceAll("\n", "
");
+ str = str.replaceAll("\r", "
");
+ return str;
}
/**
@@ -136,17 +126,17 @@ public class StringUtils {
* extensive conversion of HTML fragments to plain text equivalents, see
* {@link HtmlToText}.
*/
- public static String htmlToText(String s) {
- if (s != null) {
+ public static String htmlToText(String str) {
+ if (str != null) {
// first take out new-lines
- s = s.replaceAll("\n", "");
- s = s.replaceAll("\r", "");
+ str = str.replaceAll("\n", "");
+ str = str.replaceAll("\r", "");
- s = s.replaceAll("<[pP]>", "\n\n");
- s = s.replaceAll("
", "\n");
+ str = str.replaceAll("<[pP]>", "\n\n");
+ str = str.replaceAll("
", "\n");
// take out other tags
- s = s.replaceAll("<[^>]*>", " ");
- return s;
+ str = str.replaceAll("<[^>]*>", " ");
+ return str;
} else {
return "";
}
@@ -191,27 +181,29 @@ public class StringUtils {
* ®
*
*/
- public static String smartTextToHtml(String s) {
- ArrayList blocks = new ArrayList();
+ public static String smartTextToHtml(final String str) {
//first splits the string at every new line
- s_re.split(blocks, "/\\r?\\n(\\r?\\n)+/", s);
+// String blocks2[] = str.split( "\r?\n(\r?(?<=\n))");
+// Arrays.asList(str.split( "?<=\r?\n\r?\n")
+// ArrayList blocks = new ArrayList();
+ final List
");
@@ -231,17 +223,17 @@ public class StringUtils {
*
* @param match
* @param type
- * @param s
+ * @param str
* @return
*/
- private static String smartTextList(String match,
- String type,
- String s) {
+ private static String smartTextList(final String match,
+ final String type,
+ final String str) {
// ArrayList blocks = new ArrayList();
// s_re.split(blocks, match, s);
- String[] blocks = s.split(match);
+ final String[] blocks = str.split(match);
- StringBuffer list = new StringBuffer("<" + type + ">\n");
+ final StringBuffer list = new StringBuffer("<" + type + ">\n");
// Iterator i = blocks.iterator();
for (int j = 0; j < blocks.length; j++) {
@@ -272,240 +264,125 @@ public class StringUtils {
/**
* dont use directly, use smartTextToHtml instead
*
- * @param s
+ * @param str
* @return
*/
- private static String smartTextInline(String s) {
+ private static String smartTextInline(String str) {
if (s_log.isDebugEnabled()) {
- s_log.debug("Input {" + s + "}");
+ 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.
- s = s.replaceAll("[\u0001|\u0002|\u0003]", "");
+ str = str.replaceAll("[\u0001|\u0002|\u0003]", "");
// We transform a few common symbols
- s = s.replaceAll("1/4", "¼");
- s = s.replaceAll("1/2", "½");
- s = s.replaceAll("3/4", "¾");
- s = s.replaceAll("\\([Cc]\\)", "©");
- s = s.replaceAll("\\([Rr]\\)", "®");
- s = s.replaceAll("\\(TM\\)|\\(tm\\)", "TM");
- s = s.replaceAll("^\\+", "");
+ str = str.replaceAll("1/4", "¼");
+ str = str.replaceAll("1/2", "½");
+ str = str.replaceAll("3/4", "¾");
+ str = str.replaceAll("\\([Cc]\\)", "©");
+ str = str.replaceAll("\\([Rr]\\)", "®");
+ str = str.replaceAll("\\(TM\\)|\\(tm\\)", "TM");
+ str = str.replaceAll("^\\+", "");
if (s_log.isDebugEnabled()) {
- s_log.debug("After entities {" + s + "}");
+ s_log.debug("After entities {" + str + "}");
}
// Next lets process italics /italic/
- s = s.replaceAll("/+([a-zA-Z_0-9]+)+/", "$1");
+ str = str.replaceAll("/+([a-zA-Z_0-9]+)+/", "$1");
// Lets process bold text *bold*
- s = s.replaceAll("\\*+([a-zA-Z_0-9]+)+\\*", "$1");
+ str = str.replaceAll("\\*+([a-zA-Z_0-9]+)+\\*", "$1");
// Now we're onto the monospace stuff =monospace=
- s = s.replaceAll("\\=+([a-zA-Z_0-9]+)+\\=", "$1");
+ str = str.replaceAll("\\=+([a-zA-Z_0-9]+)+\\=", "$1");
if (s_log.isDebugEnabled()) {
- s_log.debug("After styles {" + s + "}");
+ s_log.debug("After styles {" + str + "}");
}
// untitled mailto
//"mailto:dan@berrange.com" to
//"mailto:dan@berrange.com"
- s = s.replaceAll("mailto:([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})\\b",
+ str = str.replaceAll("mailto:([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})\\b",
"mailto:$1");
//adding a '@' before every untitled link so it does not interfere with
// titled links
- s = s.replaceAll("(http[s]*://www\\..+\\.[A-Za-z]{2,4})",
+ str = str.replaceAll("(http[s]*://www\\..+\\.[A-Za-z]{2,4})",
"@$1");
// titled Links
//"@google(http://www.google.com) to
//google"
- s = s.replaceAll("@(\\w+)\\(@(http*[s]*:*/*/*www\\..+\\.[A-Za-z]{2,4})\\)",
+ str = str.replaceAll("@(\\w+)\\(@(http*[s]*:*/*/*www\\..+\\.[A-Za-z]{2,4})\\)",
"$1");
//titled mailto
//"@Dan B(mailto:dan@@berrange.com)" to
//"Dan B"
- s = s.replaceAll("@([\\w\\s]+)\\(mailto:([a-zA-Z0-9._%+-]+@@[a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})\\)",
+ str = str.replaceAll("@([\\w\\s]+)\\(mailto:([a-zA-Z0-9._%+-]+@@[a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})\\)",
"$1");
//remove one of the two @'s
- s = s.replaceAll("mailto:([a-zA-Z0-9._%+-]+)+@@([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})",
+ str = str.replaceAll("mailto:([a-zA-Z0-9._%+-]+)+@@([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})",
"mailto:$1@$2");
//untitled links (which got an '@' in front now)
- s = s.replaceAll("@(http[s]*://www\\..+\\.[A-Za-z]{2,4})",
+ str = str.replaceAll("@(http[s]*://www\\..+\\.[A-Za-z]{2,4})",
"$1");
if (s_log.isDebugEnabled()) {
- s_log.debug("After links {" + s + "}");
+ s_log.debug("After links {" + str + "}");
}
- return s;
+ return str;
}
/**
- * same as replaceAll()?
*
* @param subst
* @param pattern
- * @param s
+ * @param str
* @return
*/
- private static String smartTextReplace(Substitution subst,
- String pattern,
- String s) {
- Perl5Matcher matcher = new Perl5Matcher();
- Perl5Compiler compiler = new Perl5Compiler();
- StringBuffer result = new StringBuffer();
- PatternMatcherInput input = new PatternMatcherInput(s);
-
- try {
- Util.substitute(result,
- matcher,
- compiler.compile(pattern),
- subst,
- input,
- Util.SUBSTITUTE_ALL);
- } catch (MalformedPatternException e) {
- throw new UncheckedWrapperException("cannot perform substitution", e);
- }
- return result.toString();
+ private static String smartTextReplace(final String subst,
+ final String pattern,
+ String str) {
+ str = str.replaceAll(pattern, subst);
+ return str;
}
- /**
- *
- */
-// private static class TitledLinkSubstitution implements Substitution {
-//
-// private Map m_hash;
-//
-// public TitledLinkSubstitution(Map hash) {
-// m_hash = hash;
-// }
-//
-// @Override
-// public void appendSubstitution(StringBuffer appendBuffer,
-// MatchResult match,
-// int substitutionCount,
-// PatternMatcherInput originalInput,
-// PatternMatcher matcher,
-// Pattern pattern) {
-// String title = match.group(1);
-// String link = match.group(2);
-// s_log.debug("Link: " + link);
-//
-// Integer i = m_hash.size();
-// s_log.debug("Key: " + i);
-// m_hash.put(i, link);
-// String dst = "@" + title + "(\u0002" + i.toString() + "\u0002)";
-// appendBuffer.append(dst);
-// s_log.debug("Encoded Link: " + dst);
-// }
-// }
-// /**
-// *
-// */
-// private static class UnobscureSubstitution implements Substitution {
-//
-// private Map m_hash;
-//
-// public UnobscureSubstitution(Map hash) {
-// m_hash = hash;
-// }
-//
-// public void appendSubstitution(StringBuffer appendBuffer,
-// MatchResult match,
-// int substitutionCount,
-// PatternMatcherInput originalInput,
-// PatternMatcher matcher,
-// Pattern pattern) {
-// String s = match.group(1);
-// s_log.debug("Key: " + s);
-//
-// Integer i = Integer.valueOf(s);
-// appendBuffer.append((String) m_hash.get(i));
-// s_log.debug("Link: " + m_hash.get(i));
-// }
-// }
-//
-// /**
-// *
-// */
-// private static class EntitySubstitution implements Substitution {
-//
-// public void appendSubstitution(StringBuffer appendBuffer,
-// MatchResult match,
-// int substitutionCount,
-// PatternMatcherInput originalInput,
-// PatternMatcher matcher,
-// Pattern pattern) {
-// String s = match.group(1);
-// s_log.debug("Key: " + s);
-//
-// appendBuffer.append((String) s_entities.get(s));
-// s_log.debug("Entity: " + s_entities.get(s));
-// }
-// }
-//
-// private Map m_hash;
-//
-// public UntitledLinkSubstitution(Map hash) {
-// m_hash = hash;
-// }
-//
-// public void appendSubstitution(StringBuffer appendBuffer,
-// MatchResult match,
-// int substitutionCount,
-// PatternMatcherInput originalInput,
-// PatternMatcher matcher,
-// Pattern pattern) {
-// String link = match.group(1);
-// s_log.debug("Link: " + link);
-//
-// Integer i = m_hash.size();
-// s_log.debug("Key: " + i);
-// m_hash.put(i, link);
-// String dst = "@\u0002" + i.toString() + "\u0002(\u0002"
-// + i.toString() + "\u0002)";
-// appendBuffer.append(dst);
-// s_log.debug("Encoded Link: " + dst);
-// }
-// }
-
/**
* Convert a string of items separated by a separator character to an
* (string)array of the items. sep is the separator character. Example:
- * Input - s == "cat,house,dog,," sep==',' Output - {"cat", "house", "dog",
- * "" ,""} different to java.lang.String.split(): Input - s ==
+ * Input - str == "cat,house,dog,," sep==',' Output - {"cat", "house",
+ * "dog", "" ,""} different to java.lang.String.split(): Input - str ==
* "cat,house,dog,," sep==',' Output - {"cat", "house", "dog"}
*
- * @param s string contains items separated by a separator character.
+ * @param str string contains items separated by a separator character.
* @param sep separator character.
* @return Array of items.
*
*/
- public static String[] split(String s, char sep) {
- ArrayList al = new ArrayList();
- int start_pos, end_pos;
- start_pos = 0;
- while (start_pos < s.length()) {
- end_pos = s.indexOf(sep, start_pos);
- if (end_pos == -1) {
- end_pos = s.length();
+ public static String[] split(final String str, final char sep) {
+ final ArrayList arrl = new ArrayList();
+ int startpos, endpos;
+ startpos = 0;
+ while (startpos < str.length()) {
+ endpos = str.indexOf(sep, startpos);
+ if (endpos == -1) {
+ endpos = str.length();
}
- String found_item = s.substring(start_pos, end_pos);
- al.add(found_item);
- start_pos = end_pos + 1;
+ final String found_item = str.substring(startpos, endpos);
+ arrl.add(found_item);
+ startpos = endpos + 1;
}
- if (s.length() > 0 && s.charAt(s.length() - 1) == sep) {
- al.add(""); // In case last character is separator
+ if (str.length() > 0 && str.charAt(str.length() - 1) == sep) {
+ arrl.add(""); // In case last character is separator
}
- String[] returned_array = new String[al.size()];
- al.toArray(returned_array);
- return returned_array;
+ final String[] returnedArray = new String[arrl.size()];
+ arrl.toArray(returnedArray);
+ return returnedArray;
}
/**
@@ -519,14 +396,14 @@ public class StringUtils {
* As an example, let's say the original string is:
- * s = "/packages/foo/xsl/::vhost::/foo_::locale::.xsl";
+ * str = "/packages/foo/xsl/::vhost::/foo_::locale::.xsl";
*
*
* * We call the function like this:
* *
- * output = splitUp (s, "/::\\w+::/");
+ * output = splitUp (str, "::\\w+::");
*
*
*
@@ -555,9 +432,8 @@ public class StringUtils {
* ("The following text will be ", "", "bold", "", ".")
*
*
- * @param s The original string to split.
- * @param re The regular expression in the format required by
- * {@link org.apache.oro.text.perl.Perl5Util#match(String, String)}.
+ * @param str The original string to split.
+ * @param re The regular expression.
* @return List of substrings.
*
* @author Richard W.M. Jones
@@ -567,36 +443,70 @@ public class StringUtils {
* specifically: @a = /(RE)|(.+)/g;
find in str and
* replaces them with them with replace.
@@ -1205,11 +1118,11 @@ public class StringUtils {
throw new NullPointerException("throwable");
}
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter(sw);
- throwable.printStackTrace(pw);
- pw.close();
- return sw.toString();
+ final StringWriter strw = new StringWriter();
+ final PrintWriter prntw = new PrintWriter(strw);
+ throwable.printStackTrace(prntw);
+ prntw.close();
+ return strw.toString();
}
/**
@@ -1242,10 +1155,10 @@ public class StringUtils {
* @throws NullPointerException if throwable is null
*/
public static List getStackList(Throwable throwable) {
- StringTokenizer tkn = new StringTokenizer(getStackTrace(throwable), System.getProperty("line.separator"));
- List list = new LinkedList();
+ final StringTokenizer tkn = new StringTokenizer(getStackTrace(throwable), System.getProperty("line.separator"));
+ final List list = new LinkedList();
while (tkn.hasMoreTokens()) {
- String token = tkn.nextToken().trim();
+ final String token = tkn.nextToken().trim();
if ("".equals(token)) {
continue;
}
@@ -1274,16 +1187,16 @@ public class StringUtils {
if (name == null) {
return null;
}
- StringBuffer urlizedName = new StringBuffer(name.length());
+ final StringBuffer urlizedName = new StringBuffer(name.length());
for (int i = 0; i < name.length(); i++) {
- char ch = name.charAt(i);
+ final char chr = name.charAt(i);
- if (Character.isLetter(ch)) {
- urlizedName.append(Character.toLowerCase(ch));
- } else if (Character.isDigit(ch) || ch == '_' || ch == '-') {
- urlizedName.append(ch);
- } else if (ch == ' ' || ch == '&' || ch == '/') {
+ if (Character.isLetter(chr)) {
+ urlizedName.append(Character.toLowerCase(chr));
+ } else if (Character.isDigit(chr) || chr == '_' || chr == '-') {
+ urlizedName.append(chr);
+ } else if (chr == ' ' || chr == '&' || chr == '/') {
urlizedName.append('-');
}
}
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 4c2364ab2..5b195c3d5 100644
--- a/ccm-core/src/test/java/com/arsdigita/util/StringUtilsTest.java
+++ b/ccm-core/src/test/java/com/arsdigita/util/StringUtilsTest.java
@@ -59,6 +59,29 @@ public class StringUtilsTest {
@After
public void tearDown() {
}
+
+
+
+ @Test
+ public void testSmartText2() {
+ String src = "blabla\n"
+ +"\n"
+ +"blub";
+
+ String expected = "