Fix inconsistent whitespace, and add a close call in the main method to avoid a resource-leak warning

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1575684 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-03-09 09:53:25 +00:00
parent 2a7cebf8c0
commit f63b47b8f6

View File

@ -82,6 +82,7 @@ public class XSSFEventBasedExcelExtractor extends POIXMLTextExtractor
POIXMLTextExtractor extractor = POIXMLTextExtractor extractor =
new XSSFEventBasedExcelExtractor(args[0]); new XSSFEventBasedExcelExtractor(args[0]);
System.out.println(extractor.getText()); System.out.println(extractor.getText());
extractor.close();
} }
/** /**
@ -252,113 +253,113 @@ public class XSSFEventBasedExcelExtractor extends POIXMLTextExtractor
super.close(); super.close();
} }
protected class SheetTextExtractor implements SheetContentsHandler { protected class SheetTextExtractor implements SheetContentsHandler {
private final StringBuffer output; private final StringBuffer output;
private boolean firstCellOfRow; private boolean firstCellOfRow;
private final Map<String, String> headerFooterMap; private final Map<String, String> headerFooterMap;
protected SheetTextExtractor() { protected SheetTextExtractor() {
this.output = new StringBuffer(); this.output = new StringBuffer();
this.firstCellOfRow = true; this.firstCellOfRow = true;
this.headerFooterMap = includeHeadersFooters ? new HashMap<String, String>() : null; this.headerFooterMap = includeHeadersFooters ? new HashMap<String, String>() : null;
} }
public void startRow(int rowNum) { public void startRow(int rowNum) {
firstCellOfRow = true; firstCellOfRow = true;
} }
public void endRow() { public void endRow() {
output.append('\n'); output.append('\n');
} }
public void cell(String cellRef, String formattedValue) { public void cell(String cellRef, String formattedValue) {
if(firstCellOfRow) { if(firstCellOfRow) {
firstCellOfRow = false; firstCellOfRow = false;
} else { } else {
output.append('\t'); output.append('\t');
} }
output.append(formattedValue); output.append(formattedValue);
} }
public void headerFooter(String text, boolean isHeader, String tagName) { public void headerFooter(String text, boolean isHeader, String tagName) {
if (headerFooterMap != null) { if (headerFooterMap != null) {
headerFooterMap.put(tagName, text); headerFooterMap.put(tagName, text);
} }
} }
/** /**
* Append the text for the named header or footer if found. * Append the text for the named header or footer if found.
*/ */
private void appendHeaderFooterText(StringBuffer buffer, String name) { private void appendHeaderFooterText(StringBuffer buffer, String name) {
String text = headerFooterMap.get(name); String text = headerFooterMap.get(name);
if (text != null && text.length() > 0) { if (text != null && text.length() > 0) {
// this is a naive way of handling the left, center, and right // this is a naive way of handling the left, center, and right
// header and footer delimiters, but it seems to be as good as // header and footer delimiters, but it seems to be as good as
// the method used by XSSFExcelExtractor // the method used by XSSFExcelExtractor
text = handleHeaderFooterDelimiter(text, "&L"); text = handleHeaderFooterDelimiter(text, "&L");
text = handleHeaderFooterDelimiter(text, "&C"); text = handleHeaderFooterDelimiter(text, "&C");
text = handleHeaderFooterDelimiter(text, "&R"); text = handleHeaderFooterDelimiter(text, "&R");
buffer.append(text).append('\n'); buffer.append(text).append('\n');
} }
} }
/** /**
* Remove the delimiter if its found at the beginning of the text, * Remove the delimiter if its found at the beginning of the text,
* or replace it with a tab if its in the middle. * or replace it with a tab if its in the middle.
*/ */
private String handleHeaderFooterDelimiter(String text, String delimiter) { private String handleHeaderFooterDelimiter(String text, String delimiter) {
int index = text.indexOf(delimiter); int index = text.indexOf(delimiter);
if (index == 0) { if (index == 0) {
text = text.substring(2); text = text.substring(2);
} else if (index > 0) { } else if (index > 0) {
text = text.substring(0, index) + "\t" + text.substring(index + 2); text = text.substring(0, index) + "\t" + text.substring(index + 2);
} }
return text; return text;
} }
/** /**
* Append the text for each header type in the same order * Append the text for each header type in the same order
* they are appended in XSSFExcelExtractor. * they are appended in XSSFExcelExtractor.
* @see XSSFExcelExtractor#getText() * @see XSSFExcelExtractor#getText()
* @see org.apache.poi.hssf.extractor.ExcelExtractor#_extractHeaderFooter(org.apache.poi.ss.usermodel.HeaderFooter) * @see org.apache.poi.hssf.extractor.ExcelExtractor#_extractHeaderFooter(org.apache.poi.ss.usermodel.HeaderFooter)
*/ */
private void appendHeaderText(StringBuffer buffer) { private void appendHeaderText(StringBuffer buffer) {
appendHeaderFooterText(buffer, "firstHeader"); appendHeaderFooterText(buffer, "firstHeader");
appendHeaderFooterText(buffer, "oddHeader"); appendHeaderFooterText(buffer, "oddHeader");
appendHeaderFooterText(buffer, "evenHeader"); appendHeaderFooterText(buffer, "evenHeader");
} }
/** /**
* Append the text for each footer type in the same order * Append the text for each footer type in the same order
* they are appended in XSSFExcelExtractor. * they are appended in XSSFExcelExtractor.
* @see XSSFExcelExtractor#getText() * @see XSSFExcelExtractor#getText()
* @see org.apache.poi.hssf.extractor.ExcelExtractor#_extractHeaderFooter(org.apache.poi.ss.usermodel.HeaderFooter) * @see org.apache.poi.hssf.extractor.ExcelExtractor#_extractHeaderFooter(org.apache.poi.ss.usermodel.HeaderFooter)
*/ */
private void appendFooterText(StringBuffer buffer) { private void appendFooterText(StringBuffer buffer) {
// append the text for each footer type in the same order // append the text for each footer type in the same order
// they are appended in XSSFExcelExtractor // they are appended in XSSFExcelExtractor
appendHeaderFooterText(buffer, "firstFooter"); appendHeaderFooterText(buffer, "firstFooter");
appendHeaderFooterText(buffer, "oddFooter"); appendHeaderFooterText(buffer, "oddFooter");
appendHeaderFooterText(buffer, "evenFooter"); appendHeaderFooterText(buffer, "evenFooter");
} }
/** /**
* Append the cell contents we have collected. * Append the cell contents we have collected.
*/ */
private void appendCellText(StringBuffer buffer) { private void appendCellText(StringBuffer buffer) {
buffer.append(output); buffer.append(output);
} }
/** /**
* Reset this <code>SheetTextExtractor</code> for the next sheet. * Reset this <code>SheetTextExtractor</code> for the next sheet.
*/ */
private void reset() { private void reset() {
output.setLength(0); output.setLength(0);
firstCellOfRow = true; firstCellOfRow = true;
if (headerFooterMap != null) { if (headerFooterMap != null) {
headerFooterMap.clear(); headerFooterMap.clear();
} }
} }
} }
} }