You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ka...@apache.org on 2013/07/22 10:10:25 UTC
[12/64] [partial] Hard rename of all 'org/eobjects' folders to
'org/apache'.
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/main/java/org/eobjects/metamodel/excel/ExcelUtils.java
----------------------------------------------------------------------
diff --git a/excel/src/main/java/org/eobjects/metamodel/excel/ExcelUtils.java b/excel/src/main/java/org/eobjects/metamodel/excel/ExcelUtils.java
deleted file mode 100644
index 7b3a67e..0000000
--- a/excel/src/main/java/org/eobjects/metamodel/excel/ExcelUtils.java
+++ /dev/null
@@ -1,419 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.text.NumberFormat;
-import java.util.Date;
-import java.util.Iterator;
-
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-
-import org.apache.poi.hssf.usermodel.HSSFDateUtil;
-import org.apache.poi.hssf.usermodel.HSSFFont;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.hssf.util.HSSFColor;
-import org.apache.poi.ss.formula.FormulaParseException;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.Color;
-import org.apache.poi.ss.usermodel.Font;
-import org.apache.poi.ss.usermodel.FontUnderline;
-import org.apache.poi.ss.usermodel.FormulaError;
-import org.apache.poi.ss.usermodel.FormulaEvaluator;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.ss.usermodel.WorkbookFactory;
-import org.apache.poi.xssf.streaming.SXSSFWorkbook;
-import org.apache.poi.xssf.usermodel.XSSFCell;
-import org.apache.poi.xssf.usermodel.XSSFColor;
-import org.apache.poi.xssf.usermodel.XSSFFont;
-import org.eobjects.metamodel.MetaModelException;
-import org.eobjects.metamodel.MetaModelHelper;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.data.DataSetHeader;
-import org.eobjects.metamodel.data.DefaultRow;
-import org.eobjects.metamodel.data.EmptyDataSet;
-import org.eobjects.metamodel.data.Style;
-import org.eobjects.metamodel.data.Style.SizeUnit;
-import org.eobjects.metamodel.data.StyleBuilder;
-import org.eobjects.metamodel.query.SelectItem;
-import org.eobjects.metamodel.schema.Table;
-import org.eobjects.metamodel.util.Action;
-import org.eobjects.metamodel.util.DateUtils;
-import org.eobjects.metamodel.util.FormatHelper;
-import org.eobjects.metamodel.util.Func;
-import org.eobjects.metamodel.util.Resource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.xml.sax.XMLReader;
-
-/**
- * Convenience/reusable methods for Excel workbook handling.
- *
- * @author Kasper Sørensen
- */
-final class ExcelUtils {
-
- private static final Logger logger = LoggerFactory.getLogger(ExcelUtils.class);
-
- private static final NumberFormat _numberFormat = FormatHelper.getUiNumberFormat();
-
- private ExcelUtils() {
- // prevent instantiation
- }
-
- public static XMLReader createXmlReader() {
- try {
- SAXParserFactory saxFactory = SAXParserFactory.newInstance();
- SAXParser saxParser = saxFactory.newSAXParser();
- XMLReader sheetParser = saxParser.getXMLReader();
- return sheetParser;
- } catch (Exception e) {
- throw new MetaModelException(e);
- }
- }
-
- /**
- * Initializes a workbook instance based on a inputstream.
- *
- * @return a workbook instance based on the inputstream.
- */
- public static Workbook readWorkbook(InputStream inputStream) {
- try {
- return WorkbookFactory.create(inputStream);
- } catch (Exception e) {
- logger.error("Could not open workbook", e);
- throw new IllegalStateException("Could not open workbook", e);
- }
- }
-
- public static Workbook readWorkbook(Resource resource) {
- return resource.read(new Func<InputStream, Workbook>() {
- @Override
- public Workbook eval(InputStream inputStream) {
- return readWorkbook(inputStream);
- }
- });
- }
-
- public static boolean isXlsxFile(Resource resource) {
- if (resource == null) {
- return false;
- }
- return resource.getName().toLowerCase().endsWith(".xlsx");
- }
-
- /**
- * Initializes a workbook instance based on a {@link ExcelDataContext}.
- *
- * @return a workbook instance based on the ExcelDataContext.
- */
- public static Workbook readWorkbook(ExcelDataContext dataContext) {
- Resource resource = dataContext.getResource();
- if (!resource.isExists()) {
- if (isXlsxFile(resource)) {
- return new SXSSFWorkbook(1000);
- } else {
- return new HSSFWorkbook();
- }
- }
- return readWorkbook(resource);
- }
-
- public static void writeWorkbook(ExcelDataContext dataContext, final Workbook wb) {
- final Resource resource = dataContext.getResource();
- resource.write(new Action<OutputStream>() {
- @Override
- public void run(OutputStream outputStream) throws Exception {
- wb.write(outputStream);
- }
- });
- }
-
- public static String getCellValue(Workbook wb, Cell cell) {
- if (cell == null) {
- return null;
- }
-
- final String cellCoordinate = "(" + cell.getRowIndex() + "," + cell.getColumnIndex() + ")";
-
- final String result;
-
- switch (cell.getCellType()) {
- case Cell.CELL_TYPE_BLANK:
- result = null;
- break;
- case Cell.CELL_TYPE_BOOLEAN:
- result = Boolean.toString(cell.getBooleanCellValue());
- break;
- case Cell.CELL_TYPE_ERROR:
- String errorResult;
- try {
- byte errorCode = cell.getErrorCellValue();
- FormulaError formulaError = FormulaError.forInt(errorCode);
- errorResult = formulaError.getString();
- } catch (RuntimeException e) {
- logger.debug("Getting error code for {} failed!: {}", cellCoordinate, e.getMessage());
- if (cell instanceof XSSFCell) {
- // hack to get error string, which is available
- String value = ((XSSFCell) cell).getErrorCellString();
- errorResult = value;
- } else {
- logger.error("Couldn't handle unexpected error scenario in cell: " + cellCoordinate, e);
- throw e;
- }
- }
- result = errorResult;
- break;
- case Cell.CELL_TYPE_FORMULA:
- // result = cell.getCellFormula();
- result = getFormulaCellValue(wb, cell);
- break;
- case Cell.CELL_TYPE_NUMERIC:
- if (HSSFDateUtil.isCellDateFormatted(cell)) {
- Date date = cell.getDateCellValue();
- if (date == null) {
- result = null;
- } else {
- result = DateUtils.createDateFormat().format(date);
- }
- } else {
- // TODO: Consider not formatting it, but simple using
- // Double.toString(...)
- result = _numberFormat.format(cell.getNumericCellValue());
- }
- break;
- case Cell.CELL_TYPE_STRING:
- result = cell.getRichStringCellValue().getString();
- break;
- default:
- throw new IllegalStateException("Unknown cell type: " + cell.getCellType());
- }
-
- logger.debug("cell {} resolved to value: {}", cellCoordinate, result);
-
- return result;
- }
-
- private static String getFormulaCellValue(Workbook wb, Cell cell) {
- // first try with a cached/precalculated value
- try {
- double numericCellValue = cell.getNumericCellValue();
- // TODO: Consider not formatting it, but simple using
- // Double.toString(...)
- return _numberFormat.format(numericCellValue);
- } catch (Exception e) {
- if (logger.isInfoEnabled()) {
- logger.info("Failed to fetch cached/precalculated formula value of cell: " + cell, e);
- }
- }
-
- // evaluate cell first, if possible
- try {
- if (logger.isInfoEnabled()) {
- logger.info("cell({},{}) is a formula. Attempting to evaluate: {}",
- new Object[] { cell.getRowIndex(), cell.getColumnIndex(), cell.getCellFormula() });
- }
-
- final FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
-
- // calculates the formula and puts it's value back into the cell
- final Cell evaluatedCell = evaluator.evaluateInCell(cell);
-
- return getCellValue(wb, evaluatedCell);
- } catch (RuntimeException e) {
- logger.warn("Exception occurred while evaluating formula at position ({},{}): {}", new Object[] { cell.getRowIndex(),
- cell.getColumnIndex(), e.getMessage() });
- // Some exceptions we simply log - result will be then be the
- // actual formula
- if (e instanceof FormulaParseException) {
- logger.error("Parse exception occurred while evaluating cell formula: " + cell, e);
- } else if (e instanceof IllegalArgumentException) {
- logger.error("Illegal formula argument occurred while evaluating cell formula: " + cell, e);
- } else {
- logger.error("Unexpected exception occurred while evaluating cell formula: " + cell, e);
- }
- }
-
- // last resort: return the string formula
- return cell.getCellFormula();
- }
-
- public static Style getCellStyle(Workbook workbook, Cell cell) {
- if (cell == null) {
- return Style.NO_STYLE;
- }
- final CellStyle cellStyle = cell.getCellStyle();
-
- final short fontIndex = cellStyle.getFontIndex();
- final Font font = workbook.getFontAt(fontIndex);
- final StyleBuilder styleBuilder = new StyleBuilder();
-
- // Font bold, italic, underline
- if (font.getBoldweight() >= Font.BOLDWEIGHT_BOLD) {
- styleBuilder.bold();
- }
- if (font.getItalic()) {
- styleBuilder.italic();
- }
- if (font.getUnderline() != FontUnderline.NONE.getByteValue()) {
- styleBuilder.underline();
- }
-
- // Font size
- final Font stdFont = workbook.getFontAt((short) 0);
- final short fontSize = font.getFontHeightInPoints();
- if (stdFont.getFontHeightInPoints() != fontSize) {
- styleBuilder.fontSize(fontSize, SizeUnit.PT);
- }
-
- // Font color
- final short colorIndex = font.getColor();
- if (font instanceof HSSFFont) {
- if (colorIndex != HSSFFont.COLOR_NORMAL) {
- final HSSFWorkbook wb = (HSSFWorkbook) workbook;
- HSSFColor color = wb.getCustomPalette().getColor(colorIndex);
- if (color != null) {
- short[] triplet = color.getTriplet();
- styleBuilder.foreground(triplet);
- }
- }
- } else if (font instanceof XSSFFont) {
- XSSFFont xssfFont = (XSSFFont) font;
-
- XSSFColor color = xssfFont.getXSSFColor();
- if (color != null) {
- String argbHex = color.getARGBHex();
- if (argbHex != null) {
- styleBuilder.foreground(argbHex.substring(2));
- }
- }
- } else {
- throw new IllegalStateException("Unexpected font type: " + (font == null ? "null" : font.getClass()) + ")");
- }
-
- // Background color
- if (cellStyle.getFillPattern() == 1) {
- Color color = cellStyle.getFillForegroundColorColor();
- if (color instanceof HSSFColor) {
- short[] triplet = ((HSSFColor) color).getTriplet();
- if (triplet != null) {
- styleBuilder.background(triplet);
- }
- } else if (color instanceof XSSFColor) {
- String argb = ((XSSFColor) color).getARGBHex();
- if (argb != null) {
- styleBuilder.background(argb.substring(2));
- }
- } else {
- throw new IllegalStateException("Unexpected color type: " + (color == null ? "null" : color.getClass()) + ")");
- }
- }
-
- // alignment
- switch (cellStyle.getAlignment()) {
- case CellStyle.ALIGN_LEFT:
- styleBuilder.leftAligned();
- break;
- case CellStyle.ALIGN_RIGHT:
- styleBuilder.rightAligned();
- break;
- case CellStyle.ALIGN_CENTER:
- styleBuilder.centerAligned();
- break;
- case CellStyle.ALIGN_JUSTIFY:
- styleBuilder.justifyAligned();
- break;
- }
-
- return styleBuilder.create();
- }
-
- public static Iterator<Row> getRowIterator(Sheet sheet, ExcelConfiguration configuration, boolean jumpToDataRows) {
- final Iterator<Row> iterator;
- if (configuration.isSkipEmptyLines()) {
- iterator = sheet.rowIterator();
- } else {
- iterator = new ZeroBasedRowIterator(sheet);
- }
-
- if (jumpToDataRows) {
- final int columnNameLineNumber = configuration.getColumnNameLineNumber();
- if (columnNameLineNumber != ExcelConfiguration.NO_COLUMN_NAME_LINE) {
- // iterate past the column headers
- if (iterator.hasNext()) {
- iterator.next();
- }
- for (int i = 1; i < columnNameLineNumber; i++) {
- if (iterator.hasNext()) {
- iterator.next();
- } else {
- // no more rows!
- break;
- }
- }
- }
- }
-
- return iterator;
- }
-
- /**
- * Creates a MetaModel row based on an Excel row
- *
- * @param workbook
- * @param row
- * @param selectItems
- * select items of the columns in the table
- * @return
- */
- public static DefaultRow createRow(Workbook workbook, Row row, DataSetHeader header) {
- final int size = header.size();
- final String[] values = new String[size];
- final Style[] styles = new Style[size];
- if (row != null) {
- for (int i = 0; i < size; i++) {
- final int columnNumber = header.getSelectItem(i).getColumn().getColumnNumber();
- final Cell cell = row.getCell(columnNumber);
- final String value = ExcelUtils.getCellValue(workbook, cell);
- final Style style = ExcelUtils.getCellStyle(workbook, cell);
- values[i] = value;
- styles[i] = style;
- }
- }
-
- return new DefaultRow(header, values, styles);
- }
-
- public static DataSet getDataSet(Workbook workbook, Sheet sheet, Table table, ExcelConfiguration configuration) {
- final SelectItem[] selectItems = MetaModelHelper.createSelectItems(table.getColumns());
- final Iterator<Row> rowIterator = getRowIterator(sheet, configuration, true);
- if (!rowIterator.hasNext()) {
- // no more rows!
- return new EmptyDataSet(selectItems);
- }
-
- final DataSet dataSet = new XlsDataSet(selectItems, workbook, rowIterator);
- return dataSet;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/main/java/org/eobjects/metamodel/excel/SpreadsheetReaderDelegate.java
----------------------------------------------------------------------
diff --git a/excel/src/main/java/org/eobjects/metamodel/excel/SpreadsheetReaderDelegate.java b/excel/src/main/java/org/eobjects/metamodel/excel/SpreadsheetReaderDelegate.java
deleted file mode 100644
index 8678edf..0000000
--- a/excel/src/main/java/org/eobjects/metamodel/excel/SpreadsheetReaderDelegate.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import java.io.InputStream;
-
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.Table;
-import org.eobjects.metamodel.util.Ref;
-
-/**
- * Delegate for spreadsheet-implementation specific operations in an
- * {@link ExcelDataContext}.
- *
- * @author Kasper Sørensen
- */
-interface SpreadsheetReaderDelegate {
-
- public void notifyTablesModified(Ref<InputStream> inputStreamRef);
-
- public Schema createSchema(InputStream inputStream, String schemaName)
- throws Exception;
-
- public DataSet executeQuery(InputStream inputStream, Table table,
- Column[] columns, int maxRows) throws Exception;
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/main/java/org/eobjects/metamodel/excel/XlsDataSet.java
----------------------------------------------------------------------
diff --git a/excel/src/main/java/org/eobjects/metamodel/excel/XlsDataSet.java b/excel/src/main/java/org/eobjects/metamodel/excel/XlsDataSet.java
deleted file mode 100644
index b18fbcb..0000000
--- a/excel/src/main/java/org/eobjects/metamodel/excel/XlsDataSet.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import java.util.Iterator;
-
-import org.apache.poi.ss.usermodel.Workbook;
-import org.eobjects.metamodel.data.AbstractDataSet;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.data.Row;
-import org.eobjects.metamodel.query.SelectItem;
-
-/**
- * Stream {@link DataSet} implementation for Excel support.
- *
- * @author Kasper Sørensen
- */
-final class XlsDataSet extends AbstractDataSet {
-
- private final Iterator<org.apache.poi.ss.usermodel.Row> _rowIterator;
- private final Workbook _workbook;
-
- private volatile org.apache.poi.ss.usermodel.Row _row;
- private volatile boolean _closed;
-
- /**
- * Creates an XLS dataset
- *
- * @param selectItems
- * the selectitems representing the columns of the table
- * @param workbook
- * @param rowIterator
- */
- public XlsDataSet(SelectItem[] selectItems, Workbook workbook,
- Iterator<org.apache.poi.ss.usermodel.Row> rowIterator) {
- super(selectItems);
- _workbook = workbook;
- _rowIterator = rowIterator;
- _closed = false;
- }
-
- @Override
- public boolean next() {
- if (_rowIterator.hasNext()) {
- _row = _rowIterator.next();
- return true;
- } else {
- _row = null;
- _closed = true;
- return false;
- }
- }
-
- @Override
- public Row getRow() {
- if (_closed) {
- return null;
- }
-
- return ExcelUtils.createRow(_workbook, _row, getHeader());
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxRowCallback.java
----------------------------------------------------------------------
diff --git a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxRowCallback.java b/excel/src/main/java/org/eobjects/metamodel/excel/XlsxRowCallback.java
deleted file mode 100644
index 37bf14f..0000000
--- a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxRowCallback.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import java.util.List;
-
-import org.eobjects.metamodel.data.Style;
-
-/**
- * Callback for read rows in an XLSX spreadsheet.
- *
- * @author Kasper Sørensen
- */
-interface XlsxRowCallback {
-
- public boolean row(int rowNumber, List<String> values, List<Style> styles);
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxRowPublisherAction.java
----------------------------------------------------------------------
diff --git a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxRowPublisherAction.java b/excel/src/main/java/org/eobjects/metamodel/excel/XlsxRowPublisherAction.java
deleted file mode 100644
index 85bbd58..0000000
--- a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxRowPublisherAction.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import java.io.InputStream;
-import java.util.List;
-
-import org.apache.poi.xssf.eventusermodel.XSSFReader;
-import org.eobjects.metamodel.data.RowPublisher;
-import org.eobjects.metamodel.data.Style;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.util.Action;
-import org.eobjects.metamodel.util.FileHelper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.xml.sax.InputSource;
-import org.xml.sax.XMLReader;
-
-class XlsxRowPublisherAction implements Action<RowPublisher> {
-
- private static final Logger logger = LoggerFactory
- .getLogger(XlsxRowPublisherAction.class);
-
- private final ExcelConfiguration _configuration;
- private final Column[] _columns;
- private final String _relationshipId;
- private final XSSFReader _xssfReader;
-
- public XlsxRowPublisherAction(ExcelConfiguration configuration,
- Column[] columns, String relationshipId, XSSFReader xssfReader) {
- _configuration = configuration;
- _columns = columns;
- _relationshipId = relationshipId;
- _xssfReader = xssfReader;
- }
-
- @Override
- public void run(final RowPublisher publisher) throws Exception {
- final InputStream sheetData = _xssfReader.getSheet(_relationshipId);
-
- final XlsxRowCallback rowCallback = new XlsxRowCallback() {
- @Override
- public boolean row(int rowNumber, List<String> values,
- List<Style> styles) {
- if (_configuration.getColumnNameLineNumber() != ExcelConfiguration.NO_COLUMN_NAME_LINE) {
- final int zeroBasedLineNumber = _configuration.getColumnNameLineNumber() - 1;
- if (rowNumber <= zeroBasedLineNumber) {
- // skip header rows
- return true;
- }
- }
-
- Object[] rowData = new Object[_columns.length];
- Style[] styleData = new Style[_columns.length];
- for (int i = 0; i < _columns.length; i++) {
- int columnNumber = _columns[i].getColumnNumber();
- if (columnNumber < values.size()) {
- rowData[i] = values.get(columnNumber);
- styleData[i] = styles.get(columnNumber);
- } else {
- rowData[i] = null;
- styleData[i] = Style.NO_STYLE;
- }
- }
-
- return publisher.publish(rowData, styleData);
- }
- };
- final XlsxSheetToRowsHandler handler = new XlsxSheetToRowsHandler(
- rowCallback, _xssfReader, _configuration);
-
- final XMLReader sheetParser = ExcelUtils.createXmlReader();
- sheetParser.setContentHandler(handler);
- sheetParser.setErrorHandler(handler);
- try {
- sheetParser.parse(new InputSource(sheetData));
- } catch (XlsxStopParsingException e) {
- logger.debug("Parsing stop signal thrown");
- } catch (Exception e) {
- logger.warn("Unexpected error occurred while parsing", e);
- throw e;
- } finally {
- publisher.finished();
- FileHelper.safeClose(sheetData);
- }
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxSheetToRowsHandler.java
----------------------------------------------------------------------
diff --git a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxSheetToRowsHandler.java b/excel/src/main/java/org/eobjects/metamodel/excel/XlsxSheetToRowsHandler.java
deleted file mode 100644
index 2a6b9ad..0000000
--- a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxSheetToRowsHandler.java
+++ /dev/null
@@ -1,341 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
-
-import org.apache.poi.hssf.usermodel.HSSFDateUtil;
-import org.apache.poi.ss.usermodel.BuiltinFormats;
-import org.apache.poi.ss.usermodel.DataFormatter;
-import org.apache.poi.ss.usermodel.DateUtil;
-import org.apache.poi.ss.usermodel.FillPatternType;
-import org.apache.poi.ss.usermodel.FontUnderline;
-import org.apache.poi.xssf.eventusermodel.XSSFReader;
-import org.apache.poi.xssf.model.SharedStringsTable;
-import org.apache.poi.xssf.model.StylesTable;
-import org.apache.poi.xssf.usermodel.XSSFCellStyle;
-import org.apache.poi.xssf.usermodel.XSSFColor;
-import org.apache.poi.xssf.usermodel.XSSFFont;
-import org.apache.poi.xssf.usermodel.XSSFRichTextString;
-import org.eobjects.metamodel.data.Style;
-import org.eobjects.metamodel.data.Style.SizeUnit;
-import org.eobjects.metamodel.data.StyleBuilder;
-import org.eobjects.metamodel.util.DateUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.xml.sax.Attributes;
-import org.xml.sax.SAXException;
-import org.xml.sax.helpers.DefaultHandler;
-
-/**
- * XML handler for transforming a sheet into rows. Uses an
- * {@link XlsxRowCallback} to publish identified rows.
- *
- * @author Kasper Sørensen
- */
-final class XlsxSheetToRowsHandler extends DefaultHandler {
-
- private static final Logger logger = LoggerFactory
- .getLogger(XlsxSheetToRowsHandler.class);
-
- private static enum XssfDataType {
- BOOL, ERROR, FORMULA, INLINESTR, SSTINDEX, NUMBER,
- }
-
- // global variables
- private final XlsxRowCallback _callback;
- private final ExcelConfiguration _configuration;
- private final StylesTable _stylesTable;
- private final SharedStringsTable _sharedStringTable;
-
- // variables used to hold information about the current rows
- private int _rowNumber;
- private final List<String> _rowValues;
- private final List<Style> _styles;
-
- // variables used to hold information about the current visited cells
- private final StringBuilder _value;
- private final StyleBuilder _style;
- private boolean _inCell;
- private boolean _inFormula;
- private int _columnNumber;
- private XssfDataType _dataType;
- private int _formatIndex;
- private String _formatString;
-
- public XlsxSheetToRowsHandler(XlsxRowCallback callback,
- XSSFReader xssfReader, ExcelConfiguration configuration)
- throws Exception {
- _callback = callback;
- _configuration = configuration;
-
- _sharedStringTable = xssfReader.getSharedStringsTable();
- _stylesTable = xssfReader.getStylesTable();
-
- _value = new StringBuilder();
- _style = new StyleBuilder();
- _rowValues = new ArrayList<String>();
- _styles = new ArrayList<Style>();
- _rowNumber = -1;
- _inCell = false;
- _inFormula = false;
- }
-
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if ("row".equals(qName)) {
- // element is a row
-
- // excel row numbers are 1-based
- int rowNumber = Integer.parseInt(attributes.getValue("r"));
- rowNumber = rowNumber - 1;
-
- if (_configuration.isSkipEmptyLines()) {
- _rowNumber++;
- } else {
- while (_rowNumber + 1 < rowNumber) {
- // empty lines are not skipped, so dispatch empty lines
- _rowNumber++;
- List<String> emptyValues = Collections.emptyList();
- List<Style> emptyStyles = Collections.emptyList();
- _callback.row(_rowNumber, emptyValues, emptyStyles);
- }
- _rowNumber = rowNumber;
- }
- } else if ("c".equals(qName)) {
- // element is a cell
-
- _inCell = true;
-
- final String r = attributes.getValue("r");
- int firstDigit = -1;
- for (int c = 0; c < r.length(); ++c) {
- if (Character.isDigit(r.charAt(c))) {
- firstDigit = c;
- break;
- }
- }
- _columnNumber = nameToColumn(r.substring(0, firstDigit));
-
- // Set up defaults.
- _dataType = XssfDataType.NUMBER;
- _formatIndex = -1;
- _formatString = null;
-
- final String cellType = attributes.getValue("t");
- if ("b".equals(cellType)) {
- _dataType = XssfDataType.BOOL;
- } else if ("e".equals(cellType)) {
- _dataType = XssfDataType.ERROR;
- } else if ("inlineStr".equals(cellType)) {
- _dataType = XssfDataType.INLINESTR;
- } else if ("s".equals(cellType)) {
- _dataType = XssfDataType.SSTINDEX;
- } else if ("str".equals(cellType)) {
- _dataType = XssfDataType.FORMULA;
- }
-
- String cellStyleStr = attributes.getValue("s");
- if (cellStyleStr != null) {
- // It's a number, but almost certainly one
- // with a special style or format
- int styleIndex = Integer.parseInt(cellStyleStr);
- XSSFCellStyle style = _stylesTable.getStyleAt(styleIndex);
-
- configureStyle(style);
-
- if (_dataType == XssfDataType.NUMBER) {
- this._formatIndex = style.getDataFormat();
- this._formatString = style.getDataFormatString();
- if (this._formatString == null) {
- this._formatString = BuiltinFormats
- .getBuiltinFormat(this._formatIndex);
- }
- }
- }
- } else if (_inCell && "f".equals(qName)) {
- // skip the actual formula line
- _inFormula = true;
- }
- }
-
- private void configureStyle(XSSFCellStyle style) {
- XSSFFont font = style.getFont();
- if (font.getBold()) {
- _style.bold();
- }
- if (font.getItalic()) {
- _style.italic();
- }
- if (font.getUnderline() != FontUnderline.NONE.getByteValue()) {
- _style.underline();
- }
-
- if (style.getFillPatternEnum() == FillPatternType.SOLID_FOREGROUND) {
- XSSFColor fillForegroundXSSFColor = style
- .getFillForegroundXSSFColor();
- String argb = fillForegroundXSSFColor.getARGBHex();
- if (argb != null) {
- _style.background(argb.substring(2));
- }
- }
-
- final XSSFFont stdFont = _stylesTable.getStyleAt(0).getFont();
- final short fontHeight = style.getFont().getFontHeightInPoints();
- if (stdFont.getFontHeightInPoints() != fontHeight) {
- _style.fontSize(fontHeight, SizeUnit.PT);
- }
-
- XSSFColor fontColor = style.getFont().getXSSFColor();
- if (fontColor != null) {
- String argbHex = fontColor.getARGBHex();
- if (argbHex != null) {
- _style.foreground(argbHex.substring(2));
- }
- }
-
- switch (style.getAlignmentEnum()) {
- case LEFT:
- _style.leftAligned();
- break;
- case RIGHT:
- _style.rightAligned();
- break;
- case CENTER:
- _style.centerAligned();
- break;
- case JUSTIFY:
- _style.justifyAligned();
- break;
- }
-
- }
-
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- if ("row".equals(qName)) {
- // element was a row
- boolean next = _callback.row(_rowNumber, _rowValues, _styles);
- if (!next) {
- throw new XlsxStopParsingException();
- }
- _rowValues.clear();
- _styles.clear();
- } else if ("c".equals(qName)) {
- // element was a cell
-
- _inCell = false;
-
- while (_rowValues.size() < _columnNumber) {
- _rowValues.add(null);
- _styles.add(Style.NO_STYLE);
- }
-
- _rowValues.add(createValue());
- _styles.add(_style.create());
- _value.setLength(0);
- _style.reset();
- } else if (_inFormula && "f".equals(qName)) {
- // skip the actual formula line
- _inFormula = false;
- }
- }
-
- private String createValue() {
- if (_value.length() == 0) {
- return null;
- }
-
- switch (_dataType) {
-
- case BOOL:
- char first = _value.charAt(0);
- return first == '0' ? "false" : "true";
- case ERROR:
- logger.warn("Error-cell occurred: {}", _value);
- return _value.toString();
- case FORMULA:
- return _value.toString();
- case INLINESTR:
- XSSFRichTextString rtsi = new XSSFRichTextString(_value.toString());
- return rtsi.toString();
- case SSTINDEX:
- String sstIndex = _value.toString();
- int idx = Integer.parseInt(sstIndex);
- XSSFRichTextString rtss = new XSSFRichTextString(
- _sharedStringTable.getEntryAt(idx));
- return rtss.toString();
- case NUMBER:
- final String numberString = _value.toString();
- if (_formatString != null) {
- DataFormatter formatter = getDataFormatter();
- if (HSSFDateUtil.isADateFormat(_formatIndex, _formatString)) {
- Date date = DateUtil.getJavaDate(Double
- .parseDouble(numberString));
- return DateUtils.createDateFormat().format(date);
- }
- return formatter.formatRawCellContents(
- Double.parseDouble(numberString), _formatIndex,
- _formatString);
- } else {
- if (numberString.endsWith(".0")) {
- // xlsx only stores doubles, so integers get ".0" appended
- // to them
- return numberString.substring(0, numberString.length() - 2);
- }
- return numberString;
- }
- default:
- logger.error("Unsupported data type: {}", _dataType);
- return "";
- }
- }
-
- private DataFormatter getDataFormatter() {
- return new DataFormatter();
- }
-
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- if (_inCell && !_inFormula) {
- _value.append(ch, start, length);
- }
- }
-
- /**
- * Converts an Excel column name like "C" to a zero-based index.
- *
- * @param name
- * @return Index corresponding to the specified name
- */
- private int nameToColumn(String name) {
- int column = -1;
- for (int i = 0; i < name.length(); ++i) {
- int c = name.charAt(i);
- column = (column + 1) * 26 + c - 'A';
- }
- return column;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxSpreadsheetReaderDelegate.java
----------------------------------------------------------------------
diff --git a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxSpreadsheetReaderDelegate.java b/excel/src/main/java/org/eobjects/metamodel/excel/XlsxSpreadsheetReaderDelegate.java
deleted file mode 100644
index 423d38f..0000000
--- a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxSpreadsheetReaderDelegate.java
+++ /dev/null
@@ -1,192 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
-import org.apache.poi.openxml4j.opc.OPCPackage;
-import org.apache.poi.xssf.eventusermodel.XSSFReader;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.data.RowPublisherDataSet;
-import org.eobjects.metamodel.data.Style;
-import org.eobjects.metamodel.query.SelectItem;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.schema.ColumnType;
-import org.eobjects.metamodel.schema.MutableColumn;
-import org.eobjects.metamodel.schema.MutableSchema;
-import org.eobjects.metamodel.schema.MutableTable;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.Table;
-import org.eobjects.metamodel.util.AlphabeticSequence;
-import org.eobjects.metamodel.util.FileHelper;
-import org.eobjects.metamodel.util.Ref;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.xml.sax.InputSource;
-import org.xml.sax.XMLReader;
-
-/**
- * {@link SpreadsheetReaderDelegate} implementation for the "new" XLSX format.
- * This implementation is very efficient as it uses SAX XML parsing which does
- * not bloat memory usage in the same way that POI's user model does.
- *
- * @author Kasper Sørensen
- */
-final class XlsxSpreadsheetReaderDelegate implements SpreadsheetReaderDelegate {
-
- private static final Logger logger = LoggerFactory.getLogger(XlsxSpreadsheetReaderDelegate.class);
-
- private final ExcelConfiguration _configuration;
- private final Map<String, String> _tableNamesToInternalIds;
-
- public XlsxSpreadsheetReaderDelegate(ExcelConfiguration configuration) {
- _configuration = configuration;
- _tableNamesToInternalIds = new HashMap<String, String>();
- }
-
- @Override
- public DataSet executeQuery(InputStream inputStream, Table table, Column[] columns, int maxRows) throws Exception {
- final OPCPackage pkg = OPCPackage.open(inputStream);
- final XSSFReader xssfReader = new XSSFReader(pkg);
- final String relationshipId = _tableNamesToInternalIds.get(table.getName());
-
- return buildDataSet(columns, maxRows, relationshipId, xssfReader);
- }
-
- @Override
- public Schema createSchema(InputStream inputStream, String schemaName) throws Exception {
- final MutableSchema schema = new MutableSchema(schemaName);
- final OPCPackage pkg = OPCPackage.open(inputStream);
- final XSSFReader xssfReader = new XSSFReader(pkg);
-
- final XlsxWorkbookToTablesHandler workbookToTables = new XlsxWorkbookToTablesHandler(schema,
- _tableNamesToInternalIds);
- buildTables(xssfReader, workbookToTables);
-
- for (Entry<String, String> entry : _tableNamesToInternalIds.entrySet()) {
-
- final String tableName = entry.getKey();
- final String relationshipId = entry.getValue();
-
- final MutableTable table = (MutableTable) schema.getTableByName(tableName);
-
- buildColumns(table, relationshipId, xssfReader);
- }
- return schema;
- }
-
- @Override
- public void notifyTablesModified(Ref<InputStream> inputStreamRef) {
- InputStream inputStream = inputStreamRef.get();
- final XlsxWorkbookToTablesHandler workbookToTables = new XlsxWorkbookToTablesHandler(null,
- _tableNamesToInternalIds);
- try {
- final OPCPackage pkg = OPCPackage.open(inputStream);
- final XSSFReader xssfReader = new XSSFReader(pkg);
- buildTables(xssfReader, workbookToTables);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- } finally {
- FileHelper.safeClose(inputStream);
- }
- }
-
- private DataSet buildDataSet(final Column[] columns, int maxRows, final String relationshipId,
- final XSSFReader xssfReader) throws Exception {
-
- List<SelectItem> selectItems = new ArrayList<SelectItem>(columns.length);
- for (Column column : columns) {
- selectItems.add(new SelectItem(column));
- }
- final XlsxRowPublisherAction publishAction = new XlsxRowPublisherAction(_configuration, columns,
- relationshipId, xssfReader);
-
- return new RowPublisherDataSet(selectItems.toArray(new SelectItem[selectItems.size()]), maxRows, publishAction);
- }
-
- private void buildColumns(final MutableTable table, final String relationshipId, final XSSFReader xssfReader)
- throws Exception {
- final InputStream sheetData = xssfReader.getSheet(relationshipId);
-
- final XlsxRowCallback rowCallback = new XlsxRowCallback() {
- @Override
- public boolean row(int rowNumber, List<String> values, List<Style> styles) {
- final int columnNameLineNumber = _configuration.getColumnNameLineNumber();
- if (columnNameLineNumber == ExcelConfiguration.NO_COLUMN_NAME_LINE) {
- AlphabeticSequence alphabeticSequence = new AlphabeticSequence();
- List<String> generatedColumnNames = new ArrayList<String>(values.size());
- for (String originalColumnName : values) {
- String columnName = alphabeticSequence.next();
- if (originalColumnName == null) {
- columnName = null;
- }
- generatedColumnNames.add(columnName);
- }
- buildColumns(table, generatedColumnNames);
- return false;
- } else {
- final int zeroBasedLineNumber = columnNameLineNumber - 1;
- if (rowNumber >= zeroBasedLineNumber) {
- buildColumns(table, values);
- return false;
- }
- }
- return true;
- }
- };
- final XlsxSheetToRowsHandler handler = new XlsxSheetToRowsHandler(rowCallback, xssfReader, _configuration);
-
- final XMLReader sheetParser = ExcelUtils.createXmlReader();
- sheetParser.setContentHandler(handler);
- try {
- sheetParser.parse(new InputSource(sheetData));
- } catch (XlsxStopParsingException e) {
- logger.debug("Parsing stop signal thrown");
- } finally {
- FileHelper.safeClose(sheetData);
- }
- }
-
- protected void buildColumns(final MutableTable table, final List<String> columnNames) {
- int columnNumber = 0;
- for (String columnName : columnNames) {
- if (columnName != null || !_configuration.isSkipEmptyColumns()) {
- if (columnName == null) {
- columnName = "[Column " + (columnNumber + 1) + "]";
- }
- table.addColumn(new MutableColumn(columnName, ColumnType.VARCHAR, table, columnNumber, true));
- }
- columnNumber++;
- }
- }
-
- private void buildTables(final XSSFReader xssfReader, final XlsxWorkbookToTablesHandler workbookToTables)
- throws Exception {
- final InputStream workbookData = xssfReader.getWorkbookData();
- final XMLReader workbookParser = ExcelUtils.createXmlReader();
- workbookParser.setContentHandler(workbookToTables);
- workbookParser.parse(new InputSource(workbookData));
- FileHelper.safeClose(workbookData);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxStopParsingException.java
----------------------------------------------------------------------
diff --git a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxStopParsingException.java b/excel/src/main/java/org/eobjects/metamodel/excel/XlsxStopParsingException.java
deleted file mode 100644
index ea2ba4c..0000000
--- a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxStopParsingException.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import org.xml.sax.SAXException;
-
-/**
- * Exception thrown when all elements of interest in a spreadsheet have been
- * parsed.
- *
- * @author Kasper Sørensen
- */
-final class XlsxStopParsingException extends SAXException {
-
- private static final long serialVersionUID = 1L;
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxWorkbookToTablesHandler.java
----------------------------------------------------------------------
diff --git a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxWorkbookToTablesHandler.java b/excel/src/main/java/org/eobjects/metamodel/excel/XlsxWorkbookToTablesHandler.java
deleted file mode 100644
index 3302ab7..0000000
--- a/excel/src/main/java/org/eobjects/metamodel/excel/XlsxWorkbookToTablesHandler.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import java.util.Map;
-
-import org.eobjects.metamodel.schema.MutableSchema;
-import org.eobjects.metamodel.schema.MutableTable;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.Table;
-import org.eobjects.metamodel.schema.TableType;
-import org.xml.sax.Attributes;
-import org.xml.sax.SAXException;
-import org.xml.sax.helpers.DefaultHandler;
-
-/**
- * XML handler for transforming a workbook document into {@link Table}s in a
- * MetaModel {@link Schema}.
- *
- * @author Kasper Sørensen
- */
-final class XlsxWorkbookToTablesHandler extends DefaultHandler {
-
- private final MutableSchema _schema;
- private final Map<String, String> _tableNamesToRelationshipIds;
-
- public XlsxWorkbookToTablesHandler(MutableSchema schema,
- Map<String, String> tableNamesToRelationshipIds) {
- _schema = schema;
- _tableNamesToRelationshipIds = tableNamesToRelationshipIds;
- }
-
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if ("sheet".equals(qName)) {
- String name = attributes.getValue("name");
- assert name != null;
- String relationId = attributes.getValue("r:id");
- assert relationId != null;
-
- if (_schema != null) {
- MutableTable table = new MutableTable(name, TableType.TABLE,
- _schema);
- _schema.addTable(table);
- }
- _tableNamesToRelationshipIds.put(name, relationId);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/main/java/org/eobjects/metamodel/excel/ZeroBasedRowIterator.java
----------------------------------------------------------------------
diff --git a/excel/src/main/java/org/eobjects/metamodel/excel/ZeroBasedRowIterator.java b/excel/src/main/java/org/eobjects/metamodel/excel/ZeroBasedRowIterator.java
deleted file mode 100644
index fc18dde..0000000
--- a/excel/src/main/java/org/eobjects/metamodel/excel/ZeroBasedRowIterator.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import java.util.Iterator;
-
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-
-/**
- * An iterator implementation that iterates from the first logical (as opposed
- * to physical, which is the default in POI) row in a spreadsheet.
- *
- * @author Kasper Sørensen
- */
-final class ZeroBasedRowIterator implements Iterator<Row> {
-
- private final Sheet _sheet;
- private volatile int _rowNumber;
-
- public ZeroBasedRowIterator(Sheet sheet) {
- _sheet = sheet;
- _rowNumber = -1;
- }
-
- @Override
- public boolean hasNext() {
- return _rowNumber < _sheet.getLastRowNum();
- }
-
- @Override
- public Row next() {
- _rowNumber++;
- return _sheet.getRow(_rowNumber);
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException("remove() is not supported");
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/main/java/org/eobjects/metamodel/excel/package-info.java
----------------------------------------------------------------------
diff --git a/excel/src/main/java/org/eobjects/metamodel/excel/package-info.java b/excel/src/main/java/org/eobjects/metamodel/excel/package-info.java
deleted file mode 100644
index 4300052..0000000
--- a/excel/src/main/java/org/eobjects/metamodel/excel/package-info.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/**
- * 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.
- */
-/**
- * Module package for MS Excel spreadsheet files
- */
-package org.eobjects.metamodel.excel;
-
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/test/java/org/apache/metamodel/excel/DefaultSpreadsheetReaderDelegateTest.java
----------------------------------------------------------------------
diff --git a/excel/src/test/java/org/apache/metamodel/excel/DefaultSpreadsheetReaderDelegateTest.java b/excel/src/test/java/org/apache/metamodel/excel/DefaultSpreadsheetReaderDelegateTest.java
new file mode 100644
index 0000000..cbf9723
--- /dev/null
+++ b/excel/src/test/java/org/apache/metamodel/excel/DefaultSpreadsheetReaderDelegateTest.java
@@ -0,0 +1,244 @@
+/**
+ * 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.eobjects.metamodel.excel;
+
+import java.io.File;
+import java.lang.reflect.Field;
+
+import junit.framework.TestCase;
+
+import org.eobjects.metamodel.data.DataSet;
+import org.eobjects.metamodel.data.Row;
+import org.eobjects.metamodel.data.Style;
+import org.eobjects.metamodel.query.Query;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.Schema;
+import org.eobjects.metamodel.schema.Table;
+
+public class DefaultSpreadsheetReaderDelegateTest extends TestCase {
+
+ public void testReadAllTestResourceFiles() {
+ File[] listFiles = new File("src/test/resources").listFiles();
+ for (File file : listFiles) {
+ if (file.isFile() && file.getName().indexOf(".xls") != -1) {
+ try {
+ runTest(file);
+ } catch (Throwable e) {
+ throw new IllegalStateException("Exception in file: "
+ + file, e);
+ }
+ }
+ }
+ }
+
+ private void runTest(File file) throws Exception {
+ ExcelDataContext mainDataContext = new ExcelDataContext(file);
+ applyReaderDelegate(mainDataContext);
+
+ ExcelDataContext comparedDataContext = null;
+ if (file.getName().endsWith(".xlsx")) {
+ comparedDataContext = new ExcelDataContext(file);
+ }
+
+ Schema schema = mainDataContext.getDefaultSchema();
+ assertNotNull(schema);
+ assertEquals(file.getName(), schema.getName());
+
+ if (comparedDataContext != null) {
+ assertEquals(comparedDataContext.getDefaultSchema().getName(),
+ schema.getName());
+ }
+
+ assertEquals(DefaultSpreadsheetReaderDelegate.class,
+ mainDataContext.getSpreadsheetReaderDelegateClass());
+
+ Table[] tables = schema.getTables();
+ assertTrue(tables.length > 0);
+
+ Table[] comparedTables = null;
+ if (comparedDataContext != null) {
+ assertEquals(XlsxSpreadsheetReaderDelegate.class,
+ comparedDataContext.getSpreadsheetReaderDelegateClass());
+ comparedTables = comparedDataContext.getDefaultSchema().getTables();
+ assertEquals(comparedTables.length, tables.length);
+ }
+
+ for (int i = 0; i < tables.length; i++) {
+ Table table = tables[i];
+ Column[] columns = table.getColumns();
+ Query query = mainDataContext.query().from(table).select(columns)
+ .toQuery();
+ DataSet dataSet = mainDataContext.executeQuery(query);
+
+ DataSet comparedDataSet = null;
+ if (comparedDataContext != null) {
+ Table comparedTable = comparedTables[i];
+ assertEquals(comparedTable.getName(), table.getName());
+ assertEquals(comparedTable.getColumnCount(),
+ table.getColumnCount());
+
+ Column[] comparedColumns = comparedTable.getColumns();
+ for (int j = 0; j < comparedColumns.length; j++) {
+ assertEquals(columns[j].getColumnNumber(),
+ comparedColumns[j].getColumnNumber());
+ }
+
+ Query comparedQuery = comparedDataContext.query()
+ .from(comparedTable).select(comparedColumns).toQuery();
+ comparedDataSet = comparedDataContext
+ .executeQuery(comparedQuery);
+ }
+
+ while (dataSet.next()) {
+ Row row = dataSet.getRow();
+ assertNotNull(row);
+ Object[] values = row.getValues();
+
+ assertEquals(values.length, table.getColumnCount());
+
+ if (comparedDataSet != null) {
+ boolean next = comparedDataSet.next();
+ assertTrue("No comparable row exists for: " + row, next);
+ Row comparedRow = comparedDataSet.getRow();
+ assertNotNull(comparedRow);
+ Object[] comparedValues = comparedRow.getValues();
+ assertEquals(comparedValues.length, table.getColumnCount());
+
+ for (int j = 0; j < comparedValues.length; j++) {
+ assertEquals(comparedValues[j], values[j]);
+ }
+
+ // compare styles
+ for (int j = 0; j < comparedValues.length; j++) {
+ Style style1 = comparedRow.getStyle(j);
+ Style style2 = row.getStyle(j);
+ assertEquals("Diff in style on row: " + row
+ + " (value index = " + j + ")\nStyle 1: "
+ + style1 + "\nStyle 2: " + style2 + ". ",
+ style1, style2);
+ }
+ }
+ }
+ dataSet.close();
+
+ if (comparedDataSet != null) {
+ assertFalse(comparedDataSet.next());
+ comparedDataSet.close();
+ }
+ }
+ }
+
+ /**
+ * Applies the {@link DefaultSpreadsheetReaderDelegate} through reflection.
+ *
+ * @param dataContext
+ * @throws NoSuchFieldException
+ * @throws IllegalAccessException
+ */
+ private void applyReaderDelegate(ExcelDataContext dataContext)
+ throws NoSuchFieldException, IllegalAccessException {
+ Field field = ExcelDataContext.class
+ .getDeclaredField("_spreadsheetReaderDelegate");
+ assertNotNull(field);
+ field.setAccessible(true);
+ field.set(
+ dataContext,
+ new DefaultSpreadsheetReaderDelegate(dataContext
+ .getConfiguration()));
+ }
+
+ public void testStylingOfDateCell() throws Exception {
+ ExcelDataContext dc = new ExcelDataContext(new File(
+ "src/test/resources/Spreadsheet2007.xlsx"));
+ applyReaderDelegate(dc);
+
+ Table table = dc.getDefaultSchema().getTables()[0];
+
+ final String expectedStyling = "";
+
+ DataSet dataSet = dc.query().from(table).select("date").execute();
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertFalse(dataSet.next());
+ dataSet.close();
+ }
+
+ public void testStylingOfNullCell() throws Exception {
+ ExcelDataContext dc = new ExcelDataContext(new File(
+ "src/test/resources/formulas.xlsx"));
+ applyReaderDelegate(dc);
+
+ Table table = dc.getDefaultSchema().getTables()[0];
+
+ DataSet dataSet = dc.query().from(table).select("Foo").and("Bar")
+ .where("Foo").isEquals("7").execute();
+ assertTrue(dataSet.next());
+ Row row = dataSet.getRow();
+ assertNotNull(row.getStyle(0));
+
+ final String expectedStyling = "";
+
+ assertEquals(expectedStyling, row.getStyle(0).toCSS());
+ assertNotNull(row.getStyle(1));
+ assertEquals(expectedStyling, row.getStyle(1).toCSS());
+ assertFalse(dataSet.next());
+ dataSet.close();
+
+ dataSet = dc.query().from(table).select("Foo").and("Bar").execute();
+ assertTrue(dataSet.next());
+ row = dataSet.getRow();
+ assertNotNull(row.getStyle(0));
+ assertEquals(expectedStyling, row.getStyle(0).toCSS());
+ assertNotNull(row.getStyle(1));
+ assertEquals(expectedStyling, row.getStyle(1).toCSS());
+
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertTrue(dataSet.next());
+ assertEquals(expectedStyling, dataSet.getRow().getStyle(0).toCSS());
+ assertFalse(dataSet.next());
+ dataSet.close();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/test/java/org/apache/metamodel/excel/ExcelConfigurationTest.java
----------------------------------------------------------------------
diff --git a/excel/src/test/java/org/apache/metamodel/excel/ExcelConfigurationTest.java b/excel/src/test/java/org/apache/metamodel/excel/ExcelConfigurationTest.java
new file mode 100644
index 0000000..4a3b0f8
--- /dev/null
+++ b/excel/src/test/java/org/apache/metamodel/excel/ExcelConfigurationTest.java
@@ -0,0 +1,42 @@
+/**
+ * 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.eobjects.metamodel.excel;
+
+import org.eobjects.metamodel.excel.ExcelConfiguration;
+
+import junit.framework.TestCase;
+
+public class ExcelConfigurationTest extends TestCase {
+
+ public void testToString() throws Exception {
+ ExcelConfiguration conf = new ExcelConfiguration(1, true, false);
+ assertEquals(
+ "ExcelConfiguration[columnNameLineNumber=1, skipEmptyLines=true, skipEmptyColumns=false]",
+ conf.toString());
+ }
+
+ public void testEquals() throws Exception {
+ ExcelConfiguration conf1 = new ExcelConfiguration(1, true, false);
+ ExcelConfiguration conf2 = new ExcelConfiguration(1, true, false);
+ ExcelConfiguration conf3 = new ExcelConfiguration(2, true, false);
+
+ assertEquals(conf1, conf2);
+ assertFalse(conf1.equals(conf3));
+ }
+}