poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r576505 - in /poi/trunk/src/scratchpad: src/org/apache/poi/POIDocument.java src/org/apache/poi/hslf/HSLFSlideShow.java testcases/org/apache/poi/hslf/TestReWrite.java testcases/org/apache/poi/hslf/data/WithMacros.ppt
Date Mon, 17 Sep 2007 16:30:32 GMT
Author: nick
Date: Mon Sep 17 09:30:31 2007
New Revision: 576505

URL: http://svn.apache.org/viewvc?rev=576505&view=rev
Log:
When writing out a HSLF file, optionally also write out all the different OLE2 streams, and
not just the normal HSLF related ones

Added:
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/WithMacros.ppt   (with props)
Modified:
    poi/trunk/src/scratchpad/src/org/apache/poi/POIDocument.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/POIDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/POIDocument.java?rev=576505&r1=576504&r2=576505&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/POIDocument.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/POIDocument.java Mon Sep 17 09:30:31 2007
@@ -20,13 +20,18 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
 
 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.DirectoryEntry;
+import org.apache.poi.poifs.filesystem.DocumentEntry;
 import org.apache.poi.poifs.filesystem.DocumentInputStream;
+import org.apache.poi.poifs.filesystem.Entry;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
@@ -106,11 +111,25 @@
 	 * @param outFS the POIFSFileSystem to write the properties into
 	 */
 	protected void writeProperties(POIFSFileSystem outFS) throws IOException {
+		writeProperties(outFS, null);
+	}
+	/**
+	 * Writes out the standard Documment Information Properties (HPSF)
+	 * @param outFS the POIFSFileSystem to write the properties into
+	 * @param writtenEntries a list of POIFS entries to add the property names too
+	 */
+	protected void writeProperties(POIFSFileSystem outFS, List writtenEntries) throws IOException
{
 		if(sInf != null) {
 			writePropertySet(SummaryInformation.DEFAULT_STREAM_NAME,sInf,outFS);
+			if(writtenEntries != null) {
+				writtenEntries.add(SummaryInformation.DEFAULT_STREAM_NAME);
+			}
 		}
 		if(dsInf != null) {
 			writePropertySet(DocumentSummaryInformation.DEFAULT_STREAM_NAME,dsInf,outFS);
+			if(writtenEntries != null) {
+				writtenEntries.add(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
+			}
 		}
 	}
 	
@@ -133,6 +152,65 @@
 			logger.log(POILogger.INFO, "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");
+		}
+	}
+
+	/**
+	 * Copies nodes from one POIFS to the other minus the excepts
+	 * @param source is the source POIFS to copy from
+	 * @param target is the target POIFS to copy to
+	 * @param excepts is a list of Strings specifying what nodes NOT to copy
+	 */
+	protected void copyNodes(POIFSFileSystem source, POIFSFileSystem target,
+	                          List excepts) throws IOException {
+		//System.err.println("CopyNodes called");
+
+		DirectoryEntry root = source.getRoot();
+		DirectoryEntry newRoot = target.getRoot();
+
+		Iterator entries = root.getEntries();
+
+		while (entries.hasNext()) {
+			Entry entry = (Entry)entries.next();
+			if (!isInList(entry.getName(), excepts)) {
+				copyNodeRecursively(entry,newRoot);
+			}
+		}
+	}
+		
+	/**
+	 * Checks to see if the String is in the list, used when copying
+	 *  nodes between one POIFS and another
+	 */
+	private boolean isInList(String entry, List list) {
+		for (int k = 0; k < list.size(); k++) {
+			if (list.get(k).equals(entry)) {
+				return true;
+			}
+		}
+		return false;
+	}
+
+	/**
+	 * Copies an Entry into a target POIFS directory, recursively
+	 */
+	private void copyNodeRecursively(Entry entry, DirectoryEntry target)
+	throws IOException {
+		//System.err.println("copyNodeRecursively called with "+entry.getName()+
+		//                   ","+target.getName());
+		DirectoryEntry newTarget = null;
+		if (entry.isDirectoryEntry()) {
+			newTarget = target.createDirectory(entry.getName());
+			Iterator entries = ((DirectoryEntry)entry).getEntries();
+
+			while (entries.hasNext()) {
+				copyNodeRecursively((Entry)entries.next(),newTarget);
+			}
+		} else {
+			DocumentEntry dentry = (DocumentEntry)entry;
+			DocumentInputStream dstream = new DocumentInputStream(dentry);
+			target.createDocument(dentry.getName(),dstream);
+			dstream.close();
 		}
 	}
 }

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java?rev=576505&r1=576504&r2=576505&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java Mon Sep 17 09:30:31
2007
@@ -383,8 +383,11 @@
         // Get a new Filesystem to write into
         POIFSFileSystem outFS = new POIFSFileSystem();
 
+        // The list of entries we've written out
+        List writtenEntries = new ArrayList(1);
+        
         // Write out the Property Streams
-        writeProperties(outFS);
+        writeProperties(outFS, writtenEntries);
 
 
         // For position dependent records, hold where they were and now are
@@ -435,6 +438,7 @@
         // Write the PPT stream into the POIFS layer
         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
         outFS.createDocument(bais,"PowerPoint Document");
+        writtenEntries.add("PowerPoint Document");
 
 
         // Update and write out the Current User atom
@@ -445,6 +449,7 @@
         }
         currentUser.setCurrentEditOffset(newLastUserEditAtomPos.intValue());
         currentUser.writeToFS(outFS);
+        writtenEntries.add("Current User");
 
 	
         // Write any pictures, into another stream
@@ -456,6 +461,12 @@
             outFS.createDocument(
                 new ByteArrayInputStream(pict.toByteArray()), "Pictures"
             );
+            writtenEntries.add("Pictures");
+        }
+        
+        // If requested, write out any other streams we spot
+        if(preserveNodes) {
+        	copyNodes(filesystem, outFS, writtenEntries);
         }
 
         // Send the POIFSFileSystem object out to the underlying stream

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java?rev=576505&r1=576504&r2=576505&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java (original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java Mon Sep 17 09:30:31
2007
@@ -37,9 +37,11 @@
 	// HSLFSlideShow primed on the test data
 	private HSLFSlideShow hssA;
 	private HSLFSlideShow hssB;
+	private HSLFSlideShow hssC;
 	// POIFS primed on the test data
 	private POIFSFileSystem pfsA;
 	private POIFSFileSystem pfsB;
+	private POIFSFileSystem pfsC;
 
     public void setUp() throws Exception {
 		String dirname = System.getProperty("HSLF.testdata.path");
@@ -53,6 +55,11 @@
 		FileInputStream fisB = new FileInputStream(filenameB);
 		pfsB = new POIFSFileSystem(fisB);
 		hssB = new HSLFSlideShow(pfsB);
+		
+		String filenameC = dirname + "/WithMacros.ppt";
+		FileInputStream fisC = new FileInputStream(filenameC);
+		pfsC = new POIFSFileSystem(fisC);
+		hssC = new HSLFSlideShow(pfsC);
     }
 
     public void testWritesOutTheSame() throws Exception {
@@ -85,6 +92,34 @@
 			assertEquals(_oData[i], _nData[i]);
 		}
 	}
+    
+    public void testWithMacroStreams() throws Exception {
+    	// Check that they're apparently the same
+    	assertSlideShowWritesOutTheSame(hssC, pfsC);
+    	
+    	// Currently has a Macros stream
+    	assertNotNull( pfsC.getRoot().getEntry("Macros") );
+    	
+    	// Write out normally, will loose the macro stream
+    	ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    	hssC.write(baos);
+    	POIFSFileSystem pfsNew = new POIFSFileSystem(
+    			new ByteArrayInputStream(baos.toByteArray()) );
+    	
+    	try {
+    		pfsNew.getRoot().getEntry("Macros");
+    		fail();
+    	} catch(FileNotFoundException e) {
+    		// Good, as expected
+    	}
+    	
+    	// But if we write out with nodes preserved, will be there
+    	baos = new ByteArrayOutputStream();
+    	hssC.write(baos, true);
+    	pfsNew = new POIFSFileSystem(
+    			new ByteArrayInputStream(baos.toByteArray()) );
+    	assertNotNull( pfsNew.getRoot().getEntry("Macros") );
+    }
 
     /**
      * Ensure that simply opening a slideshow (usermodel) view of it

Added: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/WithMacros.ppt
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/WithMacros.ppt?rev=576505&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/WithMacros.ppt
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message