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

View File

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