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
This commit is contained in:
Nick Burch 2009-11-25 12:37:48 +00:00
parent 4bcde19ada
commit 6602faf140
3 changed files with 86 additions and 0 deletions

View 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>

View File

@ -86,6 +86,25 @@ public abstract class POIDocument {
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
* Documment Information Properties (HPSF).

View 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());
}
}