Skip to content

Commit 97c21e3

Browse files
committed
[bug-63774] adding lots of custom properties can cause performance issues due to way Pid is calculated
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1867597 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3d3c846 commit 97c21e3

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkbookProperties.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ public static void main(String[]args) throws IOException {
3434

3535
POIXMLProperties props = workbook.getProperties();
3636

37-
/*
38-
* Extended properties are a predefined set of metadata properties
39-
* that are specifically applicable to Office Open XML documents.
40-
* Extended properties consist of 24 simple properties and 3 complex properties stored in the
41-
* part targeted by the relationship of type
42-
*/
37+
/*
38+
* Extended properties are a predefined set of metadata properties
39+
* that are specifically applicable to Office Open XML documents.
40+
* Extended properties consist of 24 simple properties and 3 complex properties stored in the
41+
* part targeted by the relationship of type
42+
*/
4343
POIXMLProperties.ExtendedProperties ext = props.getExtendedProperties();
4444
ext.getUnderlyingProperties().setCompany("Apache Software Foundation");
4545
ext.getUnderlyingProperties().setTemplate("XSSF");
4646

47-
/*
48-
* Custom properties enable users to define custom metadata properties.
49-
*/
47+
/*
48+
* Custom properties enable users to define custom metadata properties.
49+
*/
5050

5151
POIXMLProperties.CustomProperties cust = props.getCustomProperties();
5252
cust.addProperty("Author", "John Smith");

src/ooxml/java/org/apache/poi/ooxml/POIXMLProperties.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,8 @@ public static class CustomProperties {
565565
public static final String FORMAT_ID = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}";
566566

567567
private org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument props;
568+
private Integer lastPid = null;
569+
568570
private CustomProperties(org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument props) {
569571
this.props = props;
570572
}
@@ -651,11 +653,23 @@ public void addProperty(String name, boolean value){
651653
* @return next property id starting with 2
652654
*/
653655
protected int nextPid() {
656+
int propid = lastPid == null ? getLastPid() : lastPid;
657+
int nextid = propid + 1;
658+
this.lastPid = nextid;
659+
return nextid;
660+
}
661+
662+
/**
663+
* Find the highest Pid in use
664+
*
665+
* @return the highest Pid in use in the property set; returns 1 if no properties are set
666+
*/
667+
protected int getLastPid() {
654668
int propid = 1;
655669
for(CTProperty p : props.getProperties().getPropertyList()) {
656670
if(p.getPid() > propid) propid = p.getPid();
657671
}
658-
return propid + 1;
672+
return propid;
659673
}
660674

661675
/**

src/ooxml/testcases/org/apache/poi/ooxml/TestPOIXMLProperties.java

+12
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,18 @@ private static String zeroPad(long i) {
315315
}
316316
}
317317

318+
@Test
319+
public void testAddProperty() throws IOException {
320+
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("documentProperties.docx")) {
321+
POIXMLProperties.CustomProperties cps = doc.getProperties().getCustomProperties();
322+
assertEquals(1, cps.getLastPid());
323+
cps.addProperty("prop1", "abc");
324+
assertEquals(2, cps.getLastPid());
325+
assertEquals(2, cps.getProperty("prop1").getPid());
326+
assertEquals("abc", cps.getProperty("prop1").getLpwstr());
327+
}
328+
}
329+
318330
@Test
319331
public void testBug60977() throws IOException {
320332

src/testcases/org/apache/poi/hpsf/extractor/TestHPSFPropertiesExtractor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public void testNormalUnicodeProperties() throws Exception {
8686
public void testCustomProperties() throws Exception {
8787
try (InputStream is = _samples.openResourceAsStream("TestMickey.doc");
8888
POIFSFileSystem fs = new POIFSFileSystem(is);
89-
HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs)) {
89+
HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs)) {
90+
9091
// Custom properties are part of the document info stream
9192
String dinfText = ext.getDocumentSummaryInformationText();
9293
assertContains(dinfText, "Client = sample client");

0 commit comments

Comments
 (0)