Skip to content

Commit a982263

Browse files
committed
BAEL-5732 Initial code changes for Unescaping html entities/chars.
1 parent a99c20f commit a982263

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

spring-core-4/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ This module contains articles about core Spring functionality
77
- [Creating Spring Beans Through Factory Methods](https://www.baeldung.com/spring-beans-factory-methods)
88
- [Spring BeanPostProcessor](https://www.baeldung.com/spring-beanpostprocessor)
99
- [Escape HTML Symbols in Java](https://www.baeldung.com/java-escape-html-symbols)
10+
- [Unescape HTML Symbols in Java](https://www.baeldung.com/unescape-html-characters-in-java)
1011
- More articles: [[<-- prev]](/spring-core-3)

spring-core-4/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
<artifactId>commons-text</artifactId>
7171
<version>${commons-text.version}</version>
7272
</dependency>
73+
<dependency>
74+
<groupId>org.unbescape</groupId>
75+
<artifactId>unbescape</artifactId>
76+
<version>1.1.6.RELEASE</version>
77+
</dependency>
7378
</dependencies>
7479

7580
<properties>
@@ -80,4 +85,4 @@
8085
<commons-text.version>1.10.0</commons-text.version>
8186
</properties>
8287

83-
</project>
88+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.Unescapehtml;
2+
3+
import org.apache.commons.text.StringEscapeUtils;
4+
import org.springframework.web.util.HtmlUtils;
5+
import org.unbescape.html.HtmlEscape;
6+
7+
public class HtmlUnescapeUtils {
8+
public static String unescapeWithApacheCommons(String htmlInput) {
9+
return StringEscapeUtils.unescapeHtml4(htmlInput);
10+
}
11+
12+
public static String unescapeWithSpring(String htmlInput) {
13+
return HtmlUtils.htmlUnescape(htmlInput);
14+
}
15+
16+
public static String unescapeWithUnbescape(String htmlInput) {
17+
return HtmlEscape.unescapeHtml(htmlInput);
18+
}
19+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.unescapehtml;
2+
3+
import org.junit.Assert;
4+
import org.testng.annotations.Test;
5+
6+
import com.baeldung.Unescapehtml.HtmlUnescapeUtils;
7+
8+
public class HtmlUnescapeUtilsTest {
9+
@Test
10+
public void givenAStringWithEscapedAmpersand_whenConverted_thenUnescape() {
11+
String escapedText = "&#38;";
12+
String expectedText = "&";
13+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithApacheCommons(escapedText));
14+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithSpring(escapedText));
15+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithUnbescape(escapedText));
16+
17+
escapedText = "&amp;";
18+
expectedText = "&";
19+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithApacheCommons(escapedText));
20+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithSpring(escapedText));
21+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithUnbescape(escapedText));
22+
}
23+
24+
@Test
25+
public void givenAStringWithDoubleQuotes_whenConverted_thenUnescape() {
26+
String expectedText = "\"Hello\" Baeldung";
27+
String escapedText = "&quot;Hello&quot; Baeldung";
28+
29+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithApacheCommons(escapedText));
30+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithSpring(escapedText));
31+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithUnbescape(escapedText));
32+
}
33+
34+
@Test
35+
public void givenAStringWithHtmlSymbols_whenConverted_thenUnescape() {
36+
String expectedText = "<p><strong>Test sentence in bold type.</strong></p>";
37+
String escapedText = "&lt;p&gt;&lt;strong&gt;Test sentence in bold type.&lt;/strong&gt;&lt;/p&gt;";
38+
39+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithApacheCommons(escapedText));
40+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithSpring(escapedText));
41+
Assert.assertEquals(expectedText, HtmlUnescapeUtils.unescapeWithUnbescape(escapedText));
42+
}
43+
44+
}

0 commit comments

Comments
 (0)