From 8428670c2cf1bc81264e160a37b3c1fe64e368a9 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 25 Nov 2009 12:37:48 +0000 Subject: [PATCH] Add support for creating SummaryInformation and DocumentSummaryInformation properties on POIDocuments that don't have them, via POIDocument.createInformationProperties() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@884072 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + src/java/org/apache/poi/POIDocument.java | 19 ++++++ .../org/apache/poi/TestPOIDocumentMain.java | 66 +++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 5369cda040..bda9cecca0 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 15571601b4..dacc9235ca 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 47f2c0be97..f6e0a1e9e5 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()); + } } -- 2.39.5