You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2012/02/29 08:50:11 UTC
svn commit: r1294998 - in /poi/trunk/src:
documentation/content/xdocs/status.xml
ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java
ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
Author: yegor
Date: Wed Feb 29 07:50:10 2012
New Revision: 1294998
URL: http://svn.apache.org/viewvc?rev=1294998&view=rev
Log:
Bugzilla 51780 - support replacement of content types in OPC packages
Modified:
poi/trunk/src/documentation/content/xdocs/status.xml
poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java
poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1294998&r1=1294997&r2=1294998&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Wed Feb 29 07:50:10 2012
@@ -34,6 +34,7 @@
<changes>
<release version="3.8-beta6" date="2012-??-??">
+ <action dev="poi-developers" type="add">51780 - support replacement of content types in OPC packages </action>
<action dev="poi-developers" type="fix">52784 - replace ISO control characters with question marks in SXSSF to be consistent with XSSF </action>
<action dev="poi-developers" type="add">52057 - updated formula test framework to be aware of recently added Functions </action>
<action dev="poi-developers" type="add">52574 - support setting header / footer page margins in HSSF </action>
Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java?rev=1294998&r1=1294997&r2=1294998&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java Wed Feb 29 07:50:10 2012
@@ -1439,4 +1439,50 @@ public abstract class OPCPackage impleme
*/
protected abstract PackagePart[] getPartsImpl()
throws InvalidFormatException;
+
+ /**
+ * Replace a content type in this package.
+ *
+ * <p>
+ * A typical scneario to call this method is to rename a template file to the main format, e.g.
+ * ".dotx" to ".docx"
+ * ".dotm" to ".docm"
+ * ".xltx" to ".xlsx"
+ * ".xltm" to ".xlsm"
+ * ".potx" to ".pptx"
+ * ".potm" to ".pptm"
+ * </p>
+ * For example, a code converting a .xlsm macro workbook to .xlsx would look as follows:
+ * <p>
+ * <pre><code>
+ *
+ * OPCPackage pkg = OPCPackage.open(new FileInputStream("macro-workbook.xlsm"));
+ * pkg.replaceContentType(
+ * "application/vnd.ms-excel.sheet.macroEnabled.main+xml",
+ * "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");
+ *
+ * FileOutputStream out = new FileOutputStream("workbook.xlsx");
+ * pkg.save(out);
+ * out.close();
+ *
+ * </code></pre>
+ * </p>
+ *
+ * @param oldContentType the content type to be replaced
+ * @param newContentType the replacement
+ * @return whether replacement was succesfull
+ * @since POI-3.8
+ */
+ public boolean replaceContentType(String oldContentType, String newContentType){
+ boolean success = false;
+ ArrayList<PackagePart> list = getPartsByContentType(oldContentType);
+ for (PackagePart packagePart : list) {
+ if (packagePart.getContentType().equals(oldContentType)) {
+ PackagePartName partName = packagePart.getPartName();
+ contentTypeManager.addContentType(partName, newContentType);
+ success = true;
+ }
+ }
+ return success;
+ }
}
Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java?rev=1294998&r1=1294997&r2=1294998&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java Wed Feb 29 07:50:10 2012
@@ -17,12 +17,7 @@
package org.apache.poi.openxml4j.opc;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
+import java.io.*;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.*;
@@ -528,4 +523,23 @@ public final class TestPackage extends T
assertTrue(selected.containsKey("/word/theme/theme1.xml"));
assertTrue(selected.containsKey("/word/webSettings.xml"));
}
+
+ public void testReplaceContentType() throws Exception {
+ InputStream is = OpenXML4JTestDataSamples.openSampleStream("sample.xlsx");
+ OPCPackage p = OPCPackage.open(is);
+
+ ContentTypeManager mgr = getContentTypeManager(p);
+
+ assertTrue(mgr.isContentTypeRegister("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"));
+ assertFalse(mgr.isContentTypeRegister("application/vnd.ms-excel.sheet.macroEnabled.main+xml"));
+
+ assertTrue(
+ p.replaceContentType(
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
+ "application/vnd.ms-excel.sheet.macroEnabled.main+xml")
+ );
+
+ assertFalse(mgr.isContentTypeRegister("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"));
+ assertTrue(mgr.isContentTypeRegister("application/vnd.ms-excel.sheet.macroEnabled.main+xml"));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org