You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by ni...@apache.org on 2006/04/04 18:50:06 UTC

svn commit: r391363 - in /jakarta/poi/trunk/src/scratchpad: src/org/apache/poi/POIDocument.java src/org/apache/poi/hslf/HSLFSlideShow.java testcases/org/apache/poi/TestPOIDocument.java

Author: nick
Date: Tue Apr  4 09:50:04 2006
New Revision: 391363

URL: http://svn.apache.org/viewcvs?rev=391363&view=rev
Log:
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

Added:
    jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/POIDocument.java
    jakarta/poi/trunk/src/scratchpad/testcases/org/apache/poi/TestPOIDocument.java
Modified:
    jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java

Added: jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/POIDocument.java
URL: http://svn.apache.org/viewcvs/jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/POIDocument.java?rev=391363&view=auto
==============================================================================
--- jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/POIDocument.java (added)
+++ jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/POIDocument.java Tue Apr  4 09:50:04 2006
@@ -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");
+		}
+	}
+}

Modified: jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java
URL: http://svn.apache.org/viewcvs/jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java?rev=391363&r1=391362&r2=391363&view=diff
==============================================================================
--- jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java (original)
+++ jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java Tue Apr  4 09:50:04 2006
@@ -22,6 +22,7 @@
 import java.util.*;
 import java.io.*;
 
+import org.apache.poi.POIDocument;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.poifs.filesystem.DocumentEntry;
 import org.apache.poi.poifs.filesystem.DocumentInputStream;
@@ -42,14 +43,11 @@
  * @author Nick Burch
  */
 
-public class HSLFSlideShow
+public class HSLFSlideShow extends POIDocument
 {
 	private InputStream istream;
-	private POIFSFileSystem filesystem;
 
-	// Holds metadata on our document
-	private SummaryInformation sInf;
-	private DocumentSummaryInformation dsInf;
+	// Holds metadata on where things are in our document
 	private CurrentUserAtom currentUser;
 
 	// Low level contents of the file
@@ -104,6 +102,9 @@
 
 		// Look for Property Streams:
 		readProperties();
+		
+		// Look for other streams
+		readOtherStreams();
 
 		// Look for Picture Streams:
 		readPictures();
@@ -186,15 +187,10 @@
 
 
 	/**
-	 * Find the properties from the filesystem, and load them
+	 * Find the other from the filesystem (currently just CurrentUserAtom), 
+	 *  and load them
 	 */
-	public void readProperties() {
-		// DocumentSummaryInformation
-		dsInf = (DocumentSummaryInformation)getPropertySet("\005DocumentSummaryInformation");
-
-		// SummaryInformation
-		sInf = (SummaryInformation)getPropertySet("\005SummaryInformation");
-
+	public void readOtherStreams() {
 		// Current User
 		try {
 			currentUser = new CurrentUserAtom(filesystem);
@@ -233,36 +229,6 @@
 	}
 
 
-  /** 
-   * 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
    *  this class
@@ -275,12 +241,7 @@
 	POIFSFileSystem outFS = new POIFSFileSystem();
 
 	// Write out the Property Streams
-	if(sInf != null) {
-		writePropertySet("\005SummaryInformation",sInf,outFS);
-	}
-	if(dsInf != null) {
-		writePropertySet("\005DocumentSummaryInformation",dsInf,outFS);
-	}
+	writeProperties(outFS);
 
 
 	// For position dependent records, hold where they were and now are
@@ -342,24 +303,6 @@
    }
 
 
-  /**
-   * 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 ********************* */
 
 	/**
@@ -417,16 +360,6 @@
 	 *  call to open or write - at all other times might be wrong!
 	 */
 	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

Added: jakarta/poi/trunk/src/scratchpad/testcases/org/apache/poi/TestPOIDocument.java
URL: http://svn.apache.org/viewcvs/jakarta/poi/trunk/src/scratchpad/testcases/org/apache/poi/TestPOIDocument.java?rev=391363&view=auto
==============================================================================
--- jakarta/poi/trunk/src/scratchpad/testcases/org/apache/poi/TestPOIDocument.java (added)
+++ jakarta/poi/trunk/src/scratchpad/testcases/org/apache/poi/TestPOIDocument.java Tue Apr  4 09:50:04 2006
@@ -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();
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
Mailing List:    http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta POI Project: http://jakarta.apache.org/poi/