]> source.dussan.org Git - poi.git/commitdiff
Bugzilla 51780 - support replacement of content types in OPC packages
authorYegor Kozlov <yegor@apache.org>
Wed, 29 Feb 2012 07:50:10 +0000 (07:50 +0000)
committerYegor Kozlov <yegor@apache.org>
Wed, 29 Feb 2012 07:50:10 +0000 (07:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1294998 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java

index b1a1825ad217d80280856cefade2777ce21d1afd..538273a20ed43b0b9eb9e2f40a2e2e13e1e66c46 100644 (file)
@@ -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>
index f384cc650c508bf3481e50d10c4dc9ddce022b35..7a0da815b7c5cfd7eb87936d8df0714402b7cf0a 100644 (file)
@@ -1439,4 +1439,50 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
         */
        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;
+    }
 }
index 35e87ec00d230ce4cc81e00f5ec918202dcca7df..b6bc97c18553ca21aae0fc86f5beb3756132639e 100644 (file)
 
 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 TestCase {
         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"));
+    }
 }