This is an automated email from the ASF dual-hosted git repository.
jaragunde pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new e0658769cb XWPF: add getters for break attributes in XWPFRun. (#1561)
e0658769cb is described below
commit e0658769cb7f7da25ec09178805e04069dedc6ed
Author: Jacobo Aragunde Pérez <[email protected]>
AuthorDate: Fri Nov 13 08:32:49 2026 +0100
XWPF: add getters for break attributes in XWPFRun. (#1561)
---
.../org/apache/poi/xwpf/usermodel/XWPFRun.java | 69 ++++++++++++++
.../org/apache/poi/xwpf/usermodel/TestXWPFRun.java | 100 +++++++++++++++++++++
2 files changed, 169 insertions(+)
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
index 2867a6638e..60882c3004 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
@@ -1226,6 +1226,75 @@ public class XWPFRun implements ISDTContents,
IRunElement, CharacterRun {
br.setClear(STBrClear.Enum.forInt(clear.getValue()));
}
+ /**
+ * Returns the number of breaks in the run content.
+ *
+ * @return number of break elements in this run
+ * @see #getBreak(int)
+ * @since 6.1.0
+ */
+ public int getNumberOfBreaks() {
+ return run.sizeOfBrArray();
+ }
+
+ /**
+ * Get the type of the break at the given position in the run content.
+ * <p>
+ * A run may contain any number of breaks, mixed with text, tabs and other
+ * run content. The position only counts break elements, in document
+ * order: it is not related to the positions used by text-related methods
+ * such as {@link #getText(int)}, so it does not tell where the break is
+ * placed relative to the text of the run.
+ * </p>
+ * <p>
+ * If the break does not specify a type, {@link BreakType#TEXT_WRAPPING}
+ * is returned, as that is the default value defined by the specification.
+ * </p>
+ *
+ * @param pos the 0-based position of the break among the breaks of this
+ * run, lower than {@link #getNumberOfBreaks()}
+ * @return the type of the break at the given position, or {@code null} if
+ * there is no break at that position
+ * @see #addBreak(BreakType)
+ * @since 6.1.0
+ */
+ public BreakType getBreak(int pos) {
+ if (pos < 0 || pos >= run.sizeOfBrArray()) {
+ return null;
+ }
+ STBrType.Enum type = run.getBrArray(pos).getType();
+ return type == null ? BreakType.TEXT_WRAPPING :
BreakType.valueOf(type.intValue());
+ }
+
+ /**
+ * Get the clear setting of the break at the given position in the run
+ * content.
+ * <p>
+ * The clear setting specifies where text restarts after a text wrapping
+ * break that is next to a floating object. The position only counts
+ * break elements, in document order, as in {@link #getBreak(int)}.
+ * </p>
+ * <p>
+ * If the break does not specify a clear setting, {@link BreakClear#NONE}
+ * is returned, as that is the default value defined by the specification.
+ * </p>
+ *
+ * @param pos the 0-based position of the break among the breaks of this
+ * run, lower than {@link #getNumberOfBreaks()}
+ * @return the clear setting of the break at the given position, or
+ * {@code null} if there is no break at that position
+ * @see #addBreak(BreakClear)
+ * @see #getBreak(int)
+ * @since 6.1.0
+ */
+ public BreakClear getBreakClear(int pos) {
+ if (pos < 0 || pos >= run.sizeOfBrArray()) {
+ return null;
+ }
+ STBrClear.Enum clear = run.getBrArray(pos).getClear();
+ return clear == null ? BreakClear.NONE :
BreakClear.valueOf(clear.intValue());
+ }
+
/**
* Specifies that a tab shall be placed at the current location in
* the run content.
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
index 698a0aefd9..0b1837dab5 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
@@ -58,6 +58,7 @@ import
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STEm;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STThemeColor;
@@ -368,6 +369,105 @@ class TestXWPFRun {
assertEquals(2, run.getCTR().sizeOfBrArray());
}
+ @Test
+ void testGetBreak() {
+ XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), irb);
+ assertEquals(0, run.getNumberOfBreaks());
+ assertNull(run.getBreak(0));
+
+ run.setText("TEXT1");
+ assertEquals(0, run.getNumberOfBreaks());
+ assertNull(run.getBreak(0));
+
+ // a break without a type defaults to text wrapping
+ run.addBreak();
+ assertEquals(1, run.getNumberOfBreaks());
+ assertEquals(BreakType.TEXT_WRAPPING, run.getBreak(0));
+ assertNull(run.getBreak(1));
+ assertNull(run.getBreak(-1));
+
+ // breaks are indexed among breaks only, regardless of other run
content
+ run.setText("TEXT2");
+ run.addTab();
+ run.addBreak(BreakType.PAGE);
+ run.setText("TEXT3");
+ run.addBreak(BreakType.COLUMN);
+ assertEquals(3, run.getNumberOfBreaks());
+ assertEquals(3, run.getNumberOfTexts());
+ assertEquals(BreakType.TEXT_WRAPPING, run.getBreak(0));
+ assertEquals(BreakType.PAGE, run.getBreak(1));
+ assertEquals(BreakType.COLUMN, run.getBreak(2));
+ assertNull(run.getBreak(3));
+
+ for (BreakType type : BreakType.values()) {
+ XWPFRun r = new XWPFRun(CTR.Factory.newInstance(), irb);
+ r.addBreak(type);
+ assertEquals(type, r.getBreak(0));
+ }
+
+ XWPFRun clearRun = new XWPFRun(CTR.Factory.newInstance(), irb);
+ clearRun.addBreak(BreakClear.ALL);
+ assertEquals(BreakType.TEXT_WRAPPING, clearRun.getBreak(0));
+
+ // breaks created directly in the XML
+ XWPFRun xmlRun = new XWPFRun(CTR.Factory.newInstance(), irb);
+ CTR ctr = xmlRun.getCTR();
+ ctr.addNewT().setStringValue("T1");
+ ctr.addNewBr().setType(STBrType.COLUMN);
+ ctr.addNewT().setStringValue("T2");
+ ctr.addNewBr();
+ assertEquals(2, xmlRun.getNumberOfBreaks());
+ assertEquals(BreakType.COLUMN, xmlRun.getBreak(0));
+ assertEquals(BreakType.TEXT_WRAPPING, xmlRun.getBreak(1));
+ assertNull(xmlRun.getBreak(2));
+ }
+
+ @Test
+ void testGetBreakClear() {
+ XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), irb);
+ assertNull(run.getBreakClear(0));
+
+ run.setText("TEXT1");
+ assertNull(run.getBreakClear(0));
+
+ // a break without a clear attribute defaults to none
+ run.addBreak();
+ assertEquals(BreakClear.NONE, run.getBreakClear(0));
+ assertNull(run.getBreakClear(1));
+ assertNull(run.getBreakClear(-1));
+
+ // breaks are indexed among breaks only, regardless of other run
content
+ run.setText("TEXT2");
+ run.addBreak(BreakType.PAGE);
+ run.addTab();
+ run.addBreak(BreakClear.RIGHT);
+ assertEquals(BreakClear.NONE, run.getBreakClear(0));
+ assertEquals(BreakClear.NONE, run.getBreakClear(1));
+ assertEquals(BreakClear.RIGHT, run.getBreakClear(2));
+ assertEquals(BreakType.TEXT_WRAPPING, run.getBreak(2));
+ assertNull(run.getBreakClear(3));
+
+ for (BreakClear clear : BreakClear.values()) {
+ XWPFRun r = new XWPFRun(CTR.Factory.newInstance(), irb);
+ r.addBreak(clear);
+ assertEquals(clear, r.getBreakClear(0));
+ }
+
+ // breaks created directly in the XML
+ XWPFRun xmlRun = new XWPFRun(CTR.Factory.newInstance(), irb);
+ CTR ctr = xmlRun.getCTR();
+ ctr.addNewBr().setClear(STBrClear.ALL);
+ ctr.addNewT().setStringValue("T1");
+ ctr.addNewBr().setClear(STBrClear.NONE);
+ CTBr br = ctr.addNewBr();
+ br.setType(STBrType.PAGE);
+ br.setClear(STBrClear.LEFT);
+ assertEquals(BreakClear.ALL, xmlRun.getBreakClear(0));
+ assertEquals(BreakClear.NONE, xmlRun.getBreakClear(1));
+ assertEquals(BreakClear.LEFT, xmlRun.getBreakClear(2));
+ assertEquals(BreakType.PAGE, xmlRun.getBreak(2));
+ }
+
/**
* Test that on an existing document, we do the
* right thing with it
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]