use try-with-resources in more places

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1817247 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2017-12-05 22:31:59 +00:00
parent 49b7fd5342
commit 37a349dc19
3 changed files with 136 additions and 132 deletions

View File

@ -209,12 +209,12 @@ public class XLSX2CSV {
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
int index = 0;
while (iter.hasNext()) {
InputStream stream = iter.next();
String sheetName = iter.getSheetName();
this.output.println();
this.output.println(sheetName + " [index=" + index + "]:");
processSheet(styles, strings, new SheetToCSV(), stream);
stream.close();
try (InputStream stream = iter.next()) {
String sheetName = iter.getSheetName();
this.output.println();
this.output.println(sheetName + " [index=" + index + "]:");
processSheet(styles, strings, new SheetToCSV(), stream);
}
++index;
}
}
@ -237,9 +237,9 @@ public class XLSX2CSV {
minColumns = Integer.parseInt(args[1]);
// The package open is instantaneous, as it should be.
OPCPackage p = OPCPackage.open(xlsxFile.getPath(), PackageAccess.READ);
XLSX2CSV xlsx2csv = new XLSX2CSV(p, System.out, minColumns);
xlsx2csv.process();
p.close();
}
try (OPCPackage p = OPCPackage.open(xlsxFile.getPath(), PackageAccess.READ)) {
XLSX2CSV xlsx2csv = new XLSX2CSV(p, System.out, minColumns);
xlsx2csv.process();
}
}
}

View File

@ -21,7 +21,10 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.util.SAXHelper;
import org.apache.poi.xssf.eventusermodel.XLSX2CSV;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
@ -33,7 +36,6 @@ import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* XSSF and SAX (Event API) basic example.
@ -41,122 +43,122 @@ import org.xml.sax.helpers.XMLReaderFactory;
* XSLX processing with the XSSF Event code.
*/
public class FromHowTo {
public void processFirstSheet(String filename) throws Exception {
try (OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ)) {
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
public void processFirstSheet(String filename) throws Exception {
try (OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ)) {
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
XMLReader parser = fetchSheetParser(sst);
// process the first sheet
InputStream sheet2 = r.getSheetsData().next();
InputSource sheetSource = new InputSource(sheet2);
parser.parse(sheetSource);
sheet2.close();
}
}
public void processAllSheets(String filename) throws Exception {
try (OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ)) {
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
Iterator<InputStream> sheets = r.getSheetsData();
while (sheets.hasNext()) {
System.out.println("Processing new sheet:\n");
InputStream sheet = sheets.next();
// process the first sheet
try (InputStream sheet = r.getSheetsData().next()) {
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
sheet.close();
System.out.println("");
}
}
}
}
}
public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {
XMLReader parser = XMLReaderFactory.createXMLReader();
ContentHandler handler = new SheetHandler(sst);
parser.setContentHandler(handler);
return parser;
}
public void processAllSheets(String filename) throws Exception {
try (OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ)) {
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
/**
* See org.xml.sax.helpers.DefaultHandler javadocs
*/
private static class SheetHandler extends DefaultHandler {
private final SharedStringsTable sst;
private String lastContents;
private boolean nextIsString;
private boolean inlineStr;
private final LruCache<Integer,String> lruCache = new LruCache<>(50);
XMLReader parser = fetchSheetParser(sst);
private static class LruCache<A,B> extends LinkedHashMap<A, B> {
private final int maxEntries;
public LruCache(final int maxEntries) {
super(maxEntries + 1, 1.0f, true);
this.maxEntries = maxEntries;
}
@Override
protected boolean removeEldestEntry(final Map.Entry<A, B> eldest) {
return super.size() > maxEntries;
}
}
private SheetHandler(SharedStringsTable sst) {
this.sst = sst;
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
// c => cell
if(name.equals("c")) {
// Print the cell reference
System.out.print(attributes.getValue("r") + " - ");
// Figure out if the value is an index in the SST
String cellType = attributes.getValue("t");
nextIsString = cellType != null && cellType.equals("s");
inlineStr = cellType != null && cellType.equals("inlineStr");
}
// Clear contents cache
lastContents = "";
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
// Process the last contents as required.
// Do now, as characters() may be called more than once
if(nextIsString) {
Integer idx = Integer.valueOf(lastContents);
lastContents = lruCache.get(idx);
if (lastContents == null && !lruCache.containsKey(idx)) {
lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
lruCache.put(idx, lastContents);
Iterator<InputStream> sheets = r.getSheetsData();
while (sheets.hasNext()) {
System.out.println("Processing new sheet:\n");
try (InputStream sheet = sheets.next()) {
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
}
nextIsString = false;
}
System.out.println("");
}
}
}
// v => contents of a cell
// Output after we've seen the string contents
if(name.equals("v") || (inlineStr && name.equals("c"))) {
System.out.println(lastContents);
}
}
public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException, ParserConfigurationException {
XMLReader parser = SAXHelper.newXMLReader();
ContentHandler handler = new SheetHandler(sst);
parser.setContentHandler(handler);
return parser;
}
@Override
/**
* See org.xml.sax.helpers.DefaultHandler javadocs
*/
private static class SheetHandler extends DefaultHandler {
private final SharedStringsTable sst;
private String lastContents;
private boolean nextIsString;
private boolean inlineStr;
private final LruCache<Integer,String> lruCache = new LruCache<>(50);
private static class LruCache<A,B> extends LinkedHashMap<A, B> {
private final int maxEntries;
public LruCache(final int maxEntries) {
super(maxEntries + 1, 1.0f, true);
this.maxEntries = maxEntries;
}
@Override
protected boolean removeEldestEntry(final Map.Entry<A, B> eldest) {
return super.size() > maxEntries;
}
}
private SheetHandler(SharedStringsTable sst) {
this.sst = sst;
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
// c => cell
if(name.equals("c")) {
// Print the cell reference
System.out.print(attributes.getValue("r") + " - ");
// Figure out if the value is an index in the SST
String cellType = attributes.getValue("t");
nextIsString = cellType != null && cellType.equals("s");
inlineStr = cellType != null && cellType.equals("inlineStr");
}
// Clear contents cache
lastContents = "";
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
// Process the last contents as required.
// Do now, as characters() may be called more than once
if(nextIsString) {
Integer idx = Integer.valueOf(lastContents);
lastContents = lruCache.get(idx);
if (lastContents == null && !lruCache.containsKey(idx)) {
lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
lruCache.put(idx, lastContents);
}
nextIsString = false;
}
// v => contents of a cell
// Output after we've seen the string contents
if(name.equals("v") || (inlineStr && name.equals("c"))) {
System.out.println(lastContents);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException { // NOSONAR
lastContents += new String(ch, start, length);
}
}
lastContents += new String(ch, start, length);
}
}
public static void main(String[] args) throws Exception {
FromHowTo howto = new FromHowTo();
howto.processFirstSheet(args[0]);
howto.processAllSheets(args[0]);
}
public static void main(String[] args) throws Exception {
FromHowTo howto = new FromHowTo();
howto.processFirstSheet(args[0]);
howto.processAllSheets(args[0]);
}
}

View File

@ -39,22 +39,24 @@ public class HybridStreaming {
private static final String SHEET_TO_STREAM = "large sheet";
public static void main(String[] args) throws IOException, SAXException {
InputStream sourceBytes = new FileInputStream("workbook.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(sourceBytes) {
/** Avoid DOM parse of large sheet */
@Override
public void parseSheet(java.util.Map<String,XSSFSheet> shIdMap, CTSheet ctSheet) {
if (!SHEET_TO_STREAM.equals(ctSheet.getName())) {
super.parseSheet(shIdMap, ctSheet);
try (InputStream sourceBytes = new FileInputStream("workbook.xlsx")) {
XSSFWorkbook workbook = new XSSFWorkbook(sourceBytes) {
/**
* Avoid DOM parse of large sheet
*/
@Override
public void parseSheet(java.util.Map<String, XSSFSheet> shIdMap, CTSheet ctSheet) {
if (!SHEET_TO_STREAM.equals(ctSheet.getName())) {
super.parseSheet(shIdMap, ctSheet);
}
}
}
};
// Having avoided a DOM-based parse of the sheet, we can stream it instead.
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(workbook.getPackage());
new XSSFSheetXMLHandler(workbook.getStylesSource(), strings, createSheetContentsHandler(), false);
workbook.close();
sourceBytes.close();
};
// Having avoided a DOM-based parse of the sheet, we can stream it instead.
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(workbook.getPackage());
new XSSFSheetXMLHandler(workbook.getStylesSource(), strings, createSheetContentsHandler(), false);
workbook.close();
}
}
private static SheetContentsHandler createSheetContentsHandler() {