commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chtom...@apache.org
Subject [1/3] [text] TEXT-29: Add a builder to StringEscapeUtils
Date Thu, 15 Dec 2016 18:00:43 GMT
Repository: commons-text
Updated Branches:
  refs/heads/master 56eef1f88 -> 2b0a1ebd8


TEXT-29: Add a builder to StringEscapeUtils


Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/8587bb6f
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/8587bb6f
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/8587bb6f

Branch: refs/heads/master
Commit: 8587bb6f01521e794595df2ebb726ecc704f0076
Parents: 5cdb779
Author: Rob Tompkins <chtompki@gmail.com>
Authored: Thu Dec 15 08:57:34 2016 -0500
Committer: Rob Tompkins <chtompki@gmail.com>
Committed: Thu Dec 15 08:57:34 2016 -0500

----------------------------------------------------------------------
 .../apache/commons/text/StringEscapeUtils.java  | 68 ++++++++++++++++++++
 .../commons/text/StringEscapeUtilsTest.java     |  8 +++
 2 files changed, 76 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/8587bb6f/src/main/java/org/apache/commons/text/StringEscapeUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/StringEscapeUtils.java b/src/main/java/org/apache/commons/text/StringEscapeUtils.java
index 6b88275..c056dbd 100644
--- a/src/main/java/org/apache/commons/text/StringEscapeUtils.java
+++ b/src/main/java/org/apache/commons/text/StringEscapeUtils.java
@@ -436,6 +436,74 @@ public class StringEscapeUtils {
         super();
     }
 
+    /**
+     * <p>Convenience wrapper for {@link java.lang.StringBuilder} providing escape
methods.</p>
+     *
+     * <p>Example:</p>
+     * <pre>
+     * new Builder(ESCAPE_HTML4)
+     *      .append("&lt;p>")
+     *      .escape("This is paragraph 1 and special chars like &amp; get escaped.")
+     *      .append("&lt;/p>&lt;p>")
+     *      .escape("This is paragraph 2 &amp; more...")
+     *      .append("&lt;/p>")
+     *      .toString()
+     * </pre>
+     *
+     * @since 3.6
+     */
+    public static class Builder {
+
+        private final StringBuilder sb;
+        private final CharSequenceTranslator translator;
+
+        private Builder(final CharSequenceTranslator translator) {
+            this.sb = new StringBuilder();
+            this.translator = translator;
+        }
+
+        /**
+         * <p>Escape {@code input} according to the given {@link CharSequenceTranslator}.</p>
+         *
+         * @param input the String to escape
+         * @return {@code this}, to enable chaining
+         */
+        public Builder escape(final String input) {
+            sb.append(translator.translate(input));
+            return this;
+        }
+
+        /**
+         * Literal append, no escaping being done.
+         *
+         * @param input the String to append
+         * @return {@code this}, to enable chaining
+         */
+        public Builder append(final String input) {
+            sb.append(input);
+            return this;
+        }
+
+        /**
+         * <p>Return the escaped string.</p>
+         *
+         * @return the escaped string
+         */
+        @Override
+        public String toString() {
+            return sb.toString();
+        }
+    }
+
+    /**
+     * Get a {@link Builder}.
+     * @return {@link Builder}
+     * @since 3.2
+     */
+    public static StringEscapeUtils.Builder builder(final CharSequenceTranslator translator)
{
+        return new Builder(translator);
+    }
+
     // Java and JavaScript
     //--------------------------------------------------------------------------
     /**

http://git-wip-us.apache.org/repos/asf/commons-text/blob/8587bb6f/src/test/java/org/apache/commons/text/StringEscapeUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/StringEscapeUtilsTest.java b/src/test/java/org/apache/commons/text/StringEscapeUtilsTest.java
index 4700cde..6914f8f 100644
--- a/src/test/java/org/apache/commons/text/StringEscapeUtilsTest.java
+++ b/src/test/java/org/apache/commons/text/StringEscapeUtilsTest.java
@@ -618,4 +618,12 @@ public class StringEscapeUtilsTest {
         assertEquals(expected, StringEscapeUtils.escapeJson(input));
     }
 
+    @Test
+    public void testBuilder() {
+        String result = StringEscapeUtils.builder(StringEscapeUtils.ESCAPE_XML10)
+                .escape("<").append(">").toString();
+
+        assertEquals("&lt;>", result);
+    }
+
 }
\ No newline at end of file


Mime
View raw message