make xssf streaming code more extensible

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1836795 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2018-07-27 09:19:58 +00:00
parent ad4f5ea6d2
commit d2d9202772
2 changed files with 85 additions and 88 deletions

View File

@ -90,7 +90,7 @@ public class XSSFReader {
// strict OOXML likely not fully supported, see #57699 // strict OOXML likely not fully supported, see #57699
// this code is similar to POIXMLDocumentPart.getPartFromOPCPackage(), but I could not combine it // this code is similar to POIXMLDocumentPart.getPartFromOPCPackage(), but I could not combine it
// easily due to different return values // easily due to different return values
if(coreDocRelationship == null) { if (coreDocRelationship == null) {
if (this.pkg.getRelationshipsByType( if (this.pkg.getRelationshipsByType(
PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0) != null) { PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0) != null) {
throw new POIXMLException("Strict OOXML isn't currently supported, please see bug #57699"); throw new POIXMLException("Strict OOXML isn't currently supported, please see bug #57699");
@ -110,7 +110,7 @@ public class XSSFReader {
* shared strings. * shared strings.
*/ */
public SharedStringsTable getSharedStringsTable() throws IOException, InvalidFormatException { public SharedStringsTable getSharedStringsTable() throws IOException, InvalidFormatException {
ArrayList<PackagePart> parts = pkg.getPartsByContentType( XSSFRelation.SHARED_STRINGS.getContentType()); ArrayList<PackagePart> parts = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType());
return parts.size() == 0 ? null : new SharedStringsTable(parts.get(0)); return parts.size() == 0 ? null : new SharedStringsTable(parts.get(0));
} }
@ -119,13 +119,13 @@ public class XSSFReader {
* returns a handy object for working with cell styles * returns a handy object for working with cell styles
*/ */
public StylesTable getStylesTable() throws IOException, InvalidFormatException { public StylesTable getStylesTable() throws IOException, InvalidFormatException {
ArrayList<PackagePart> parts = pkg.getPartsByContentType( XSSFRelation.STYLES.getContentType()); ArrayList<PackagePart> parts = pkg.getPartsByContentType(XSSFRelation.STYLES.getContentType());
if(parts.size() == 0) return null; if (parts.size() == 0) return null;
// Create the Styles Table, and associate the Themes if present // Create the Styles Table, and associate the Themes if present
StylesTable styles = new StylesTable(parts.get(0)); StylesTable styles = new StylesTable(parts.get(0));
parts = pkg.getPartsByContentType( XSSFRelation.THEME.getContentType()); parts = pkg.getPartsByContentType(XSSFRelation.THEME.getContentType());
if(parts.size() != 0) { if (parts.size() != 0) {
styles.setTheme(new ThemesTable(parts.get(0))); styles.setTheme(new ThemesTable(parts.get(0)));
} }
return styles; return styles;
@ -168,17 +168,18 @@ public class XSSFReader {
/** /**
* Returns an InputStream to read the contents of the * Returns an InputStream to read the contents of the
* specified Sheet. * specified Sheet.
*
* @param relId The relationId of the sheet, from a r:id on the workbook * @param relId The relationId of the sheet, from a r:id on the workbook
*/ */
public InputStream getSheet(String relId) throws IOException, InvalidFormatException { public InputStream getSheet(String relId) throws IOException, InvalidFormatException {
PackageRelationship rel = workbookPart.getRelationship(relId); PackageRelationship rel = workbookPart.getRelationship(relId);
if(rel == null) { if (rel == null) {
throw new IllegalArgumentException("No Sheet found with r:id " + relId); throw new IllegalArgumentException("No Sheet found with r:id " + relId);
} }
PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI()); PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
PackagePart sheet = pkg.getPart(relName); PackagePart sheet = pkg.getPart(relName);
if(sheet == null) { if (sheet == null) {
throw new IllegalArgumentException("No data found for Sheet with r:id " + relId); throw new IllegalArgumentException("No data found for Sheet with r:id " + relId);
} }
return sheet.getInputStream(); return sheet.getInputStream();
@ -232,7 +233,7 @@ public class XSSFReader {
sheetMap = new HashMap<>(); sheetMap = new HashMap<>();
OPCPackage pkg = wb.getPackage(); OPCPackage pkg = wb.getPackage();
Set<String> worksheetRels = getSheetRelationships(); Set<String> worksheetRels = getSheetRelationships();
for(PackageRelationship rel : wb.getRelationships()){ for (PackageRelationship rel : wb.getRelationships()) {
String relType = rel.getRelationshipType(); String relType = rel.getRelationshipType();
if (worksheetRels.contains(relType)) { if (worksheetRels.contains(relType)) {
PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI()); PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
@ -242,7 +243,7 @@ public class XSSFReader {
//step 2. Read array of CTSheet elements, wrap it in a LinkedList //step 2. Read array of CTSheet elements, wrap it in a LinkedList
//and construct an iterator //and construct an iterator
sheetIterator = createSheetIteratorFromWB(wb); sheetIterator = createSheetIteratorFromWB(wb);
} catch (InvalidFormatException e){ } catch (InvalidFormatException e) {
throw new POIXMLException(e); throw new POIXMLException(e);
} }
} }
@ -311,7 +312,7 @@ public class XSSFReader {
try { try {
PackagePart sheetPkg = sheetMap.get(sheetId); PackagePart sheetPkg = sheetMap.get(sheetId);
return sheetPkg.getInputStream(); return sheetPkg.getInputStream();
} catch(IOException e) { } catch (IOException e) {
throw new POIXMLException(e); throw new POIXMLException(e);
} }
} }
@ -336,15 +337,14 @@ public class XSSFReader {
try { try {
PackageRelationshipCollection commentsList = PackageRelationshipCollection commentsList =
sheetPkg.getRelationshipsByType(XSSFRelation.SHEET_COMMENTS.getRelation()); sheetPkg.getRelationshipsByType(XSSFRelation.SHEET_COMMENTS.getRelation());
if(commentsList.size() > 0) { if (commentsList.size() > 0) {
PackageRelationship comments = commentsList.getRelationship(0); PackageRelationship comments = commentsList.getRelationship(0);
PackagePartName commentsName = PackagingURIHelper.createPartName(comments.getTargetURI()); PackagePartName commentsName = PackagingURIHelper.createPartName(comments.getTargetURI());
PackagePart commentsPart = sheetPkg.getPackage().getPart(commentsName); PackagePart commentsPart = sheetPkg.getPackage().getPart(commentsName);
return new CommentsTable(commentsPart); return new CommentsTable(commentsPart);
} }
} catch (InvalidFormatException e) { } catch (InvalidFormatException|IOException e) {
return null; LOGGER.log(POILogger.WARN, e);
} catch (IOException e) {
return null; return null;
} }
return null; return null;
@ -356,27 +356,24 @@ public class XSSFReader {
*/ */
public List<XSSFShape> getShapes() { public List<XSSFShape> getShapes() {
PackagePart sheetPkg = getSheetPart(); PackagePart sheetPkg = getSheetPart();
List<XSSFShape> shapes= new LinkedList<>(); List<XSSFShape> shapes = new LinkedList<>();
// Do we have a comments relationship? (Only ever one if so) // Do we have a comments relationship? (Only ever one if so)
try { try {
PackageRelationshipCollection drawingsList = sheetPkg.getRelationshipsByType(XSSFRelation.DRAWINGS.getRelation()); PackageRelationshipCollection drawingsList = sheetPkg.getRelationshipsByType(XSSFRelation.DRAWINGS.getRelation());
for (int i = 0; i < drawingsList.size(); i++){ for (int i = 0; i < drawingsList.size(); i++) {
PackageRelationship drawings = drawingsList.getRelationship(i); PackageRelationship drawings = drawingsList.getRelationship(i);
PackagePartName drawingsName = PackagingURIHelper.createPartName(drawings.getTargetURI()); PackagePartName drawingsName = PackagingURIHelper.createPartName(drawings.getTargetURI());
PackagePart drawingsPart = sheetPkg.getPackage().getPart(drawingsName); PackagePart drawingsPart = sheetPkg.getPackage().getPart(drawingsName);
if (drawingsPart == null) { if (drawingsPart == null) {
//parts can go missing; Excel ignores them silently -- TIKA-2134 //parts can go missing; Excel ignores them silently -- TIKA-2134
LOGGER.log(POILogger.WARN, "Missing drawing: "+drawingsName +". Skipping it."); LOGGER.log(POILogger.WARN, "Missing drawing: " + drawingsName + ". Skipping it.");
continue; continue;
} }
XSSFDrawing drawing = new XSSFDrawing(drawingsPart); XSSFDrawing drawing = new XSSFDrawing(drawingsPart);
shapes.addAll(drawing.getShapes()); shapes.addAll(drawing.getShapes());
} }
} catch (XmlException e){ } catch (XmlException|InvalidFormatException|IOException e) {
return null; LOGGER.log(POILogger.WARN, e);
} catch (InvalidFormatException e) {
return null;
} catch (IOException e) {
return null; return null;
} }
return shapes; return shapes;

View File

@ -58,16 +58,16 @@ public class XSSFEventBasedExcelExtractor extends POIXMLTextExtractor
private static final POILogger LOGGER = POILogFactory.getLogger(XSSFEventBasedExcelExtractor.class); private static final POILogger LOGGER = POILogFactory.getLogger(XSSFEventBasedExcelExtractor.class);
private OPCPackage container; protected OPCPackage container;
private POIXMLProperties properties; protected POIXMLProperties properties;
private Locale locale; protected Locale locale;
private boolean includeTextBoxes = true; protected boolean includeTextBoxes = true;
private boolean includeSheetNames = true; protected boolean includeSheetNames = true;
private boolean includeCellComments; protected boolean includeCellComments;
private boolean includeHeadersFooters = true; protected boolean includeHeadersFooters = true;
private boolean formulasNotResults; protected boolean formulasNotResults;
private boolean concatenatePhoneticRuns = true; protected boolean concatenatePhoneticRuns = true;
public XSSFEventBasedExcelExtractor(String path) throws XmlException, OpenXML4JException, IOException { public XSSFEventBasedExcelExtractor(String path) throws XmlException, OpenXML4JException, IOException {
this(OPCPackage.open(path)); this(OPCPackage.open(path));
@ -254,7 +254,7 @@ public class XSSFEventBasedExcelExtractor extends POIXMLTextExtractor
} }
} }
protected SharedStrings createSharedStringsTable(OPCPackage container, boolean concatenatePhoneticRuns) protected SharedStrings createSharedStringsTable(XSSFReader xssfReader, OPCPackage container)
throws IOException, SAXException { throws IOException, SAXException {
return new ReadOnlySharedStringsTable(container, concatenatePhoneticRuns); return new ReadOnlySharedStringsTable(container, concatenatePhoneticRuns);
} }
@ -264,8 +264,8 @@ public class XSSFEventBasedExcelExtractor extends POIXMLTextExtractor
*/ */
public String getText() { public String getText() {
try { try {
SharedStrings strings = createSharedStringsTable(container, concatenatePhoneticRuns);
XSSFReader xssfReader = new XSSFReader(container); XSSFReader xssfReader = new XSSFReader(container);
SharedStrings strings = createSharedStringsTable(xssfReader, container);
StylesTable styles = xssfReader.getStylesTable(); StylesTable styles = xssfReader.getStylesTable();
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
StringBuilder text = new StringBuilder(64); StringBuilder text = new StringBuilder(64);