From afaf6472d0c580f06598bb2ea55c55cd3d1fcf16 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 29 Feb 2012 07:50:10 +0000 Subject: [PATCH] Bugzilla 51780 - support replacement of content types in OPC packages git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1294998 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/openxml4j/opc/OPCPackage.java | 46 +++++++++++++++++++ .../apache/poi/openxml4j/opc/TestPackage.java | 26 ++++++++--- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index b1a1825ad2..538273a20e 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51780 - support replacement of content types in OPC packages 52784 - replace ISO control characters with question marks in SXSSF to be consistent with XSSF 52057 - updated formula test framework to be aware of recently added Functions 52574 - support setting header / footer page margins in HSSF diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java index f384cc650c..7a0da815b7 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java @@ -1439,4 +1439,50 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { */ protected abstract PackagePart[] getPartsImpl() throws InvalidFormatException; + + /** + * Replace a content type in this package. + * + *

+ * 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" + *

+ * For example, a code converting a .xlsm macro workbook to .xlsx would look as follows: + *

+ *


+     *
+     *     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();
+     *
+     *    
+ *

+ * + * @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 list = getPartsByContentType(oldContentType); + for (PackagePart packagePart : list) { + if (packagePart.getContentType().equals(oldContentType)) { + PackagePartName partName = packagePart.getPartName(); + contentTypeManager.addContentType(partName, newContentType); + success = true; + } + } + return success; + } } diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java index 35e87ec00d..b6bc97c185 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java @@ -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 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")); + } } -- 2.39.5