diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 5369cda04..bda9cecca 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Add support for creating SummaryInformation and DocumentSummaryInformation properties on POIDocuments that don't have them, via POIDocument.createInformationProperties() 48180 - be more forgiving of short chart records, which skip some unused fields 48274 - fix erronious wrapping of byte colours in HSSFPalette.findSimilarColor 48269 - fix fetching of error codes from XSSF formula cells diff --git a/src/java/org/apache/poi/POIDocument.java b/src/java/org/apache/poi/POIDocument.java index 15571601b..dacc9235c 100644 --- a/src/java/org/apache/poi/POIDocument.java +++ b/src/java/org/apache/poi/POIDocument.java @@ -85,6 +85,25 @@ public abstract class POIDocument { if(!initialized) readProperties(); return sInf; } + + /** + * Will create whichever of SummaryInformation + * and DocumentSummaryInformation (HPSF) properties + * are not already part of your document. + * This is normally useful when creating a new + * document from scratch. + * If the information properties are already there, + * then nothing will happen. + */ + public void createInformationProperties() { + if(!initialized) readProperties(); + if(sInf == null) { + sInf = PropertySetFactory.newSummaryInformation(); + } + if(dsInf == null) { + dsInf = PropertySetFactory.newDocumentSummaryInformation(); + } + } /** * Find, and create objects for, the standard diff --git a/src/testcases/org/apache/poi/TestPOIDocumentMain.java b/src/testcases/org/apache/poi/TestPOIDocumentMain.java index 47f2c0be9..f6e0a1e9e 100644 --- a/src/testcases/org/apache/poi/TestPOIDocumentMain.java +++ b/src/testcases/org/apache/poi/TestPOIDocumentMain.java @@ -20,10 +20,12 @@ package org.apache.poi; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import junit.framework.TestCase; import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** @@ -104,4 +106,68 @@ public final class TestPOIDocumentMain extends TestCase { // Delegate test testReadProperties(); } + + public void testCreateNewProperties() throws IOException { + POIDocument doc = new HSSFWorkbook(); + + // New document won't have them + assertNull(doc.getSummaryInformation()); + assertNull(doc.getDocumentSummaryInformation()); + + // Add them in + doc.createInformationProperties(); + assertNotNull(doc.getSummaryInformation()); + assertNotNull(doc.getDocumentSummaryInformation()); + + // Write out and back in again, no change + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + doc.write(baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + + doc = new HSSFWorkbook(bais); + + assertNotNull(doc.getSummaryInformation()); + assertNotNull(doc.getDocumentSummaryInformation()); + } + + public void testCreateNewPropertiesOnExistingFile() throws IOException { + POIDocument doc = new HSSFWorkbook(); + + // New document won't have them + assertNull(doc.getSummaryInformation()); + assertNull(doc.getDocumentSummaryInformation()); + + // Write out and back in again, no change + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + doc.write(baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + doc = new HSSFWorkbook(bais); + + assertNull(doc.getSummaryInformation()); + assertNull(doc.getDocumentSummaryInformation()); + + // Create, and change + doc.createInformationProperties(); + doc.getSummaryInformation().setAuthor("POI Testing"); + doc.getDocumentSummaryInformation().setCompany("ASF"); + + // Save and re-load + baos = new ByteArrayOutputStream(); + doc.write(baos); + bais = new ByteArrayInputStream(baos.toByteArray()); + doc = new HSSFWorkbook(bais); + + // Check + assertNotNull(doc.getSummaryInformation()); + assertNotNull(doc.getDocumentSummaryInformation()); + assertEquals("POI Testing", doc.getSummaryInformation().getAuthor()); + assertEquals("ASF", doc.getDocumentSummaryInformation().getCompany()); + + // Asking to re-create will make no difference now + doc.createInformationProperties(); + assertNotNull(doc.getSummaryInformation()); + assertNotNull(doc.getDocumentSummaryInformation()); + assertEquals("POI Testing", doc.getSummaryInformation().getAuthor()); + assertEquals("ASF", doc.getDocumentSummaryInformation().getCompany()); + } }