diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java index 39b385396..de12a9216 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java @@ -83,18 +83,23 @@ public class XWPFSDTContent { public String getText(){ StringBuilder text = new StringBuilder(); + boolean addNewLine = false; for (int i = 0; i < bodyElements.size(); i++){ Object o = bodyElements.get(i); if (o instanceof XWPFParagraph){ text.append(((XWPFParagraph)o).getText()); + addNewLine = true; } else if (o instanceof XWPFTable){ text.append(((XWPFTable)o).getText()); + addNewLine = true; } else if (o instanceof XWPFSDT){ text.append(((XWPFSDT)o).getContent().getText()); + addNewLine = true; } else if (o instanceof XWPFRun){ text.append(((XWPFRun)o).toString()); + addNewLine = false; } - if (i < bodyElements.size()-1){ + if (addNewLine == true && i < bodyElements.size()-1){ text.append("\n"); } } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java index 2359beac2..f1a585567 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java @@ -93,6 +93,31 @@ public final class TestXWPFSDT extends TestCase { } assertEquals("SDT as cell known failure", false, found); } + + /** + * POI-55142 and Tika 1130 + */ + public void testNewLinesBetweenRuns() throws Exception{ + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug55142.docx"); + List sdts = extractAllSDTs(doc); + List targs = new ArrayList(); + //these test newlines and tabs in paragraphs/body elements + targs.add("Rich-text1 abcdefghi"); + targs.add("Rich-text2 abcd\t\tefgh"); + targs.add("Rich-text3 abcd\nefg"); + targs.add("Rich-text4 abcdefg"); + targs.add("Rich-text5 abcdefg\nhijk"); + targs.add("Plain-text1 abcdefg"); + targs.add("Plain-text2 abcdefg\nhijk\nlmnop"); + //this tests consecutive runs within a cell (not a paragraph) + //this test case was triggered by Tika-1130 + targs.add("sdt_incell2 abcdefg"); + + for (int i = 0; i < sdts.size(); i++){ + XWPFSDT sdt = sdts.get(i); + assertEquals(targs.get(i), targs.get(i), sdt.getContent().getText()); + } + } private List extractAllSDTs(XWPFDocument doc){ diff --git a/test-data/document/Bug55142.docx b/test-data/document/Bug55142.docx new file mode 100644 index 000000000..1ae2adafe Binary files /dev/null and b/test-data/document/Bug55142.docx differ