You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2026/10/03 19:30:17 UTC
(poi) branch trunk updated: Load header and footer references of a section in linear time (#1358)
This is an automated email from the ASF dual-hosted git repository.
pjfanning 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 112a02d10e Load header and footer references of a section in linear time (#1358)
112a02d10e is described below
commit 112a02d10ed79d7ebd58454da6935a9ab1f90fd9
Author: Bernd Engelhardt <be...@softvision.de>
AuthorDate: Sat Oct 3 21:30:11 2026 +0200
Load header and footer references of a section in linear time (#1358)
XWPFHeaderFooterPolicy iterated the header and footer references of a
section with sizeOfHeaderReferenceArray() and getHeaderReferenceArray(i).
XmlBeans counts or walks the children of the element on every such call,
so opening a document became quadratic in the number of references: a
section with 40,000 references (11 KB .docx) took 19 s to load.
Iterate over getHeaderReferenceArray() / getFooterReferenceArray()
instead, which collects the children in one pass. The references are
visited in the same order, so the last reference of a type still wins.
Co-authored-by: Bernd Engelhardt <29...@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <no...@anthropic.com>
---
.../poi/xwpf/model/XWPFHeaderFooterPolicy.java | 8 +++----
.../poi/xwpf/model/TestXWPFHeaderFooterPolicy.java | 27 ++++++++++++++++++++++
2 files changed, 31 insertions(+), 4 deletions(-)
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
index a18767921b..f88f027875 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
@@ -105,9 +105,10 @@ public class XWPFHeaderFooterPolicy {
this.doc = doc;
this.sectPr = sectPr;
- for (int i = 0; i < sectPr.sizeOfHeaderReferenceArray(); i++) {
+ // Iterate over the array: XmlBeans counts or walks the children on every indexed access,
+ // which made an index loop quadratic in the number of references
+ for (CTHdrFtrRef ref : sectPr.getHeaderReferenceArray()) {
// Get the header
- CTHdrFtrRef ref = sectPr.getHeaderReferenceArray(i);
POIXMLDocumentPart relatedPart = doc.getRelationById(ref.getId());
XWPFHeader hdr = null;
if (relatedPart instanceof XWPFHeader header) {
@@ -123,9 +124,8 @@ public class XWPFHeaderFooterPolicy {
assignHeader(hdr, type);
}
- for (int i = 0; i < sectPr.sizeOfFooterReferenceArray(); i++) {
+ for (CTHdrFtrRef ref : sectPr.getFooterReferenceArray()) {
// Get the footer
- CTHdrFtrRef ref = sectPr.getFooterReferenceArray(i);
POIXMLDocumentPart relatedPart = doc.getRelationById(ref.getId());
XWPFFooter ftr = null;
if (relatedPart instanceof XWPFFooter footer) {
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java
index 41838b704a..099669d8e8 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java
@@ -24,6 +24,8 @@ import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr;
import java.io.IOException;
@@ -204,4 +206,29 @@ class TestXWPFHeaderFooterPolicy {
policy.getEvenPageHeader().getText()
);
}
+
+ /**
+ * A section may state several references of one type; the last one wins. Pins the order in which the
+ * references of a section are read.
+ */
+ @Test
+ void testLastReferenceOfATypeWins() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ XWPFHeaderFooterPolicy policy = doc.createHeaderFooterPolicy();
+ policy.createHeader(STHdrFtr.DEFAULT).createParagraph().createRun().setText("default");
+ XWPFHeader first = policy.createHeader(STHdrFtr.FIRST);
+ first.createParagraph().createRun().setText("first");
+ // a second default reference, to the header created for the first page
+ CTHdrFtrRef ref = doc.getDocument().getBody().getSectPr().addNewHeaderReference();
+ ref.setType(STHdrFtr.DEFAULT);
+ ref.setId(doc.getRelationId(first));
+
+ try (XWPFDocument back = XWPFTestDataSamples.writeOutAndReadBack(doc)) {
+ XWPFHeaderFooterPolicy read = back.getHeaderFooterPolicy();
+ assertNotNull(read);
+ assertEquals("first", read.getDefaultHeader().getText().trim());
+ assertEquals("first", read.getFirstPageHeader().getText().trim());
+ }
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org