From 51255a17f752eaa655b2e6a209ecff90feda6121 Mon Sep 17 00:00:00 2001 From: nuviosoftware Date: Sat, 27 Jan 2018 16:58:12 +0000 Subject: [PATCH] fix bug with html parser class cast exception on empty content --- .../com/lowagie/text/html/HtmlParserTest.java | 38 +++++++++++++++++++ .../com/lowagie/text/xml/SAXiTextHandler.java | 6 ++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 pdf-html/src/test/java/com/lowagie/text/html/HtmlParserTest.java diff --git a/pdf-html/src/test/java/com/lowagie/text/html/HtmlParserTest.java b/pdf-html/src/test/java/com/lowagie/text/html/HtmlParserTest.java new file mode 100644 index 000000000..462897ace --- /dev/null +++ b/pdf-html/src/test/java/com/lowagie/text/html/HtmlParserTest.java @@ -0,0 +1,38 @@ +package com.lowagie.text.html; + +import static org.junit.Assert.assertNotNull; + +import java.io.StringReader; + +import org.junit.Test; + +import com.lowagie.text.Document; +import com.lowagie.text.TextElementArray; + +public class HtmlParserTest { + + /** + * Bug fix scenario: a table with a space throws a {@link TextElementArray} + * class cast Exception. A table without spaces is parsed correctly. + */ + @Test + public void testParse_tableWithNoSpaces() { + Document doc1 = new Document(); + doc1.open(); + HtmlParser.parse(doc1, new StringReader("
test
")); // succeeds + assertNotNull(doc1); + } + + /** + * Bug fix scenario: a table with a space throws a {@link TextElementArray} + * class cast Exception. + */ + @Test + public void testParse_tableWithSpaces() { + Document doc1 = new Document(); + doc1.open(); + HtmlParser.parse(doc1, new StringReader("
test
")); // fails + assertNotNull(doc1); + } + +} diff --git a/pdf-xml/src/main/java/com/lowagie/text/xml/SAXiTextHandler.java b/pdf-xml/src/main/java/com/lowagie/text/xml/SAXiTextHandler.java index 8f638b2dc..ce0d4c1dc 100644 --- a/pdf-xml/src/main/java/com/lowagie/text/xml/SAXiTextHandler.java +++ b/pdf-xml/src/main/java/com/lowagie/text/xml/SAXiTextHandler.java @@ -206,6 +206,10 @@ public void startElement(String uri, String lname, String name, handleStartingTags(name, attributes); } + private boolean isNotBlank(String text) { + return text != null && !text.trim().isEmpty(); + } + /** * This method deals with the starting tags. * @@ -223,7 +227,7 @@ public void handleStartingTags(String name, Properties attributes) { } // maybe there is some meaningful data that wasn't between tags - if (currentChunk != null) { + if (currentChunk != null && isNotBlank(currentChunk.getContent())) { TextElementArray current; try { current = (TextElementArray) stack.pop();