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
This commit is contained in:
Yegor Kozlov 2012-02-29 07:50:10 +00:00
parent cfa6bdc291
commit c0e87067e6
3 changed files with 67 additions and 6 deletions

View 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>

View 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;
}
}

View File

@ -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"));
}
}