Patch from Tim Allison from bug #55142 - Not all XWPF SDT block

s need newlines

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1496458 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2013-06-25 13:09:08 +00:00
parent c1d02cd0f5
commit 678991d868
3 changed files with 31 additions and 1 deletions

View File

@ -83,18 +83,23 @@ public class XWPFSDTContent {
public String getText(){ public String getText(){
StringBuilder text = new StringBuilder(); StringBuilder text = new StringBuilder();
boolean addNewLine = false;
for (int i = 0; i < bodyElements.size(); i++){ for (int i = 0; i < bodyElements.size(); i++){
Object o = bodyElements.get(i); Object o = bodyElements.get(i);
if (o instanceof XWPFParagraph){ if (o instanceof XWPFParagraph){
text.append(((XWPFParagraph)o).getText()); text.append(((XWPFParagraph)o).getText());
addNewLine = true;
} else if (o instanceof XWPFTable){ } else if (o instanceof XWPFTable){
text.append(((XWPFTable)o).getText()); text.append(((XWPFTable)o).getText());
addNewLine = true;
} else if (o instanceof XWPFSDT){ } else if (o instanceof XWPFSDT){
text.append(((XWPFSDT)o).getContent().getText()); text.append(((XWPFSDT)o).getContent().getText());
addNewLine = true;
} else if (o instanceof XWPFRun){ } else if (o instanceof XWPFRun){
text.append(((XWPFRun)o).toString()); text.append(((XWPFRun)o).toString());
addNewLine = false;
} }
if (i < bodyElements.size()-1){ if (addNewLine == true && i < bodyElements.size()-1){
text.append("\n"); text.append("\n");
} }
} }

View File

@ -94,6 +94,31 @@ public final class TestXWPFSDT extends TestCase {
assertEquals("SDT as cell known failure", false, found); 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<XWPFSDT> sdts = extractAllSDTs(doc);
List<String> targs = new ArrayList<String>();
//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<XWPFSDT> extractAllSDTs(XWPFDocument doc){ private List<XWPFSDT> extractAllSDTs(XWPFDocument doc){
List<XWPFSDT> sdts = new ArrayList<XWPFSDT>(); List<XWPFSDT> sdts = new ArrayList<XWPFSDT>();

Binary file not shown.