From a98eaeb917943adcd6bfe04c9e446d59a94f7de2 Mon Sep 17 00:00:00 2001 From: Tim Allison Date: Thu, 27 Apr 2017 18:50:22 +0000 Subject: [PATCH] bug 61051 -- add new worksheet-like relations for xlsb git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1792940 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/eventusermodel/XSSFBReader.java | 22 +++++++++++++-- .../poi/xssf/eventusermodel/XSSFReader.java | 27 ++++++++++++++++--- .../poi/xssf/usermodel/XSSFRelation.java | 21 +++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java index 0606ecec4..6653ee655 100644 --- a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java +++ b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java @@ -19,10 +19,14 @@ package org.apache.poi.xssf.eventusermodel; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.BitSet; +import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Set; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.OpenXML4JException; @@ -53,7 +57,17 @@ import org.apache.poi.xssf.usermodel.XSSFRelation; */ public class XSSFBReader extends XSSFReader { - private final static POILogger log = POILogFactory.getLogger(XSSFBReader.class); + private static final POILogger log = POILogFactory.getLogger(XSSFBReader.class); + private static final Set WORKSHEET_RELS = + Collections.unmodifiableSet(new HashSet( + Arrays.asList(new String[]{ + XSSFRelation.WORKSHEET.getRelation(), + XSSFRelation.CHARTSHEET.getRelation(), + XSSFRelation.MACRO_SHEET_BIN.getRelation(), + XSSFRelation.INTL_MACRO_SHEET_BIN.getRelation(), + XSSFRelation.DIALOG_SHEET_BIN.getRelation() + }) + )); /** * Creates a new XSSFReader, for the given package @@ -105,7 +119,6 @@ public class XSSFBReader extends XSSFReader { } - public static class SheetIterator extends XSSFReader.SheetIterator { /** @@ -117,6 +130,11 @@ public class XSSFBReader extends XSSFReader { super(wb); } + @Override + Set getSheetRelationships() { + return WORKSHEET_RELS; + } + Iterator createSheetIteratorFromWB(PackagePart wb) throws IOException { SheetRefLoader sheetRefLoader = new SheetRefLoader(wb.getInputStream()); sheetRefLoader.parse(); diff --git a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java index 08dfaa6ff..68a85df54 100644 --- a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java +++ b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java @@ -20,13 +20,16 @@ import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import org.apache.poi.POIXMLException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; @@ -64,6 +67,13 @@ import org.xml.sax.helpers.DefaultHandler; */ public class XSSFReader { + private static final Set WORKSHEET_RELS = + Collections.unmodifiableSet(new HashSet( + Arrays.asList(new String[]{ + XSSFRelation.WORKSHEET.getRelation(), + XSSFRelation.CHARTSHEET.getRelation(), + }) + )); private static final POILogger LOGGER = POILogFactory.getLogger(XSSFReader.class); protected OPCPackage pkg; @@ -123,7 +133,6 @@ public class XSSFReader { } - /** * Returns an InputStream to read the contents of the * shared strings table. @@ -223,11 +232,10 @@ public class XSSFReader { //step 1. Map sheet's relationship Id and the corresponding PackagePart sheetMap = new HashMap(); OPCPackage pkg = wb.getPackage(); - String REL_WORKSHEET = XSSFRelation.WORKSHEET.getRelation(); - String REL_CHARTSHEET = XSSFRelation.CHARTSHEET.getRelation(); + Set worksheetRels = getSheetRelationships(); for(PackageRelationship rel : wb.getRelationships()){ String relType = rel.getRelationshipType(); - if (relType.equals(REL_WORKSHEET) || relType.equals(REL_CHARTSHEET)) { + if (worksheetRels.contains(relType)) { PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI()); sheetMap.put(rel.getId(), pkg.getPart(relName)); } @@ -269,6 +277,17 @@ public class XSSFReader { return validSheets.iterator(); } + /** + * Gets string representations of relationships + * that are sheet-like. Added to allow subclassing + * by XSSFBReader. This is used to decide what + * relationships to load into the sheetRefs + * + * @return + */ + Set getSheetRelationships() { + return WORKSHEET_RELS; + } /** * Returns true if the iteration has more elements. diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java index 019d552a2..dee6706f3 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java @@ -286,6 +286,27 @@ public final class XSSFRelation extends POIXMLRelation { null ); + public static final XSSFRelation MACRO_SHEET_BIN = new XSSFRelation( + null,//TODO: figure out what this should be? + "http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet", + "/xl/macroSheets/sheet#.bin", + null + ); + + public static final XSSFRelation INTL_MACRO_SHEET_BIN = new XSSFRelation( + null,//TODO: figure out what this should be? + "http://schemas.microsoft.com/office/2006/relationships/xlIntlMacrosheet", + "/xl/macroSheets/sheet#.bin", + null + ); + + public static final XSSFRelation DIALOG_SHEET_BIN = new XSSFRelation( + null,//TODO: figure out what this should be? + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/dialogsheet", + "/xl/dialogSheets/sheet#.bin", + null + ); + public static final XSSFRelation THEME = new XSSFRelation( "application/vnd.openxmlformats-officedocument.theme+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",