From f333bf18c86bc934487b750d89fdb4d4d1ab2524 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 18 Mar 2014 14:26:28 +0000 Subject: [PATCH] Support adding tabs to a XWPFRun, and add unit tests on toString() for addTab and addCarriageReturn git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1578921 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xwpf/usermodel/XWPFRun.java | 18 ++++++++++++--- .../poi/xwpf/usermodel/TestXWPFRun.java | 23 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index 87932534c8..b1f9c62863 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -712,6 +712,18 @@ public class XWPFRun implements ISDTContents, IRunElement{ br.setClear(STBrClear.Enum.forInt(clear.getValue())); } + /** + * Specifies that a tab shall be placed at the current location in + * the run content. + */ + public void addTab() { + run.addNewTab(); + } + + public void removeTab() { + //TODO + } + /** * Specifies that a carriage return shall be placed at the * current location in the run content. @@ -897,13 +909,13 @@ public class XWPFRun implements ISDTContents, IRunElement{ // This bit works around it, and replicates the above // rules for that case String tagName = o.getDomNode().getNodeName(); - if ("w:tab".equals(tagName)) { + if ("w:tab".equals(tagName) || "tab".equals(tagName)) { text.append("\t"); } - if ("w:br".equals(tagName)) { + if ("w:br".equals(tagName) || "br".equals(tagName)) { text.append("\n"); } - if ("w:cr".equals(tagName)) { + if ("w:cr".equals(tagName) || "cr".equals(tagName)) { text.append("\n"); } } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java index dd4f27174e..d3c42e912f 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -181,6 +181,29 @@ public class TestXWPFRun extends TestCase { run.setText("T2"); run.addCarriageReturn(); assertEquals(3, run.getCTR().getCrList().size()); + + assertEquals("T1\n\nT2\n", run.toString()); + } + + public void testAddTabsAndLineBreaks() { + ctRun.addNewT().setStringValue("TEST STRING"); + ctRun.addNewCr(); + ctRun.addNewT().setStringValue("TEST2 STRING"); + ctRun.addNewTab(); + ctRun.addNewT().setStringValue("TEST3 STRING"); + assertEquals(1, ctRun.sizeOfCrArray()); + assertEquals(1, ctRun.sizeOfTabArray()); + + XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), p); + run.setText("T1"); + run.addCarriageReturn(); + run.setText("T2"); + run.addTab(); + run.setText("T3"); + assertEquals(1, run.getCTR().getCrList().size()); + assertEquals(1, run.getCTR().getTabList().size()); + + assertEquals("T1\nT2\tT3", run.toString()); } public void testAddPageBreak() { -- 2.39.5