diff --git a/src/java/org/apache/commons/lang/StringEscapeUtils.java b/src/java/org/apache/commons/lang/StringEscapeUtils.java index d8fe391ba..abcc6aa9b 100644 --- a/src/java/org/apache/commons/lang/StringEscapeUtils.java +++ b/src/java/org/apache/commons/lang/StringEscapeUtils.java @@ -559,6 +559,32 @@ public static void unescapeHtml(Writer writer, String string) throws IOException Entities.HTML40.unescape(writer, string); } + /** + *
Escapes the characters in a String
using XML entities.
For example: "bread" & "butter" => + * "bread" & "butter". + *
+ * + *Supports only the five basic XML entities (gt, lt, quot, amp, apos). + * Does not support DTDs or external entities.
+ * + * @param writer writer receiving the unescaped string + * @param str theString
to escape, may be null
+ * @return a new escaped String
, null
if null string input
+ * @see #unescapeXml(java.lang.String)
+ **/
+ public static void escapeXml(Writer writer, String str) throws IOException {
+ if (writer == null ) {
+ throw new IllegalArgumentException ("The Writer must not be null.");
+ }
+
+ if (str == null) {
+ return;
+ }
+ Entities.XML.escape(writer, str);
+ }
+
/**
* Escapes the characters in a String
using XML entities.
Unescapes a string containing XML entity escapes to a string + * containing the actual Unicode characters corresponding to the + * escapes.
+ * + *Supports only the five basic XML entities (gt, lt, quot, amp, apos). + * Does not support DTDs or external entities.
+ * + * @param writer writer receiving the unescaped string + * @param str theString
to unescape, may be null
+ * @return a new unescaped String
, null
if null string input
+ * @see #escapeXml(String)
+ **/
+ public static void unescapeXml(Writer writer, String str) throws IOException {
+ if (writer == null ) {
+ throw new IllegalArgumentException ("The Writer must not be null.");
+ }
+
+ if (str == null) {
+ return;
+ }
+ Entities.XML.unescape(writer, str);
+ }
+
/**
* Unescapes a string containing XML entity escapes to a string
* containing the actual Unicode characters corresponding to the
diff --git a/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java b/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java
index cb8cf2733..33583bcaa 100644
--- a/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java
+++ b/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java
@@ -290,6 +290,20 @@ public void testEscapeXml() throws Exception {
assertEquals("", StringEscapeUtils.escapeXml(""));
assertEquals(null, StringEscapeUtils.escapeXml(null));
assertEquals(null, StringEscapeUtils.unescapeXml(null));
+
+ StringWriter sw = new StringWriter();
+ try {
+ StringEscapeUtils.escapeXml(sw, "