Author: tallison
Date: Fri Apr 21 13:02:29 2017
New Revision: 1792198

URL: http://svn.apache.org/viewvc?rev=1792198&view=rev
Log:
bug 61021 - extract abspath from xlsb

Modified:
    poi/site/src/documentation/content/xdocs/status.xml
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParser.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRecordType.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java
    
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFBReader.java

Modified: poi/site/src/documentation/content/xdocs/status.xml
URL: 
http://svn.apache.org/viewvc/poi/site/src/documentation/content/xdocs/status.xml?rev=1792198&r1=1792197&r2=1792198&view=diff
==============================================================================
--- poi/site/src/documentation/content/xdocs/status.xml (original)
+++ poi/site/src/documentation/content/xdocs/status.xml Fri Apr 21 13:02:29 2017
@@ -58,6 +58,7 @@
 
     <release version="3.17-beta1" date="2017-07-??">
       <actions>
+        <action dev="PD" type="add" fixes-bug="61021" module="XSSF">Extract 
Excel 2013 absPath info from xlsb</action>
         <action dev="PD" type="fix" fixes-bug="60998" 
module="HSLF">HSLFTable.setRowHeight sets row height incorrect</action>
         <action dev="PD" type="fix" fixes-bug="60996" module="XSSF">Multiple 
embedded objects on same sheet are ignored</action>
         <action dev="PD" type="fix" fixes-bug="60993" module="HSLF">Grid and 
rowspan calculation in table cells is wrong</action>

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParser.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParser.java?rev=1792198&r1=1792197&r2=1792198&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParser.java 
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParser.java Fri 
Apr 21 13:02:29 2017
@@ -47,7 +47,7 @@ public abstract class XSSFBParser {
      * @param is inputStream
      * @param bitSet call {@link #handleRecord(int, byte[])} only on those 
records in this bitSet
      */
-    XSSFBParser(InputStream is, BitSet bitSet) {
+    protected XSSFBParser(InputStream is, BitSet bitSet) {
         this.is = new LittleEndianInputStream(is);
         records = bitSet;
     }

Modified: 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRecordType.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRecordType.java?rev=1792198&r1=1792197&r2=1792198&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRecordType.java 
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRecordType.java 
Fri Apr 21 13:02:29 2017
@@ -74,6 +74,8 @@ public enum XSSFBRecordType {
 
     BrtBundleSh(156), //defines worksheet in wb part
 
+    BrtAbsPath15(2071), //Excel 2013 path where the file was stored in wbpart
+
     //TODO -- implement these as needed
     //BrtFileVersion(128), //file version
     //BrtWbProp(153), //Workbook prop contains 1904/1900-date based bit

Modified: 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java?rev=1792198&r1=1792197&r2=1792198&view=diff
==============================================================================
--- 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java 
(original)
+++ 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java 
Fri Apr 21 13:02:29 2017
@@ -19,6 +19,7 @@ package org.apache.poi.xssf.eventusermod
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.BitSet;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -31,6 +32,7 @@ import org.apache.poi.openxml4j.opc.Pack
 import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
 import org.apache.poi.openxml4j.opc.PackagingURIHelper;
+import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.LittleEndian;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
@@ -63,6 +65,26 @@ public class XSSFBReader extends XSSFRea
     }
 
     /**
+     * In Excel 2013, the absolute path where the file was last saved may be 
stored in
+     * the {@link XSSFBRecordType#BrtAbsPath15} record.  The equivalent in 
ooxml is
+     * &lt;x15ac:absPath&gt;.
+     *
+     * @return absolute path or <code>null</code> if it could not be found.
+     * @throws IOException when there's a problem with the workbook part's 
stream
+     */
+    public String getAbsPathMetadata() throws IOException {
+        InputStream is = null;
+        try {
+            is = workbookPart.getInputStream();
+            PathExtractor p = new PathExtractor(workbookPart.getInputStream());
+            p.parse();
+            return p.getPath();
+        } finally {
+            IOUtils.closeQuietly(is);
+        }
+    }
+
+    /**
      * Returns an Iterator which will let you get at all the
      *  different Sheets in turn.
      * Each sheet's InputStream is only opened when fetched
@@ -137,6 +159,36 @@ public class XSSFBReader extends XSSFRea
 
     }
 
+
+    private static class PathExtractor extends XSSFBParser {
+        private static BitSet RECORDS = new BitSet();
+        static {
+            RECORDS.set(XSSFBRecordType.BrtAbsPath15.getId());
+        }
+        private String path = null;
+        public PathExtractor(InputStream is) {
+            super(is, RECORDS);
+        }
+
+        @Override
+        public void handleRecord(int recordType, byte[] data) throws 
XSSFBParseException {
+            if (recordType != XSSFBRecordType.BrtAbsPath15.getId()) {
+                return;
+            }
+            StringBuilder sb = new StringBuilder();
+            XSSFBUtils.readXLWideString(data, 0, sb);
+            path = sb.toString();
+        }
+
+        /**
+         *
+         * @return the path if found, otherwise <code>null</code>
+         */
+        String getPath() {
+            return path;
+        }
+    }
+
     private static class SheetRefLoader extends XSSFBParser {
         List<XSSFSheetRef> sheets = new LinkedList<XSSFSheetRef>();
 

Modified: 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFBReader.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFBReader.java?rev=1792198&r1=1792197&r2=1792198&view=diff
==============================================================================
--- 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFBReader.java
 (original)
+++ 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFBReader.java
 Fri Apr 21 13:02:29 2017
@@ -118,6 +118,13 @@ public class TestXSSFBReader {
 
     }
 
+    @Test
+    public void testAbsPath() throws Exception {
+        OPCPackage pkg = 
OPCPackage.open(_ssTests.openResourceAsStream("testVarious.xlsb"));
+        XSSFBReader r = new XSSFBReader(pkg);
+        assertEquals("C:\\Users\\tallison\\Desktop\\working\\xlsb\\", 
r.getAbsPathMetadata());
+    }
+
     private List<String> getSheets(String testFileName) throws Exception {
         OPCPackage pkg = 
OPCPackage.open(_ssTests.openResourceAsStream(testFileName));
         List<String> sheetTexts = new ArrayList<String>();



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to