Create a new Superclass, POIDocument, which handles the property (hpsf) stuff which was previously done by HSLFSlideShow. Add tests for this, and convert HSLFSlideShow to using it
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@391363 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
59121bead0
commit
7fa4feb4bb
128
src/scratchpad/src/org/apache/poi/POIDocument.java
Normal file
128
src/scratchpad/src/org/apache/poi/POIDocument.java
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
/* ====================================================================
|
||||||
|
Copyright 2002-2004 Apache Software Foundation
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
==================================================================== */
|
||||||
|
|
||||||
|
package org.apache.poi;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.poi.hpsf.DocumentSummaryInformation;
|
||||||
|
import org.apache.poi.hpsf.MutablePropertySet;
|
||||||
|
import org.apache.poi.hpsf.PropertySet;
|
||||||
|
import org.apache.poi.hpsf.PropertySetFactory;
|
||||||
|
import org.apache.poi.hpsf.SummaryInformation;
|
||||||
|
import org.apache.poi.poifs.filesystem.DocumentInputStream;
|
||||||
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This holds the common functionality for all POI
|
||||||
|
* Document classes.
|
||||||
|
* Currently, this relates to Document Information Properties
|
||||||
|
*
|
||||||
|
* @author Nick Burch
|
||||||
|
*/
|
||||||
|
public abstract class POIDocument {
|
||||||
|
// Holds metadata on our document
|
||||||
|
protected SummaryInformation sInf;
|
||||||
|
protected DocumentSummaryInformation dsInf;
|
||||||
|
|
||||||
|
protected POIFSFileSystem filesystem;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the Document Summary Information of the document
|
||||||
|
*/
|
||||||
|
public DocumentSummaryInformation getDocumentSummaryInformation() { return dsInf; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the Summary Information of the document
|
||||||
|
*/
|
||||||
|
public SummaryInformation getSummaryInformation() { return sInf; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find, and create objects for, the standard
|
||||||
|
* Documment Information Properties (hpsf)
|
||||||
|
*/
|
||||||
|
protected void readProperties() {
|
||||||
|
// DocumentSummaryInformation
|
||||||
|
dsInf = (DocumentSummaryInformation)getPropertySet("\005DocumentSummaryInformation");
|
||||||
|
|
||||||
|
// SummaryInformation
|
||||||
|
sInf = (SummaryInformation)getPropertySet("\005SummaryInformation");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For a given named property entry, either return it or null if
|
||||||
|
* if it wasn't found
|
||||||
|
*/
|
||||||
|
protected PropertySet getPropertySet(String setName) {
|
||||||
|
DocumentInputStream dis;
|
||||||
|
try {
|
||||||
|
// Find the entry, and get an input stream for it
|
||||||
|
dis = filesystem.createDocumentInputStream(setName);
|
||||||
|
} catch(IOException ie) {
|
||||||
|
// Oh well, doesn't exist
|
||||||
|
System.err.println("Error getting property set with name " + setName + "\n" + ie);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Create the Property Set
|
||||||
|
PropertySet set = PropertySetFactory.create(dis);
|
||||||
|
return set;
|
||||||
|
} catch(IOException ie) {
|
||||||
|
// Must be corrupt or something like that
|
||||||
|
System.err.println("Error creating property set with name " + setName + "\n" + ie);
|
||||||
|
} catch(org.apache.poi.hpsf.HPSFException he) {
|
||||||
|
// Oh well, doesn't exist
|
||||||
|
System.err.println("Error creating property set with name " + setName + "\n" + he);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes out the standard Documment Information Properties (hpsf)
|
||||||
|
* @param outFS the POIFSFileSystem to write the properties into
|
||||||
|
*/
|
||||||
|
protected void writeProperties(POIFSFileSystem outFS) throws IOException {
|
||||||
|
if(sInf != null) {
|
||||||
|
writePropertySet("\005SummaryInformation",sInf,outFS);
|
||||||
|
}
|
||||||
|
if(dsInf != null) {
|
||||||
|
writePropertySet("\005DocumentSummaryInformation",dsInf,outFS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes out a given ProperySet
|
||||||
|
* @param name the (POIFS Level) name of the property to write
|
||||||
|
* @param set the PropertySet to write out
|
||||||
|
* @param outFS the POIFSFileSystem to write the property into
|
||||||
|
*/
|
||||||
|
protected void writePropertySet(String name, PropertySet set, POIFSFileSystem outFS) throws IOException {
|
||||||
|
try {
|
||||||
|
MutablePropertySet mSet = new MutablePropertySet(set);
|
||||||
|
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
||||||
|
mSet.write(bOut);
|
||||||
|
byte[] data = bOut.toByteArray();
|
||||||
|
ByteArrayInputStream bIn = new ByteArrayInputStream(data);
|
||||||
|
outFS.createDocument(bIn,name);
|
||||||
|
System.out.println("Wrote property set " + name + " of size " + data.length);
|
||||||
|
} catch(org.apache.poi.hpsf.WritingNotSupportedException wnse) {
|
||||||
|
System.err.println("Couldn't write property set with name " + name + " as not supported by HPSF yet");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -22,6 +22,7 @@ package org.apache.poi.hslf;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
|
import org.apache.poi.POIDocument;
|
||||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||||
import org.apache.poi.poifs.filesystem.DocumentEntry;
|
import org.apache.poi.poifs.filesystem.DocumentEntry;
|
||||||
import org.apache.poi.poifs.filesystem.DocumentInputStream;
|
import org.apache.poi.poifs.filesystem.DocumentInputStream;
|
||||||
@ -42,14 +43,11 @@ import org.apache.poi.hslf.usermodel.PictureData;
|
|||||||
* @author Nick Burch
|
* @author Nick Burch
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class HSLFSlideShow
|
public class HSLFSlideShow extends POIDocument
|
||||||
{
|
{
|
||||||
private InputStream istream;
|
private InputStream istream;
|
||||||
private POIFSFileSystem filesystem;
|
|
||||||
|
|
||||||
// Holds metadata on our document
|
// Holds metadata on where things are in our document
|
||||||
private SummaryInformation sInf;
|
|
||||||
private DocumentSummaryInformation dsInf;
|
|
||||||
private CurrentUserAtom currentUser;
|
private CurrentUserAtom currentUser;
|
||||||
|
|
||||||
// Low level contents of the file
|
// Low level contents of the file
|
||||||
@ -105,6 +103,9 @@ public class HSLFSlideShow
|
|||||||
// Look for Property Streams:
|
// Look for Property Streams:
|
||||||
readProperties();
|
readProperties();
|
||||||
|
|
||||||
|
// Look for other streams
|
||||||
|
readOtherStreams();
|
||||||
|
|
||||||
// Look for Picture Streams:
|
// Look for Picture Streams:
|
||||||
readPictures();
|
readPictures();
|
||||||
}
|
}
|
||||||
@ -186,15 +187,10 @@ public class HSLFSlideShow
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the properties from the filesystem, and load them
|
* Find the other from the filesystem (currently just CurrentUserAtom),
|
||||||
|
* and load them
|
||||||
*/
|
*/
|
||||||
public void readProperties() {
|
public void readOtherStreams() {
|
||||||
// DocumentSummaryInformation
|
|
||||||
dsInf = (DocumentSummaryInformation)getPropertySet("\005DocumentSummaryInformation");
|
|
||||||
|
|
||||||
// SummaryInformation
|
|
||||||
sInf = (SummaryInformation)getPropertySet("\005SummaryInformation");
|
|
||||||
|
|
||||||
// Current User
|
// Current User
|
||||||
try {
|
try {
|
||||||
currentUser = new CurrentUserAtom(filesystem);
|
currentUser = new CurrentUserAtom(filesystem);
|
||||||
@ -233,36 +229,6 @@ public class HSLFSlideShow
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For a given named property entry, either return it or null if
|
|
||||||
* if it wasn't found
|
|
||||||
*/
|
|
||||||
public PropertySet getPropertySet(String setName) {
|
|
||||||
DocumentInputStream dis;
|
|
||||||
try {
|
|
||||||
// Find the entry, and get an input stream for it
|
|
||||||
dis = filesystem.createDocumentInputStream(setName);
|
|
||||||
} catch(IOException ie) {
|
|
||||||
// Oh well, doesn't exist
|
|
||||||
System.err.println("Error getting property set with name " + setName + "\n" + ie);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Create the Property Set
|
|
||||||
PropertySet set = PropertySetFactory.create(dis);
|
|
||||||
return set;
|
|
||||||
} catch(IOException ie) {
|
|
||||||
// Must be corrupt or something like that
|
|
||||||
System.err.println("Error creating property set with name " + setName + "\n" + ie);
|
|
||||||
} catch(org.apache.poi.hpsf.HPSFException he) {
|
|
||||||
// Oh well, doesn't exist
|
|
||||||
System.err.println("Error creating property set with name " + setName + "\n" + he);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes out the slideshow file the is represented by an instance of
|
* Writes out the slideshow file the is represented by an instance of
|
||||||
* this class
|
* this class
|
||||||
@ -275,12 +241,7 @@ public class HSLFSlideShow
|
|||||||
POIFSFileSystem outFS = new POIFSFileSystem();
|
POIFSFileSystem outFS = new POIFSFileSystem();
|
||||||
|
|
||||||
// Write out the Property Streams
|
// Write out the Property Streams
|
||||||
if(sInf != null) {
|
writeProperties(outFS);
|
||||||
writePropertySet("\005SummaryInformation",sInf,outFS);
|
|
||||||
}
|
|
||||||
if(dsInf != null) {
|
|
||||||
writePropertySet("\005DocumentSummaryInformation",dsInf,outFS);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// For position dependent records, hold where they were and now are
|
// For position dependent records, hold where they were and now are
|
||||||
@ -342,24 +303,6 @@ public class HSLFSlideShow
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes out a given ProperySet
|
|
||||||
*/
|
|
||||||
private void writePropertySet(String name, PropertySet set, POIFSFileSystem fs) throws IOException {
|
|
||||||
try {
|
|
||||||
MutablePropertySet mSet = new MutablePropertySet(set);
|
|
||||||
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
|
||||||
mSet.write(bOut);
|
|
||||||
byte[] data = bOut.toByteArray();
|
|
||||||
ByteArrayInputStream bIn = new ByteArrayInputStream(data);
|
|
||||||
fs.createDocument(bIn,name);
|
|
||||||
System.out.println("Wrote property set " + name + " of size " + data.length);
|
|
||||||
} catch(org.apache.poi.hpsf.WritingNotSupportedException wnse) {
|
|
||||||
System.err.println("Couldn't write property set with name " + name + " as not supported by HPSF yet");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* ******************* adding methods follow ********************* */
|
/* ******************* adding methods follow ********************* */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -418,16 +361,6 @@ public class HSLFSlideShow
|
|||||||
*/
|
*/
|
||||||
public byte[] getUnderlyingBytes() { return _docstream; }
|
public byte[] getUnderlyingBytes() { return _docstream; }
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch the Document Summary Information of the document
|
|
||||||
*/
|
|
||||||
public DocumentSummaryInformation getDocumentSummaryInformation() { return dsInf; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch the Summary Information of the document
|
|
||||||
*/
|
|
||||||
public SummaryInformation getSummaryInformation() { return sInf; }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch the Current User Atom of the document
|
* Fetch the Current User Atom of the document
|
||||||
*/
|
*/
|
||||||
|
95
src/scratchpad/testcases/org/apache/poi/TestPOIDocument.java
Normal file
95
src/scratchpad/testcases/org/apache/poi/TestPOIDocument.java
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
|
||||||
|
/* ====================================================================
|
||||||
|
Copyright 2002-2004 Apache Software Foundation
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
==================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
package org.apache.poi;
|
||||||
|
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
import org.apache.poi.hslf.HSLFSlideShow;
|
||||||
|
import org.apache.poi.poifs.filesystem.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that POIDocument correctly loads and saves the common
|
||||||
|
* (hspf) Document Properties
|
||||||
|
*
|
||||||
|
* @author Nick Burch (nick at torchbox dot com)
|
||||||
|
*/
|
||||||
|
public class TestPOIDocument extends TestCase {
|
||||||
|
// The POI Document to work on
|
||||||
|
private POIDocument doc;
|
||||||
|
// POIFS primed on the test (powerpoint) data
|
||||||
|
private POIFSFileSystem pfs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set things up, using a PowerPoint document for our testing
|
||||||
|
*/
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
String dirname = System.getProperty("HSLF.testdata.path");
|
||||||
|
String filename = dirname + "/basic_test_ppt_file.ppt";
|
||||||
|
FileInputStream fis = new FileInputStream(filename);
|
||||||
|
pfs = new POIFSFileSystem(fis);
|
||||||
|
doc = new HSLFSlideShow(pfs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testReadProperties() throws Exception {
|
||||||
|
// We should have both sets
|
||||||
|
assertNotNull(doc.getDocumentSummaryInformation());
|
||||||
|
assertNotNull(doc.getSummaryInformation());
|
||||||
|
|
||||||
|
// Check they are as expected for the test doc
|
||||||
|
assertEquals("Hogwarts", doc.getSummaryInformation().getAuthor());
|
||||||
|
assertEquals(10598, doc.getDocumentSummaryInformation().getByteCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testWriteProperties() throws Exception {
|
||||||
|
// Just check we can write them back out into a filesystem
|
||||||
|
POIFSFileSystem outFS = new POIFSFileSystem();
|
||||||
|
doc.writeProperties(outFS);
|
||||||
|
|
||||||
|
// Should now hold them
|
||||||
|
assertNotNull(
|
||||||
|
outFS.createDocumentInputStream("\005SummaryInformation")
|
||||||
|
);
|
||||||
|
assertNotNull(
|
||||||
|
outFS.createDocumentInputStream("\005DocumentSummaryInformation")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testWriteReadProperties() throws Exception {
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
// Write them out
|
||||||
|
POIFSFileSystem outFS = new POIFSFileSystem();
|
||||||
|
doc.writeProperties(outFS);
|
||||||
|
outFS.writeFilesystem(baos);
|
||||||
|
|
||||||
|
// Create a new version
|
||||||
|
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||||
|
POIFSFileSystem inFS = new POIFSFileSystem(bais);
|
||||||
|
|
||||||
|
// Check they're still there
|
||||||
|
doc.filesystem = inFS;
|
||||||
|
doc.readProperties();
|
||||||
|
|
||||||
|
// Delegate test
|
||||||
|
testReadProperties();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user