You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ta...@apache.org on 2017/03/16 18:37:14 UTC
svn commit: r1787228 [1/2] - in /poi: site/src/documentation/content/xdocs/
trunk/src/ooxml/java/org/apache/poi/extractor/
trunk/src/ooxml/java/org/apache/poi/xssf/
trunk/src/ooxml/java/org/apache/poi/xssf/binary/
trunk/src/ooxml/java/org/apache/poi/xs...
Author: tallison
Date: Thu Mar 16 18:37:13 2017
New Revision: 1787228
URL: http://svn.apache.org/viewvc?rev=1787228&view=rev
Log:
60826 -- add initial support for streaming reading of xlsb files.
Added:
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellHeader.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellRange.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBComment.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCommentsTable.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooter.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooters.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHyperlinksTable.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParseException.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParser.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRecordType.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRelation.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichStr.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichTextString.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSheetHandler.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBStylesTable.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBUtils.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFHyperlinkRecord.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/package.html
- copied, changed from r1787212, poi/trunk/src/java/org/apache/poi/hssf/usermodel/package.html
poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java (with props)
poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFBEventBasedExcelExtractor.java (with props)
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/binary/
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/binary/TestXSSFBSharedStringsTable.java (with props)
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/binary/TestXSSFBSheetHyperlinkManager.java (with props)
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFBReader.java (with props)
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFBEventBasedExcelExtractor.java (with props)
poi/trunk/test-data/spreadsheet/51519.xlsb (with props)
poi/trunk/test-data/spreadsheet/WithTextBox.xlsb (with props)
poi/trunk/test-data/spreadsheet/comments.xlsb (with props)
poi/trunk/test-data/spreadsheet/date.xlsb (with props)
poi/trunk/test-data/spreadsheet/hyperlink.xlsb (with props)
poi/trunk/test-data/spreadsheet/sample.xlsb (with props)
poi/trunk/test-data/spreadsheet/testVarious.xlsb (with props)
Modified:
poi/site/src/documentation/content/xdocs/status.xml
poi/trunk/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java
poi/trunk/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java
poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java
poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java
poi/trunk/src/ooxml/testcases/org/apache/poi/extractor/TestExtractorFactory.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=1787228&r1=1787227&r2=1787228&view=diff
==============================================================================
--- poi/site/src/documentation/content/xdocs/status.xml (original)
+++ poi/site/src/documentation/content/xdocs/status.xml Thu Mar 16 18:37:13 2017
@@ -58,6 +58,7 @@
<release version="3.16-beta3" date="2017-04-??">
<actions>
+ <action dev="PD" type="add" fixes-bug="60826" module="XSSF">Add initial streaming, read-only support for xlsb files</action>
<action dev="PD" type="fix" fixes-bug="51519" module="XSSF">Allow user to select or ignore phonetic strings in shared strings table</action>
<action dev="PD" type="fix" fixes-bug="60662" module="XSLF">Slide import delete unrecognized elements in group shape</action>
<action dev="PD" type="fix" fixes-bug="60715" module="XSLF">Blank layout was not found</action>
Modified: poi/trunk/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java?rev=1787228&r1=1787227&r2=1787228&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java Thu Mar 16 18:37:13 2017
@@ -56,6 +56,7 @@ import org.apache.poi.xdgf.extractor.XDG
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.XSLFRelation;
import org.apache.poi.xslf.usermodel.XSLFSlideShow;
+import org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor;
import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xssf.usermodel.XSSFRelation;
@@ -244,6 +245,13 @@ public class ExtractorFactory {
return new XSLFPowerPointExtractor(new XSLFSlideShow(pkg));
}
+ // How about xlsb?
+ for (XSSFRelation rel : XSSFBEventBasedExcelExtractor.SUPPORTED_TYPES) {
+ if (rel.getContentType().equals(contentType)) {
+ return new XSSFBEventBasedExcelExtractor(pkg);
+ }
+ }
+
throw new IllegalArgumentException("No supported documents found in the OOXML package (found "+contentType+")");
} catch (IOException e) {
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java?rev=1787228&r1=1787227&r2=1787228&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java Thu Mar 16 18:37:13 2017
@@ -19,7 +19,9 @@ package org.apache.poi.xssf;
import org.apache.poi.UnsupportedFileFormatException;
/**
- * We don't support .xlsb files, sorry
+ * We don't support .xlsb for read and write via {@link org.apache.poi.xssf.usermodel.XSSFWorkbook}.
+ * As of POI 3.15-beta3, we do support streaming reading of xlsb files
+ * via {@link org.apache.poi.xssf.eventusermodel.XSSFBReader}
*/
public class XLSBUnsupportedException extends UnsupportedFileFormatException {
private static final long serialVersionUID = 7849681804154571175L;
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellHeader.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellHeader.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellHeader.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellHeader.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,71 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.LittleEndian;
+
+/**
+ * This class encapsulates what the spec calls a "Cell" object.
+ * I added "Header" to clarify that this does not contain the contents
+ * of the cell, only the column number, the style id and the phonetic boolean
+ */
+@Internal
+class XSSFBCellHeader {
+ public static int length = 8;
+
+ /**
+ *
+ * @param data raw data
+ * @param offset offset at which to start reading the record
+ * @param currentRow 0-based current row count
+ * @param cell cell buffer to update
+ */
+ public static void parse(byte[] data, int offset, int currentRow, XSSFBCellHeader cell) {
+ long colNum = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
+ int styleIdx = XSSFBUtils.get24BitInt(data, offset); offset += 3;
+ //TODO: range checking
+ boolean showPhonetic = false;//TODO: fill this out
+ cell.reset(currentRow, (int)colNum, styleIdx, showPhonetic);
+ }
+
+ private int rowNum;
+ private int colNum;
+ private int styleIdx;
+ private boolean showPhonetic;
+
+ public void reset(int rowNum, int colNum, int styleIdx, boolean showPhonetic) {
+ this.rowNum = rowNum;
+ this.colNum = colNum;
+ this.styleIdx = styleIdx;
+ this.showPhonetic = showPhonetic;
+ }
+
+ int getColNum() {
+ return colNum;
+ }
+
+ String formatAddressAsString() {
+ return CellReference.convertNumToColString(colNum)+(rowNum+1);
+ }
+
+ int getStyleIdx() {
+ return styleIdx;
+ }
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellHeader.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellRange.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellRange.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellRange.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellRange.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,54 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.LittleEndian;
+
+@Internal
+class XSSFBCellRange {
+
+ public final static int length = 4* LittleEndian.INT_SIZE;
+ /**
+ * Parses an RfX cell range from the data starting at the offset.
+ * This performs no range checking.
+ * @param data raw bytes
+ * @param offset offset at which to start reading from data
+ * @param cellRange to overwrite. If null, a new cellRange will be created.
+ * @return a mutable cell range.
+ */
+ public static XSSFBCellRange parse(byte[] data, int offset, XSSFBCellRange cellRange) {
+ if (cellRange == null) {
+ cellRange = new XSSFBCellRange();
+ }
+ cellRange.firstRow = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset)); offset += LittleEndian.INT_SIZE;
+ cellRange.lastRow = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset)); offset += LittleEndian.INT_SIZE;
+ cellRange.firstCol = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset)); offset += LittleEndian.INT_SIZE;
+ cellRange.lastCol = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset));
+
+ return cellRange;
+ }
+
+ int firstRow;
+ int lastRow;
+ int firstCol;
+ int lastCol;
+
+
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellRange.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBComment.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBComment.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBComment.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBComment.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,112 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+
+import org.apache.poi.ss.usermodel.ClientAnchor;
+import org.apache.poi.ss.usermodel.RichTextString;
+import org.apache.poi.ss.util.CellAddress;
+import org.apache.poi.util.Internal;
+import org.apache.poi.xssf.usermodel.XSSFComment;
+
+@Internal
+class XSSFBComment extends XSSFComment {
+
+ private final CellAddress cellAddress;
+ private final String author;
+ private final XSSFBRichTextString comment;
+ private boolean visible = true;
+
+ XSSFBComment(CellAddress cellAddress, String author, String comment) {
+ super(null, null, null);
+ this.cellAddress = cellAddress;
+ this.author = author;
+ this.comment = new XSSFBRichTextString(comment);
+ }
+
+ @Override
+ public void setVisible(boolean visible) {
+ throw new IllegalArgumentException("XSSFBComment is read only.");
+ }
+
+ @Override
+ public boolean isVisible() {
+ return visible;
+ }
+
+ @Override
+ public CellAddress getAddress() {
+ return cellAddress;
+ }
+
+ @Override
+ public void setAddress(CellAddress addr) {
+ throw new IllegalArgumentException("XSSFBComment is read only");
+ }
+
+ @Override
+ public void setAddress(int row, int col) {
+ throw new IllegalArgumentException("XSSFBComment is read only");
+
+ }
+
+ @Override
+ public int getRow() {
+ return cellAddress.getRow();
+ }
+
+ @Override
+ public void setRow(int row) {
+ throw new IllegalArgumentException("XSSFBComment is read only");
+ }
+
+ @Override
+ public int getColumn() {
+ return cellAddress.getColumn();
+ }
+
+ @Override
+ public void setColumn(int col) {
+ throw new IllegalArgumentException("XSSFBComment is read only");
+ }
+
+ @Override
+ public String getAuthor() {
+ return author;
+ }
+
+ @Override
+ public void setAuthor(String author) {
+ throw new IllegalArgumentException("XSSFBComment is read only");
+ }
+
+ @Override
+ public XSSFBRichTextString getString() {
+ return comment;
+ }
+
+ @Override
+ public void setString(RichTextString string) {
+ throw new IllegalArgumentException("XSSFBComment is read only");
+ }
+
+ @Override
+ public ClientAnchor getClientAnchor() {
+ return null;
+ }
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBComment.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCommentsTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCommentsTable.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCommentsTable.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCommentsTable.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,113 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.TreeMap;
+
+import org.apache.poi.ss.util.CellAddress;
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.LittleEndian;
+
+@Internal
+public class XSSFBCommentsTable extends XSSFBParser {
+
+ private Map<CellAddress, XSSFBComment> comments = new TreeMap<CellAddress, XSSFBComment>(new CellAddressComparator());//String is the cellAddress A1
+ private Queue<CellAddress> commentAddresses = new LinkedList<CellAddress>();
+ private List<String> authors = new ArrayList<String>();
+
+ //these are all used only during parsing, and they are mutable!
+ private int authorId = -1;
+ private CellAddress cellAddress = null;
+ private XSSFBCellRange cellRange = null;
+ private String comment = null;
+ private StringBuilder authorBuffer = new StringBuilder();
+
+
+ public XSSFBCommentsTable(InputStream is) throws IOException {
+ super(is);
+ parse();
+ commentAddresses.addAll(comments.keySet());
+ }
+
+ @Override
+ public void handleRecord(int id, byte[] data) throws XSSFBParseException {
+ XSSFBRecordType recordType = XSSFBRecordType.lookup(id);
+ switch (recordType) {
+ case BrtBeginComment:
+ int offset = 0;
+ authorId = XSSFBUtils.castToInt(LittleEndian.getUInt(data)); offset += LittleEndian.INT_SIZE;
+ cellRange = XSSFBCellRange.parse(data, offset, cellRange);
+ offset+= XSSFBCellRange.length;
+ //for strict parsing; confirm that firstRow==lastRow and firstCol==colLats (2.4.28)
+ cellAddress = new CellAddress(cellRange.firstRow, cellRange.firstCol);
+ break;
+ case BrtCommentText:
+ XSSFBRichStr xssfbRichStr = XSSFBRichStr.build(data, 0);
+ comment = xssfbRichStr.getString();
+ break;
+ case BrtEndComment:
+ comments.put(cellAddress, new XSSFBComment(cellAddress, authors.get(authorId), comment));
+ authorId = -1;
+ cellAddress = null;
+ break;
+ case BrtCommentAuthor:
+ authorBuffer.setLength(0);
+ XSSFBUtils.readXLWideString(data, 0, authorBuffer);
+ authors.add(authorBuffer.toString());
+ break;
+ }
+ }
+
+
+ public Queue<CellAddress> getAddresses() {
+ return commentAddresses;
+ }
+
+ public XSSFBComment get(CellAddress cellAddress) {
+ if (cellAddress == null) {
+ return null;
+ }
+ return comments.get(cellAddress);
+ }
+
+ private final static class CellAddressComparator implements Comparator<CellAddress> {
+
+ @Override
+ public int compare(CellAddress o1, CellAddress o2) {
+ if (o1.getRow() < o2.getRow()) {
+ return -1;
+ } else if (o1.getRow() > o2.getRow()) {
+ return 1;
+ }
+ if (o1.getColumn() < o2.getColumn()) {
+ return -1;
+ } else if (o1.getColumn() > o2.getColumn()) {
+ return 1;
+ }
+ return 0;
+ }
+ }
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCommentsTable.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooter.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooter.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooter.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,75 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+import org.apache.poi.util.Internal;
+import org.apache.poi.xssf.usermodel.helpers.HeaderFooterHelper;
+
+@Internal
+class XSSFBHeaderFooter {
+ private final String headerFooterTypeLabel;
+ private final boolean isHeader;
+ private String rawString;
+ private HeaderFooterHelper headerFooterHelper = new HeaderFooterHelper();
+
+
+ XSSFBHeaderFooter(String headerFooterTypeLabel, boolean isHeader) {
+ this.headerFooterTypeLabel = headerFooterTypeLabel;
+ this.isHeader = isHeader;
+ }
+
+ String getHeaderFooterTypeLabel() {
+ return headerFooterTypeLabel;
+ }
+
+ String getRawString() {
+ return rawString;
+ }
+
+ String getString() {
+ StringBuilder sb = new StringBuilder();
+ String left = headerFooterHelper.getLeftSection(rawString);
+ String center = headerFooterHelper.getCenterSection(rawString);
+ String right = headerFooterHelper.getRightSection(rawString);
+ if (left != null && left.length() > 0) {
+ sb.append(left);
+ }
+ if (center != null && center.length() > 0) {
+ if (sb.length() > 0) {
+ sb.append(" ");
+ }
+ sb.append(center);
+ }
+ if (right != null && right.length() > 0) {
+ if (sb.length() > 0) {
+ sb.append(" ");
+ }
+ sb.append(right);
+ }
+ return sb.toString();
+ }
+
+ void setRawString(String rawString) {
+ this.rawString = rawString;
+ }
+
+ boolean isHeader() {
+ return isHeader;
+ }
+
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooter.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooters.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooters.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooters.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooters.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,87 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.xssf.binary;
+
+import org.apache.poi.util.Internal;
+
+@Internal
+class XSSFBHeaderFooters {
+
+ public static XSSFBHeaderFooters parse(byte[] data) {
+ boolean diffOddEven = false;
+ boolean diffFirst = false;
+ boolean scaleWDoc = false;
+ boolean alignMargins = false;
+
+ int offset = 2;
+ XSSFBHeaderFooters xssfbHeaderFooter = new XSSFBHeaderFooters();
+ xssfbHeaderFooter.header = new XSSFBHeaderFooter("header", true);
+ xssfbHeaderFooter.footer = new XSSFBHeaderFooter("footer", false);
+ xssfbHeaderFooter.headerEven = new XSSFBHeaderFooter("evenHeader", true);
+ xssfbHeaderFooter.footerEven = new XSSFBHeaderFooter("evenFooter", false);
+ xssfbHeaderFooter.headerFirst = new XSSFBHeaderFooter("firstHeader", true);
+ xssfbHeaderFooter.footerFirst = new XSSFBHeaderFooter("firstFooter", false);
+ offset += readHeaderFooter(data, offset, xssfbHeaderFooter.header);
+ offset += readHeaderFooter(data, offset, xssfbHeaderFooter.footer);
+ offset += readHeaderFooter(data, offset, xssfbHeaderFooter.headerEven);
+ offset += readHeaderFooter(data, offset, xssfbHeaderFooter.footerEven);
+ offset += readHeaderFooter(data, offset, xssfbHeaderFooter.headerFirst);
+ readHeaderFooter(data, offset, xssfbHeaderFooter.footerFirst);
+ return xssfbHeaderFooter;
+ }
+
+ private static int readHeaderFooter(byte[] data, int offset, XSSFBHeaderFooter headerFooter) {
+ if (offset + 4 >= data.length) {
+ return 0;
+ }
+ StringBuilder sb = new StringBuilder();
+ int bytesRead = XSSFBUtils.readXLNullableWideString(data, offset, sb);
+ headerFooter.setRawString(sb.toString());
+ return bytesRead;
+ }
+
+ private XSSFBHeaderFooter header;
+ private XSSFBHeaderFooter footer;
+ private XSSFBHeaderFooter headerEven;
+ private XSSFBHeaderFooter footerEven;
+ private XSSFBHeaderFooter headerFirst;
+ private XSSFBHeaderFooter footerFirst;
+
+ public XSSFBHeaderFooter getHeader() {
+ return header;
+ }
+
+ public XSSFBHeaderFooter getFooter() {
+ return footer;
+ }
+
+ public XSSFBHeaderFooter getHeaderEven() {
+ return headerEven;
+ }
+
+ public XSSFBHeaderFooter getFooterEven() {
+ return footerEven;
+ }
+
+ public XSSFBHeaderFooter getHeaderFirst() {
+ return headerFirst;
+ }
+
+ public XSSFBHeaderFooter getFooterFirst() {
+ return footerFirst;
+ }
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHeaderFooters.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHyperlinksTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHyperlinksTable.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHyperlinksTable.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHyperlinksTable.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,181 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.ss.util.CellAddress;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.CellRangeUtil;
+import org.apache.poi.util.Internal;
+import org.apache.poi.xssf.usermodel.XSSFRelation;
+
+@Internal
+public class XSSFBHyperlinksTable {
+
+ private final static BitSet RECORDS = new BitSet();
+
+
+ static {
+ RECORDS.set(XSSFBRecordType.BrtHLink.getId());
+ }
+
+
+ private final List<XSSFHyperlinkRecord> hyperlinkRecords = new ArrayList<XSSFHyperlinkRecord>();
+
+ //cache the relId to hyperlink url from the sheet's .rels
+ private Map<String, String> relIdToHyperlink = new HashMap<String, String>();
+
+ public XSSFBHyperlinksTable(PackagePart sheetPart) throws IOException {
+ //load the urls from the sheet .rels
+ loadUrlsFromSheetRels(sheetPart);
+ //now load the hyperlinks from the bottom of the sheet
+ HyperlinkSheetScraper scraper = new HyperlinkSheetScraper(sheetPart.getInputStream());
+ scraper.parse();
+ }
+
+ /**
+ *
+ * @return a map of the hyperlinks. The key is the top left cell address in their CellRange
+ */
+ public Map<CellAddress, List<XSSFHyperlinkRecord>> getHyperLinks() {
+ Map<CellAddress, List<XSSFHyperlinkRecord>> hyperlinkMap =
+ new TreeMap<CellAddress, List<XSSFHyperlinkRecord>>(new TopLeftCellAddressComparator());
+ for (XSSFHyperlinkRecord hyperlinkRecord : hyperlinkRecords) {
+ CellAddress cellAddress = new CellAddress(hyperlinkRecord.getCellRangeAddress().getFirstRow(),
+ hyperlinkRecord.getCellRangeAddress().getFirstColumn());
+ List<XSSFHyperlinkRecord> list = hyperlinkMap.get(cellAddress);
+ if (list == null) {
+ list = new ArrayList<XSSFHyperlinkRecord>();
+ }
+ list.add(hyperlinkRecord);
+ hyperlinkMap.put(cellAddress, list);
+ }
+ return hyperlinkMap;
+ }
+
+
+ /**
+ *
+ * @param cellAddress cell address to find
+ * @return null if not a hyperlink
+ */
+ public List<XSSFHyperlinkRecord> findHyperlinkRecord(CellAddress cellAddress) {
+ List<XSSFHyperlinkRecord> overlapping = null;
+ CellRangeAddress targetCellRangeAddress = new CellRangeAddress(cellAddress.getRow(),
+ cellAddress.getRow(),
+ cellAddress.getColumn(),
+ cellAddress.getColumn());
+ for (XSSFHyperlinkRecord record : hyperlinkRecords) {
+ if (CellRangeUtil.intersect(targetCellRangeAddress, record.getCellRangeAddress()) != CellRangeUtil.NO_INTERSECTION) {
+ if (overlapping == null) {
+ overlapping = new ArrayList<XSSFHyperlinkRecord>();
+ }
+ overlapping.add(record);
+ }
+ }
+ return overlapping;
+ }
+
+ private void loadUrlsFromSheetRels(PackagePart sheetPart) {
+ try {
+ for (PackageRelationship rel : sheetPart.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation())) {
+ relIdToHyperlink.put(rel.getId(), rel.getTargetURI().toString());
+ }
+ } catch (InvalidFormatException e) {
+ //swallow
+ }
+ }
+
+ private class HyperlinkSheetScraper extends XSSFBParser {
+
+ private XSSFBCellRange hyperlinkCellRange = new XSSFBCellRange();
+ private final StringBuilder xlWideStringBuffer = new StringBuilder();
+
+ HyperlinkSheetScraper(InputStream is) {
+ super(is, RECORDS);
+ }
+
+ @Override
+ public void handleRecord(int recordType, byte[] data) throws XSSFBParseException {
+ if (recordType != XSSFBRecordType.BrtHLink.getId()) {
+ return;
+ }
+ int offset = 0;
+ String relId = "";
+ String location = "";
+ String toolTip = "";
+ String display = "";
+
+ hyperlinkCellRange = XSSFBCellRange.parse(data, offset, hyperlinkCellRange);
+ offset += XSSFBCellRange.length;
+ xlWideStringBuffer.setLength(0);
+ offset += XSSFBUtils.readXLNullableWideString(data, offset, xlWideStringBuffer);
+ relId = xlWideStringBuffer.toString();
+ xlWideStringBuffer.setLength(0);
+ offset += XSSFBUtils.readXLWideString(data, offset, xlWideStringBuffer);
+ location = xlWideStringBuffer.toString();
+ xlWideStringBuffer.setLength(0);
+ offset += XSSFBUtils.readXLWideString(data, offset, xlWideStringBuffer);
+ toolTip = xlWideStringBuffer.toString();
+ xlWideStringBuffer.setLength(0);
+ offset += XSSFBUtils.readXLWideString(data, offset, xlWideStringBuffer);
+ display = xlWideStringBuffer.toString();
+ CellRangeAddress cellRangeAddress = new CellRangeAddress(hyperlinkCellRange.firstRow, hyperlinkCellRange.lastRow, hyperlinkCellRange.firstCol, hyperlinkCellRange.lastCol);
+
+ String url = relIdToHyperlink.get(relId);
+ if (location == null || location.length() == 0) {
+ location = url;
+ }
+
+ hyperlinkRecords.add(
+ new XSSFHyperlinkRecord(cellRangeAddress, relId, location, toolTip, display)
+ );
+ }
+ }
+
+ private static class TopLeftCellAddressComparator implements Comparator<CellAddress> {
+
+ @Override
+ public int compare(CellAddress o1, CellAddress o2) {
+ if (o1.getRow() < o2.getRow()) {
+ return -1;
+ } else if (o1.getRow() > o2.getRow()) {
+ return 1;
+ }
+ if (o1.getColumn() < o2.getColumn()) {
+ return -1;
+ } else if (o1.getColumn() > o2.getColumn()) {
+ return 1;
+ }
+ return 0;
+ }
+ }
+
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBHyperlinksTable.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParseException.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParseException.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParseException.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParseException.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,28 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+/**
+ * Parse exception while reading an xssfb
+ */
+public class XSSFBParseException extends RuntimeException {
+
+ public XSSFBParseException(String msg) {
+ super(msg);
+ }
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParseException.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: 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=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParser.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParser.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,105 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.BitSet;
+
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.LittleEndianInputStream;
+
+/**
+ * Experimental parser for Microsoft's ooxml xssfb format.
+ * Not thread safe, obviously. Need to create a new one
+ * for each thread.
+ */
+@Internal
+public abstract class XSSFBParser {
+
+ private final LittleEndianInputStream is;
+ private final BitSet records;
+
+ public XSSFBParser(InputStream is) {
+ this.is = new LittleEndianInputStream(is);
+ records = null;
+ }
+
+ XSSFBParser(InputStream is, BitSet bitSet) {
+ this.is = new LittleEndianInputStream(is);
+ records = bitSet;
+ }
+
+ public void parse() throws IOException {
+
+ while (true) {
+ int bInt = is.read();
+ if (bInt == -1) {
+ return;
+ }
+ readNext((byte) bInt);
+ }
+ }
+
+ private void readNext(byte b1) throws IOException {
+ int recordId = 0;
+
+ //if highest bit == 1
+ if ((b1 >> 7 & 1) == 1) {
+ byte b2 = is.readByte();
+ b1 &= ~(1<<7); //unset highest bit
+ b2 &= ~(1<<7); //unset highest bit (if it exists?)
+ recordId = (128*(int)b2)+(int)b1;
+ } else {
+ recordId = (int)b1;
+ }
+
+ long recordLength = 0;
+ int i = 0;
+ boolean halt = false;
+ while (i < 4 && ! halt) {
+ byte b = is.readByte();
+ halt = (b >> 7 & 1) == 0; //if highest bit !=1 then continue
+ b &= ~(1<<7);
+ recordLength += (int)b << (i*7); //multiply by 128^i
+ i++;
+
+ }
+ if (records == null || records.get(recordId)) {
+ //add sanity check for length?
+ byte[] buff = new byte[(int) recordLength];
+ is.readFully(buff);
+ handleRecord(recordId, buff);
+ } else {
+ long length = is.skip(recordLength);
+ if (length != recordLength) {
+ throw new XSSFBParseException("End of file reached before expected.\t"+
+ "Tried to skip "+recordLength + ", but only skipped "+length);
+ }
+ }
+ }
+
+ //It hurts, hurts, hurts to create a new byte array for every record.
+ //However, on a large Excel spreadsheet, this parser was 1/3 faster than
+ //the ooxml sax parser (5 seconds for xssfb and 7.5 seconds for xssf.
+ //The code is far cleaner to have the parser read all
+ //of the data rather than having every component promise that it read
+ //the correct amount.
+ abstract public void handleRecord(int recordType, byte[] data) throws XSSFBParseException;
+
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: 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=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRecordType.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRecordType.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,92 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+import org.apache.poi.util.Internal;
+
+@Internal
+public enum XSSFBRecordType {
+
+ BrtCellBlank(1),
+ BrtCellRk(2),
+ BrtCellError(3),
+ BrtCellBool(4),
+ BrtCellReal(5),
+ BrtCellSt(6),
+ BrtCellIsst(7),
+ BrtFmlaString(8),
+ BrtFmlaNum(9),
+ BrtFmlaBool(10),
+ BrtFmlaError(11),
+ BrtRowHdr(0),
+ BrtCellRString(62),
+ BrtBeginSheet(129),
+ BrtWsProp(147),
+ BrtWsDim(148),
+ BrtColInfo(60),
+ BrtBeginSheetData(145),
+ BrtEndSheetData(146),
+ BrtHLink(494),
+ BrtBeginHeaderFooter(479),
+
+ //comments
+ BrtBeginCommentAuthors(630),
+ BrtEndCommentAuthors(631),
+ BrtCommentAuthor(632),
+ BrtBeginComment(635),
+ BrtCommentText(637),
+ BrtEndComment(636),
+ //styles table
+ BrtXf(47),
+ BrtFmt(44),
+ BrtBeginFmts(615),
+ BrtEndFmts(616),
+ BrtBeginCellXFs(617),
+ BrtEndCellXFs(618),
+ BrtBeginCellStyleXFS(626),
+ BrtEndCellStyleXFS(627),
+
+ //stored strings table
+ BrtSstItem(19), //stored strings items
+ BrtBeginSst(159), //stored strings begin sst
+ BrtEndSst(160), //stored strings end sst
+
+ BrtBundleSh(156), //defines worksheet in wb part
+ Unimplemented(-1);
+
+
+ private final int id;
+
+ XSSFBRecordType(int id) {
+ this.id = id;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public static XSSFBRecordType lookup(int id) {
+ for (XSSFBRecordType r : XSSFBRecordType.values()) {
+ if (r.id == id) {
+ return r;
+ }
+ }
+ return Unimplemented;
+ }
+
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRecordType.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRelation.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRelation.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRelation.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRelation.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,85 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Iterator;
+
+import org.apache.poi.POIXMLDocumentPart;
+import org.apache.poi.POIXMLRelation;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackagePartName;
+import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
+import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
+import org.apache.poi.openxml4j.opc.PackagingURIHelper;
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
+
+/**
+ * Need to have this mirror class of {@link org.apache.poi.xssf.usermodel.XSSFRelation}
+ * because of conflicts with regular ooxml relations.
+ * If we failed to break this into a separate class, in the cases of SharedStrings and Styles,
+ * 2 parts would exist, and "Packages shall not contain equivalent part names..."
+ * <p>
+ * Also, we need to avoid the possibility of breaking the marshalling process for xml.
+ */
+@Internal
+public class XSSFBRelation extends POIXMLRelation {
+ private static final POILogger log = POILogFactory.getLogger(XSSFBRelation.class);
+
+ static final XSSFBRelation SHARED_STRINGS_BINARY = new XSSFBRelation(
+ "application/vnd.ms-excel.sharedStrings",
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
+ "/xl/sharedStrings.bin",
+ null
+ );
+
+ public static final XSSFBRelation STYLES_BINARY = new XSSFBRelation(
+ "application/vnd.ms-excel.styles",
+ PackageRelationshipTypes.STYLE_PART,
+ "/xl/styles.bin",
+ null
+ );
+
+ private XSSFBRelation(String type, String rel, String defaultName, Class<? extends POIXMLDocumentPart> cls) {
+ super(type, rel, defaultName, cls);
+ }
+
+ /**
+ * Fetches the InputStream to read the contents, based
+ * of the specified core part, for which we are defined
+ * as a suitable relationship
+ */
+ public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
+ PackageRelationshipCollection prc =
+ corePart.getRelationshipsByType(getRelation());
+ Iterator<PackageRelationship> it = prc.iterator();
+ if (it.hasNext()) {
+ PackageRelationship rel = it.next();
+ PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
+ PackagePart part = corePart.getPackage().getPart(relName);
+ return part.getInputStream();
+ }
+ log.log(POILogger.WARN, "No part " + getDefaultFileName() + " found");
+ return null;
+ }
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRelation.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichStr.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichStr.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichStr.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichStr.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,47 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+import org.apache.poi.util.Internal;
+
+@Internal
+class XSSFBRichStr {
+
+ public static XSSFBRichStr build(byte[] bytes, int offset) throws XSSFBParseException {
+ byte first = bytes[offset];
+ boolean dwSizeStrRunExists = (first >> 7 & 1) == 1;//first bit == 1?
+ boolean phoneticExists = (first >> 6 & 1) == 1;//second bit == 1?
+ StringBuilder sb = new StringBuilder();
+
+ int read = XSSFBUtils.readXLWideString(bytes, offset+1, sb);
+ //TODO: parse phonetic strings.
+ return new XSSFBRichStr(sb.toString(), "");
+ }
+
+ private final String string;
+ private final String phoneticString;
+
+ XSSFBRichStr(String string, String phoneticString) {
+ this.string = string;
+ this.phoneticString = phoneticString;
+ }
+
+ public String getString() {
+ return string;
+ }
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichStr.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichTextString.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichTextString.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichTextString.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichTextString.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,80 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.util.Internal;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
+
+/**
+ * Wrapper class around String so that we can use it in Comment.
+ * Nothing has been implemented yet except for {@link #getString()}.
+ */
+@Internal
+class XSSFBRichTextString extends XSSFRichTextString {
+ private final String string;
+
+ XSSFBRichTextString(String string) {
+ this.string = string;
+ }
+
+ @Override
+ public void applyFont(int startIndex, int endIndex, short fontIndex) {
+
+ }
+
+ @Override
+ public void applyFont(int startIndex, int endIndex, Font font) {
+
+ }
+
+ @Override
+ public void applyFont(Font font) {
+
+ }
+
+ @Override
+ public void clearFormatting() {
+
+ }
+
+ @Override
+ public String getString() {
+ return string;
+ }
+
+ @Override
+ public int length() {
+ return string.length();
+ }
+
+ @Override
+ public int numFormattingRuns() {
+ return 0;
+ }
+
+ @Override
+ public int getIndexOfFormattingRun(int index) {
+ return 0;
+ }
+
+ @Override
+ public void applyFont(short fontIndex) {
+
+ }
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBRichTextString.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,137 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.xssf.binary;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.LittleEndian;
+import org.xml.sax.SAXException;
+
+@Internal
+public class XSSFBSharedStringsTable {
+
+ /**
+ * An integer representing the total count of strings in the workbook. This count does not
+ * include any numbers, it counts only the total of text strings in the workbook.
+ */
+ private int count;
+
+ /**
+ * An integer representing the total count of unique strings in the Shared String Table.
+ * A string is unique even if it is a copy of another string, but has different formatting applied
+ * at the character level.
+ */
+ private int uniqueCount;
+
+ /**
+ * The shared strings table.
+ */
+ private List<String> strings = new ArrayList<String>();
+
+ /**
+ * @param pkg The {@link OPCPackage} to use as basis for the shared-strings table.
+ * @throws IOException If reading the data from the package fails.
+ * @throws SAXException if parsing the XML data fails.
+ */
+ public XSSFBSharedStringsTable(OPCPackage pkg)
+ throws IOException, SAXException {
+ ArrayList<PackagePart> parts =
+ pkg.getPartsByContentType(XSSFBRelation.SHARED_STRINGS_BINARY.getContentType());
+
+ // Some workbooks have no shared strings table.
+ if (parts.size() > 0) {
+ PackagePart sstPart = parts.get(0);
+
+ readFrom(sstPart.getInputStream());
+ }
+ }
+
+ /**
+ * Like POIXMLDocumentPart constructor
+ *
+ * @since POI 3.14-Beta3
+ */
+ XSSFBSharedStringsTable(PackagePart part) throws IOException, SAXException {
+ readFrom(part.getInputStream());
+ }
+
+ private void readFrom(InputStream inputStream) throws IOException {
+ SSTBinaryReader reader = new SSTBinaryReader(inputStream);
+ reader.parse();
+ }
+
+ public List<String> getItems() {
+ return strings;
+ }
+
+ public String getEntryAt(int i) {
+ return strings.get(i);
+ }
+
+ /**
+ * Return an integer representing the total count of strings in the workbook. This count does not
+ * include any numbers, it counts only the total of text strings in the workbook.
+ *
+ * @return the total count of strings in the workbook
+ */
+ public int getCount() {
+ return this.count;
+ }
+
+ /**
+ * Returns an integer representing the total count of unique strings in the Shared String Table.
+ * A string is unique even if it is a copy of another string, but has different formatting applied
+ * at the character level.
+ *
+ * @return the total count of unique strings in the workbook
+ */
+ public int getUniqueCount() {
+ return this.uniqueCount;
+ }
+
+ private class SSTBinaryReader extends XSSFBParser {
+
+ SSTBinaryReader(InputStream is) {
+ super(is);
+ }
+
+ @Override
+ public void handleRecord(int recordType, byte[] data) throws XSSFBParseException {
+ XSSFBRecordType type = XSSFBRecordType.lookup(recordType);
+
+ switch (type) {
+ case BrtSstItem:
+ XSSFBRichStr rstr = XSSFBRichStr.build(data, 0);
+ strings.add(rstr.getString());
+ break;
+ case BrtBeginSst:
+ count = (int) LittleEndian.getUInt(data,0);
+ uniqueCount = (int) LittleEndian.getUInt(data, 4);
+ break;
+ }
+
+ }
+ }
+
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSheetHandler.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSheetHandler.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSheetHandler.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSheetHandler.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,329 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+
+import java.io.InputStream;
+import java.util.Queue;
+
+import org.apache.poi.ss.usermodel.DataFormatter;
+import org.apache.poi.ss.util.CellAddress;
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.LittleEndian;
+import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
+import org.apache.poi.xssf.usermodel.XSSFComment;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
+
+@Internal
+public class XSSFBSheetHandler extends XSSFBParser {
+
+ private final static int CHECK_ALL_ROWS = -1;
+
+ private final XSSFBSharedStringsTable stringsTable;
+ private final XSSFSheetXMLHandler.SheetContentsHandler handler;
+ private final XSSFBStylesTable styles;
+ private final XSSFBCommentsTable comments;
+ private final DataFormatter dataFormatter;
+ private final boolean formulasNotResults;//TODO: implement this
+
+ private int lastEndedRow = -1;
+ private int lastStartedRow = -1;
+ private int currentRow = 0;
+ private byte[] rkBuffer = new byte[8];
+ private XSSFBCellRange hyperlinkCellRange = null;
+ private StringBuilder xlWideStringBuffer = new StringBuilder();
+
+ private final XSSFBCellHeader cellBuffer = new XSSFBCellHeader();
+ public XSSFBSheetHandler(InputStream is,
+ XSSFBStylesTable styles,
+ XSSFBCommentsTable comments,
+ XSSFBSharedStringsTable strings,
+ XSSFSheetXMLHandler.SheetContentsHandler sheetContentsHandler,
+ DataFormatter dataFormatter,
+ boolean formulasNotResults) {
+ super(is);
+ this.styles = styles;
+ this.comments = comments;
+ this.stringsTable = strings;
+ this.handler = sheetContentsHandler;
+ this.dataFormatter = dataFormatter;
+ this.formulasNotResults = formulasNotResults;
+ }
+
+ @Override
+ public void handleRecord(int id, byte[] data) throws XSSFBParseException {
+ XSSFBRecordType type = XSSFBRecordType.lookup(id);
+
+ switch(type) {
+ case BrtRowHdr:
+ long rw = LittleEndian.getUInt(data, 0);
+ if (rw > 0x00100000L) {//could make sure this is larger than currentRow, according to spec?
+ throw new XSSFBParseException("Row number beyond allowable range: "+rw);
+ }
+ currentRow = (int)rw;
+ checkMissedComments(currentRow);
+ startRow(currentRow);
+ break;
+ case BrtCellIsst:
+ handleBrtCellIsst(data);
+ break;
+ case BrtCellSt: //TODO: needs test
+ handleCellSt(data);
+ break;
+ case BrtCellRk:
+ handleCellRk(data);
+ break;
+ case BrtCellReal:
+ handleCellReal(data);
+ break;
+ case BrtCellBool:
+ handleBoolean(data);
+ break;
+ case BrtCellError:
+ handleCellError(data);
+ break;
+ case BrtCellBlank:
+ beforeCellValue(data);//read cell info and check for missing comments
+ break;
+ case BrtFmlaString:
+ handleFmlaString(data);
+ break;
+ case BrtFmlaNum:
+ handleFmlaNum(data);
+ break;
+ case BrtFmlaError:
+ handleFmlaError(data);
+ break;
+ //TODO: All the PCDI and PCDIA
+ case BrtEndSheetData:
+ checkMissedComments(CHECK_ALL_ROWS);
+ endRow(lastStartedRow);
+ break;
+ case BrtBeginHeaderFooter:
+ handleHeaderFooter(data);
+ break;
+ }
+ }
+
+
+ private void beforeCellValue(byte[] data) {
+ XSSFBCellHeader.parse(data, 0, currentRow, cellBuffer);
+ checkMissedComments(currentRow, cellBuffer.getColNum());
+ }
+
+ private void handleCellValue(String formattedValue) {
+ CellAddress cellAddress = new CellAddress(currentRow, cellBuffer.getColNum());
+ XSSFBComment comment = null;
+ if (comments != null) {
+ comment = comments.get(cellAddress);
+ }
+ handler.cell(cellAddress.formatAsString(), formattedValue, comment);
+ }
+
+ private void handleFmlaNum(byte[] data) {
+ beforeCellValue(data);
+ //xNum
+ double val = LittleEndian.getDouble(data, XSSFBCellHeader.length);
+ String formatString = styles.getNumberFormatString(cellBuffer.getStyleIdx());
+ String formattedVal = dataFormatter.formatRawCellContents(val, cellBuffer.getStyleIdx(), formatString);
+ handleCellValue(formattedVal);
+ }
+
+ private void handleCellSt(byte[] data) {
+ beforeCellValue(data);
+ xlWideStringBuffer.setLength(0);
+ XSSFBUtils.readXLWideString(data, XSSFBCellHeader.length, xlWideStringBuffer);
+ handleCellValue(xlWideStringBuffer.toString());
+ }
+
+ private void handleFmlaString(byte[] data) {
+ beforeCellValue(data);
+ xlWideStringBuffer.setLength(0);
+ XSSFBUtils.readXLWideString(data, XSSFBCellHeader.length, xlWideStringBuffer);
+ handleCellValue(xlWideStringBuffer.toString());
+ }
+
+ private void handleCellError(byte[] data) {
+ beforeCellValue(data);
+ //TODO, read byte to figure out the type of error
+ handleCellValue("ERROR");
+ }
+
+ private void handleFmlaError(byte[] data) {
+ beforeCellValue(data);
+ //TODO, read byte to figure out the type of error
+ handleCellValue("ERROR");
+ }
+
+ private void handleBoolean(byte[] data) {
+ beforeCellValue(data);
+ String formattedVal = (data[XSSFBCellHeader.length] == 1) ? "TRUE" : "FALSE";
+ handleCellValue(formattedVal);
+ }
+
+ private void handleCellReal(byte[] data) {
+ beforeCellValue(data);
+ //xNum
+ double val = LittleEndian.getDouble(data, XSSFBCellHeader.length);
+ String formatString = styles.getNumberFormatString(cellBuffer.getStyleIdx());
+ String formattedVal = dataFormatter.formatRawCellContents(val, cellBuffer.getStyleIdx(), formatString);
+ handleCellValue(formattedVal);
+ }
+
+ private void handleCellRk(byte[] data) {
+ beforeCellValue(data);
+ double val = rkNumber(data, XSSFBCellHeader.length);
+ String formatString = styles.getNumberFormatString(cellBuffer.getStyleIdx());
+ String formattedVal = dataFormatter.formatRawCellContents(val, cellBuffer.getStyleIdx(), formatString);
+ handleCellValue(formattedVal);
+ }
+
+ private void handleBrtCellIsst(byte[] data) {
+ beforeCellValue(data);
+ long idx = LittleEndian.getUInt(data, XSSFBCellHeader.length);
+ //check for out of range, buffer overflow
+
+ XSSFRichTextString rtss = new XSSFRichTextString(stringsTable.getEntryAt((int)idx));
+ handleCellValue(rtss.getString());
+ }
+
+
+ private void handleHeaderFooter(byte[] data) {
+ XSSFBHeaderFooters headerFooter = XSSFBHeaderFooters.parse(data);
+ outputHeaderFooter(headerFooter.getHeader());
+ outputHeaderFooter(headerFooter.getFooter());
+ outputHeaderFooter(headerFooter.getHeaderEven());
+ outputHeaderFooter(headerFooter.getFooterEven());
+ outputHeaderFooter(headerFooter.getHeaderFirst());
+ outputHeaderFooter(headerFooter.getFooterFirst());
+ }
+
+ private void outputHeaderFooter(XSSFBHeaderFooter headerFooter) {
+ String text = headerFooter.getString();
+ if (text != null && text.trim().length() > 0) {
+ handler.headerFooter(text, headerFooter.isHeader(), headerFooter.getHeaderFooterTypeLabel());
+ }
+ }
+
+
+ //at start of next cell or end of row, return the cellAddress if it equals currentRow and col
+ private void checkMissedComments(int currentRow, int colNum) {
+ if (comments == null) {
+ return;
+ }
+ Queue<CellAddress> queue = comments.getAddresses();
+ while (queue.size() > 0) {
+ CellAddress cellAddress = queue.peek();
+ if (cellAddress.getRow() == currentRow && cellAddress.getColumn() < colNum) {
+ cellAddress = queue.remove();
+ dumpEmptyCellComment(cellAddress, comments.get(cellAddress));
+ } else if (cellAddress.getRow() == currentRow && cellAddress.getColumn() == colNum) {
+ queue.remove();
+ return;
+ } else if (cellAddress.getRow() == currentRow && cellAddress.getColumn() > colNum) {
+ return;
+ } else if (cellAddress.getRow() > currentRow) {
+ return;
+ }
+ }
+ }
+
+ //check for anything from rows before
+ private void checkMissedComments(int currentRow) {
+ if (comments == null) {
+ return;
+ }
+ Queue<CellAddress> queue = comments.getAddresses();
+ int lastInterpolatedRow = -1;
+ while (queue.size() > 0) {
+ CellAddress cellAddress = queue.peek();
+ if (currentRow == CHECK_ALL_ROWS || cellAddress.getRow() < currentRow) {
+ cellAddress = queue.remove();
+ if (cellAddress.getRow() != lastInterpolatedRow) {
+ startRow(cellAddress.getRow());
+ }
+ dumpEmptyCellComment(cellAddress, comments.get(cellAddress));
+ lastInterpolatedRow = cellAddress.getRow();
+ } else {
+ break;
+ }
+ }
+
+ }
+
+ private void startRow(int row) {
+ if (row == lastStartedRow) {
+ return;
+ }
+
+ if (lastStartedRow != lastEndedRow) {
+ endRow(lastStartedRow);
+ }
+ handler.startRow(row);
+ lastStartedRow = row;
+ }
+
+ private void endRow(int row) {
+ if (lastEndedRow == row) {
+ return;
+ }
+ handler.endRow(row);
+ lastEndedRow = row;
+ }
+
+ private void dumpEmptyCellComment(CellAddress cellAddress, XSSFBComment comment) {
+ handler.cell(cellAddress.formatAsString(), null, comment);
+ }
+
+ private double rkNumber(byte[] data, int offset) {
+ //see 2.5.122 for this abomination
+ byte b0 = data[offset];
+ String s = Integer.toString(b0, 2);
+ boolean numDivBy100 = ((b0 & 1) == 1); // else as is
+ boolean floatingPoint = ((b0 >> 1 & 1) == 0); // else signed integer
+
+ //unset highest 2 bits
+ b0 &= ~1;
+ b0 &= ~(1<<1);
+
+ rkBuffer[4] = b0;
+ for (int i = 1; i < 4; i++) {
+ rkBuffer[i+4] = data[offset+i];
+ }
+ double d = 0.0;
+ if (floatingPoint) {
+ d = LittleEndian.getDouble(rkBuffer);
+ } else {
+ d = LittleEndian.getInt(rkBuffer);
+ }
+ d = (numDivBy100) ? d/100 : d;
+ return d;
+ }
+
+ /**
+ * You need to implement this to handle the results
+ * of the sheet parsing.
+ */
+ public interface SheetContentsHandler extends XSSFSheetXMLHandler.SheetContentsHandler {
+ /**
+ * A cell, with the given formatted value (may be null),
+ * a url (may be null), a toolTip (may be null)
+ * and possibly a comment (may be null), was encountered */
+ void hyperlinkCell(String cellReference, String formattedValue, String url, String toolTip, XSSFComment comment);
+ }
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBSheetHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBStylesTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBStylesTable.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBStylesTable.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBStylesTable.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,101 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+import org.apache.poi.POIXMLException;
+import org.apache.poi.ss.usermodel.BuiltinFormats;
+import org.apache.poi.util.Internal;
+
+@Internal
+public class XSSFBStylesTable extends XSSFBParser {
+
+ private final SortedMap<Short, String> numberFormats = new TreeMap<Short,String>();
+ private final List<Short> styleIds = new ArrayList<Short>();
+
+ private boolean inCellXFS = false;
+ private boolean inFmts = false;
+ public XSSFBStylesTable(InputStream is) throws IOException {
+ super(is);
+ parse();
+ }
+
+ String getNumberFormatString(int idx) {
+ if (numberFormats.containsKey(styleIds.get((short)idx))) {
+ return numberFormats.get(styleIds.get((short)idx));
+ }
+
+ return BuiltinFormats.getBuiltinFormat(styleIds.get((short)idx));
+ }
+
+ @Override
+ public void handleRecord(int recordType, byte[] data) throws XSSFBParseException {
+ XSSFBRecordType type = XSSFBRecordType.lookup(recordType);
+ switch (type) {
+ case BrtBeginCellXFs:
+ inCellXFS = true;
+ break;
+ case BrtEndCellXFs:
+ inCellXFS = false;
+ break;
+ case BrtXf:
+ if (inCellXFS) {
+ handleBrtXFInCellXF(data);
+ }
+ break;
+ case BrtBeginFmts:
+ inFmts = true;
+ break;
+ case BrtEndFmts:
+ inFmts = false;
+ break;
+ case BrtFmt:
+ if (inFmts) {
+ handleFormat(data);
+ }
+ break;
+
+ }
+ }
+
+ private void handleFormat(byte[] data) {
+ int ifmt = data[0] & 0xFF;
+ if (ifmt > Short.MAX_VALUE) {
+ throw new POIXMLException("Format id must be a short");
+ }
+ StringBuilder sb = new StringBuilder();
+ XSSFBUtils.readXLWideString(data, 2, sb);
+ String fmt = sb.toString();
+ numberFormats.put((short)ifmt, fmt);
+ }
+
+ private void handleBrtXFInCellXF(byte[] data) {
+ int ifmtOffset = 2;
+ //int ifmtLength = 2;
+
+ //numFmtId in xml terms
+ int ifmt = data[ifmtOffset] & 0xFF;//the second byte is ignored
+ styleIds.add((short)ifmt);
+ }
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBStylesTable.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBUtils.java?rev=1787228&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBUtils.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBUtils.java Thu Mar 16 18:37:13 2017
@@ -0,0 +1,108 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.binary;
+
+
+import java.nio.charset.Charset;
+
+import org.apache.poi.POIXMLException;
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.LittleEndian;
+
+@Internal
+public class XSSFBUtils {
+
+ /**
+ * Reads an XLNullableWideString.
+ * @param data data from which to read
+ * @param offset in data from which to start
+ * @param sb buffer to which to write. You must setLength(0) before calling!
+ * @return number of bytes read
+ * @throws XSSFBParseException if there was an exception during reading
+ */
+ static int readXLNullableWideString(byte[] data, int offset, StringBuilder sb) throws XSSFBParseException {
+ long numChars = LittleEndian.getUInt(data, offset);
+ if (numChars < 0) {
+ throw new XSSFBParseException("too few chars to read");
+ } else if (numChars == 0xFFFFFFFFL) { //this means null value (2.5.166), do not read any bytes!!!
+ return 0;
+ } else if (numChars > 0xFFFFFFFFL) {
+ throw new XSSFBParseException("too many chars to read");
+ }
+
+ int numBytes = 2*(int)numChars;
+ offset += 4;
+ if (offset+numBytes > data.length) {
+ throw new XSSFBParseException("trying to read beyond data length:" +
+ "offset="+offset+", numBytes="+numBytes+", data.length="+data.length);
+ }
+ sb.append(new String(data, offset, numBytes, Charset.forName("UTF-16LE")));
+ numBytes+=4;
+ return numBytes;
+ }
+
+
+ /**
+ * Reads an XLNullableWideString.
+ * @param data data from which to read
+ * @param offset in data from which to start
+ * @param sb buffer to which to write. You must setLength(0) before calling!
+ * @return number of bytes read
+ * @throws XSSFBParseException if there was an exception while trying to read the string
+ */
+ public static int readXLWideString(byte[] data, int offset, StringBuilder sb) throws XSSFBParseException {
+ long numChars = LittleEndian.getUInt(data, offset);
+ if (numChars < 0) {
+ throw new XSSFBParseException("too few chars to read");
+ } else if (numChars > 0xFFFFFFFFL) {
+ throw new XSSFBParseException("too many chars to read");
+ }
+ int numBytes = 2*(int)numChars;
+ offset += 4;
+ if (offset+numBytes > data.length) {
+ throw new XSSFBParseException("trying to read beyond data length");
+ }
+ sb.append(new String(data, offset, numBytes, Charset.forName("UTF-16LE")));
+ numBytes+=4;
+ return numBytes;
+ }
+
+ static int castToInt(long val) {
+ if (val < Integer.MAX_VALUE && val > Integer.MIN_VALUE) {
+ return (int)val;
+ }
+ throw new POIXMLException("val ("+val+") can't be cast to int");
+ }
+
+ static short castToShort(int val) {
+ if (val < Short.MAX_VALUE && val > Short.MIN_VALUE) {
+ return (short)val;
+ }
+ throw new POIXMLException("val ("+val+") can't be cast to short");
+
+ }
+
+ //TODO: move to LittleEndian?
+ static int get24BitInt( byte[] data, int offset) {
+ int i = offset;
+ int b0 = data[i++] & 0xFF;
+ int b1 = data[i++] & 0xFF;
+ int b2 = data[i] & 0xFF;
+ return ( b2 << 16 ) + ( b1 << 8 ) + b0;
+ }
+}
Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org