Have ExtractorFactory open OPCPackages from files in read-only mode by default, since writing should never be needed when extracting text

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1652877 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-01-18 23:46:29 +00:00
parent bcb02aa4bc
commit cef16bab94
2 changed files with 11 additions and 8 deletions

View File

@ -44,6 +44,7 @@ import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection; import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.DirectoryEntry;
@ -131,7 +132,7 @@ public class ExtractorFactory {
return createExtractor(new POIFSFileSystem(inp)); return createExtractor(new POIFSFileSystem(inp));
} }
if(POIXMLDocument.hasOOXMLHeader(inp)) { if(POIXMLDocument.hasOOXMLHeader(inp)) {
return createExtractor(OPCPackage.open(f.toString())); return createExtractor(OPCPackage.open(f.toString(), PackageAccess.READ));
} }
throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file"); throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file");
} finally { } finally {

View File

@ -195,13 +195,15 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor
// add textboxes // add textboxes
if (includeTextBoxes){ if (includeTextBoxes){
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.getDrawingPatriarch();
for (XSSFShape shape : drawing.getShapes()){ if (drawing != null) {
if (shape instanceof XSSFSimpleShape){ for (XSSFShape shape : drawing.getShapes()){
String boxText = ((XSSFSimpleShape)shape).getText(); if (shape instanceof XSSFSimpleShape){
if (boxText.length() > 0){ String boxText = ((XSSFSimpleShape)shape).getText();
text.append(boxText); if (boxText.length() > 0){
text.append('\n'); text.append(boxText);
text.append('\n');
}
} }
} }
} }