]> source.dussan.org Git - poi.git/commitdiff
Add support for creating SummaryInformation and DocumentSummaryInformation properties...
authorNick Burch <nick@apache.org>
Wed, 25 Nov 2009 12:37:48 +0000 (12:37 +0000)
committerNick Burch <nick@apache.org>
Wed, 25 Nov 2009 12:37:48 +0000 (12:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@884072 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/POIDocument.java
src/testcases/org/apache/poi/TestPOIDocumentMain.java

index 5369cda040ff771ebd698f22fdcfc0cd33ed10f5..bda9cecca0c25021bd80a73af9b2b4a010f43060 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.6-beta1" date="2009-??-??">
+           <action dev="POI-DEVELOPERS" type="add">Add support for creating SummaryInformation and DocumentSummaryInformation properties on POIDocuments that don't have them, via POIDocument.createInformationProperties()</action>
            <action dev="POI-DEVELOPERS" type="fix">48180 - be more forgiving of short chart records, which skip some unused fields</action>
            <action dev="POI-DEVELOPERS" type="fix">48274 - fix erronious wrapping of byte colours in HSSFPalette.findSimilarColor</action>
            <action dev="POI-DEVELOPERS" type="fix">48269 - fix fetching of error codes from XSSF formula cells</action>
index 15571601b45d2d7ae21d02371843058ddbd02d3c..dacc9235caa3991084dde122d8ff9da45d2cacb5 100644 (file)
@@ -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
index 47f2c0be979167da17998cfa966d5f96f7266afb..f6e0a1e9e5ea12ca0ad79a4c7dbad5ea3a235d8e 100644 (file)
@@ -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());
+       }
 }