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
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;
/**
// 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());
+ }
}