Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #55 #56

Merged
merged 4 commits into from
Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ abstract class HSSFUnmarshaller implements Unmarshaller {
@Override
public <T> void unmarshal(Class<T> type, Consumer<? super T> consumer) {
Workbook workbook = workbook();
Sheet sheet = workbook.getSheetAt(options.sheetIndex());
//Sheet sheet = workbook.getSheetAt(options.sheetIndex());

//work out which sheet must process
//ISSUE #55
int processIndex = PoijiOptions.getSheetIndexToProcess(workbook, options);
Sheet sheet = workbook.getSheetAt(processIndex);

int skip = options.skip();
int maxPhysicalNumberOfRows = sheet.getPhysicalNumberOfRows() + 1 - skip;
Expand Down Expand Up @@ -138,8 +143,9 @@ private boolean skip(final Row currentRow, int skip) {
private boolean isRowEmpty(Row row) {
for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
Cell cell = row.getCell(c, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (cell != null && cell.getCellTypeEnum() != CellType.BLANK)
if (cell != null && cell.getCellTypeEnum() != CellType.BLANK) {
return false;
}
}
return true;
}
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/com/poiji/bind/mapping/WorkBookContentHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.poiji.bind.mapping;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

/**
*
* @author Matthew 2018/09/01
*/
public class WorkBookContentHandler implements ContentHandler {

public List<WorkBookSheet> sheets = new ArrayList<>();
private WorkBookSheet individualSheet;

@Override
public void setDocumentLocator(Locator locator) {
}

@Override
public void startDocument() throws SAXException {
}

@Override
public void endDocument() throws SAXException {
}

@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
}

@Override
public void endPrefixMapping(String prefix) throws SAXException {
}

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {

if (qName.equals("sheet")) {
individualSheet = new WorkBookSheet();

for (int i = 0; i < atts.getLength(); i++) {

// Attribute: name:Sheet3
// Attribute: sheetId:3
// Attribute: state:hidden
if (atts.getQName(i).equals("name")) {
individualSheet.name = atts.getValue(i);
}
if (atts.getQName(i).equals("sheetId")) {
individualSheet.sheetId = atts.getValue(i);
}
if (atts.getQName(i).equals("state")) {
individualSheet.state = atts.getValue(i);
}
}
}
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {

if (qName.equals("sheet")) {
sheets.add(individualSheet);
individualSheet = null;
}

}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
}

@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
}

@Override
public void processingInstruction(String target, String data) throws SAXException {
}

@Override
public void skippedEntity(String name) throws SAXException {
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/poiji/bind/mapping/WorkBookSheet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.poiji.bind.mapping;

/**
*
* @author Matthew 2018/09/01
*/
public class WorkBookSheet {

public String name;
public String sheetId;
public String state;

@Override
public String toString() {
return "WorkBookSheet{" + "name=" + name + ", sheetId=" + sheetId + ", state=" + state + '}';
}

}
105 changes: 98 additions & 7 deletions src/main/java/com/poiji/bind/mapping/XSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,27 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Consumer;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import static org.apache.poi.xssf.eventusermodel.XSSFReader.SheetIterator;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
////
//import java.io.InputStream;
//
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
//import org.apache.poi.openxml4j.opc.OPCPackage;
//import org.apache.poi.xssf.eventusermodel.XSSFReader;
//import org.apache.poi.xssf.model.SharedStringsTable;
//import org.apache.xerces.parsers.SAXParser;
//import org.xml.sax.InputSource;
//import org.xml.sax.XMLReader;
//
//import poi.example.eventmodel.SheetHandler;
//import poi.example.eventmodel.WorkbookHandler;
//import poi.example.eventmodel.mapping.annotation.Sheet;

/**
* Created by hakan on 22/10/2017
Expand All @@ -39,6 +58,78 @@ abstract class XSSFUnmarshaller implements Unmarshaller {

<T> void unmarshal0(Class<T> type, Consumer<? super T> consumer, OPCPackage open) throws IOException, SAXException, OpenXML4JException {

WorkBookContentHandler wbch = null;
try {
XSSFReader workbookReader = new XSSFReader(open);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(new WorkBookContentHandler());
InputSource is = new InputSource(workbookReader.getWorkbookData());
reader.parse(is);
wbch = (WorkBookContentHandler) reader.getContentHandler();
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new PoijiException("Problem occurred while reading workbook data", e);
}

// System.out.println("wbch" + wbch);
int processIndex = 0;
if (wbch != null) {

// System.out.println("wbch.sheets" + wbch.sheets);
int findIndex;
//if given sheet index to use, use that
if (options.sheetIndex() != null && options.sheetIndex() > -1) {
findIndex = options.sheetIndex();
} else {
//else default
findIndex = 0;
}

// System.out.println("findIndex " + findIndex);
int sheetIndex;
//if set to hignore hidden find the visiable sheet that matches the index requested
if (options.ignoreHiddenSheets()) {
// System.out.println("ignoreHiddenSheets true ");
Integer visiableIndex = null;

int sheetCount = 0;
for (WorkBookSheet s : wbch.sheets) {

// System.out.println("WorkBookSheet s " + s);
if (s.state == null) {
// System.out.println("WorkBookSheet NULL");
//cannot use sheet is, cos that is its id not its index, they seem to be diffent things
visiableIndex = sheetCount;
}

sheetCount++;

}

// System.out.println("visiableIndex" + visiableIndex);
if (visiableIndex != null) {
sheetIndex = visiableIndex;
} else {
//if no sheet found, default back
sheetIndex = findIndex;
}

// System.out.println("sheetIndex" + sheetIndex);
} else {

//if dont want to ignore hidden sheets, use index given or default
sheetIndex = findIndex;
// System.out.println("ELSE sheetIndex" + sheetIndex);
}
processIndex = sheetIndex;
}

// System.out.println("processIndex" + processIndex);
// XSSFWorkbook wb = new XSSFWorkbook(open);
// Workbook workbook = new SXSSFWorkbook(wb);
// //work out which sheet must process
// int processIndex = PoijiOptions.getSheetIndexToProcess(workbook, options);
ReadOnlySharedStringsTable readOnlySharedStringsTable = new ReadOnlySharedStringsTable(open);
XSSFReader xssfReader = new XSSFReader(open);
StylesTable styles = xssfReader.getStylesTable();
Expand All @@ -48,7 +139,7 @@ <T> void unmarshal0(Class<T> type, Consumer<? super T> consumer, OPCPackage open

while (iter.hasNext()) {
try (InputStream stream = iter.next()) {
if (index == options.sheetIndex()) {
if (index == processIndex) {
processSheet(styles, readOnlySharedStringsTable, type, stream, consumer);
return;
}
Expand All @@ -59,18 +150,18 @@ <T> void unmarshal0(Class<T> type, Consumer<? super T> consumer, OPCPackage open

@SuppressWarnings("unchecked")
private <T> void processSheet(StylesTable styles,
ReadOnlySharedStringsTable readOnlySharedStringsTable,
Class<T> type,
InputStream sheetInputStream,
Consumer<? super T> consumer) {
ReadOnlySharedStringsTable readOnlySharedStringsTable,
Class<T> type,
InputStream sheetInputStream,
Consumer<? super T> consumer) {

DataFormatter formatter = new DataFormatter();
InputSource sheetSource = new InputSource(sheetInputStream);
try {
XMLReader sheetParser = SAXHelper.newXMLReader();
PoijiHandler poijiHandler = new PoijiHandler(type, options, consumer);
ContentHandler contentHandler =
new XSSFSheetXMLHandler(styles, null, readOnlySharedStringsTable, poijiHandler, formatter, false);
ContentHandler contentHandler
= new XSSFSheetXMLHandler(styles, null, readOnlySharedStringsTable, poijiHandler, formatter, false);
sheetParser.setContentHandler(contentHandler);
sheetParser.parse(sheetSource);
} catch (ParserConfigurationException | SAXException | IOException e) {
Expand Down
Loading