Commit fb1b7d38 authored by Matija Obreza's avatar Matija Obreza
Browse files

SpreadsheetUtil added

parent f8469e04
......@@ -24,7 +24,6 @@ import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.croptrust.excel.templates.ExcelToJSON;
......@@ -46,7 +45,7 @@ import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
public class ExcelToJSON2Impl implements ExcelToJSON {
/** The Constant LOG. */
private final static Logger LOG = LoggerFactory.getLogger(ExcelToJSON2Impl.class);
final static Logger LOG = LoggerFactory.getLogger(ExcelToJSON2Impl.class);
/** JsonPath configuration */
private static Configuration jsonPathConfig;
......@@ -133,7 +132,7 @@ public class ExcelToJSON2Impl implements ExcelToJSON {
final JsonPath compiledPath = JsonPath.compile(jsonPath);
final Cell cell = row.getCell(i);
final Object cellValue = cellValue(cell);
final Object cellValue = SpreadsheetUtil.cellValue(cell);
if (LOG.isTraceEnabled()) {
LOG.trace("Mapping {} to {}", cellValue, jsonPath);
......@@ -289,38 +288,4 @@ public class ExcelToJSON2Impl implements ExcelToJSON {
return l;
}
/**
* Cell value.
*
* @param cell the cell
* @return the object
*/
private Object cellValue(final Cell cell) {
if (cell == null) {
return null;
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue();
case Cell.CELL_TYPE_NUMERIC:
return cell.getNumericCellValue();
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
case Cell.CELL_TYPE_BLANK:
return null;
case Cell.CELL_TYPE_ERROR:
LOG.debug("Cell contains #ERROR: {}", cell.getErrorCellValue());
return null;
case Cell.CELL_TYPE_FORMULA:
LOG.debug("Cell contains a formula");
final FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper()
.createFormulaEvaluator();
return cellValue(evaluator.evaluateInCell(cell));
default:
LOG.warn("Unsupported cell type {}", cell.getCellType());
return null;
}
}
}
......@@ -24,7 +24,6 @@ import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.croptrust.excel.templates.ExcelToJSON;
......@@ -134,7 +133,7 @@ public class ExcelToJSONImpl implements ExcelToJSON {
final JsonPath compiledPath = JsonPath.compile(jsonPath);
final Cell cell = row.getCell(i);
final Object cellValue = cellValue(cell);
final Object cellValue = SpreadsheetUtil.cellValue(cell);
if (LOG.isTraceEnabled()) {
LOG.trace("Mapping {} to {}", cellValue, jsonPath);
......@@ -304,39 +303,4 @@ public class ExcelToJSONImpl implements ExcelToJSON {
}
}
}
/**
* Cell value.
*
* @param cell the cell
* @return the object
*/
private Object cellValue(final Cell cell) {
if (cell == null) {
return null;
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue();
case Cell.CELL_TYPE_NUMERIC:
return cell.getNumericCellValue();
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
case Cell.CELL_TYPE_BLANK:
return null;
case Cell.CELL_TYPE_ERROR:
LOG.debug("Cell contains #ERROR: {}", cell.getErrorCellValue());
return null;
case Cell.CELL_TYPE_FORMULA:
LOG.debug("Cell contains a formula");
final FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper()
.createFormulaEvaluator();
return cellValue(evaluator.evaluateInCell(cell));
default:
LOG.warn("Unsupported cell type {}", cell.getCellType());
return null;
}
}
}
......@@ -24,7 +24,6 @@ import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.croptrust.excel.templates.ExcelToJSON;
......@@ -57,7 +56,7 @@ public class ExcelToMapsJSONImpl implements ExcelToJSON {
/*
* (non-Javadoc)
*
*
* @see org.croptrust.excel.templates.ExcelToJSON#readAsJson(org.apache.poi.ss.usermodel.Sheet, java.lang.String[],
* java.lang.String, boolean, boolean)
*/
......@@ -133,7 +132,7 @@ public class ExcelToMapsJSONImpl implements ExcelToJSON {
final JsonPath compiledPath = JsonPath.compile(jsonPath);
final Cell cell = row.getCell(i);
final Object cellValue = cellValue(cell);
final Object cellValue = SpreadsheetUtil.cellValue(cell);
if (LOG.isTraceEnabled()) {
LOG.trace("Mapping {} to {}", cellValue, jsonPath);
......@@ -394,43 +393,4 @@ public class ExcelToMapsJSONImpl implements ExcelToJSON {
}
throw new UnsupportedOperationException("Unsupported JSON type " + value.getClass());
}
/**
* Cell value.
*
* @param cell the cell
* @return the object
*/
private Object cellValue(final Cell cell) {
if (cell == null) {
return null;
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue();
case Cell.CELL_TYPE_NUMERIC:
return cell.getNumericCellValue();
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
case Cell.CELL_TYPE_BLANK:
return null;
case Cell.CELL_TYPE_ERROR:
if (LOG.isDebugEnabled()) {
LOG.debug("Cell contains #ERROR: {}", cell.getErrorCellValue());
}
return null;
case Cell.CELL_TYPE_FORMULA:
if (LOG.isTraceEnabled()) {
LOG.trace("Cell contains a formula");
}
final FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper()
.createFormulaEvaluator();
return cellValue(evaluator.evaluateInCell(cell));
default:
LOG.warn("Unsupported cell type {}", cell.getCellType());
return null;
}
}
}
/*
* Copyright 2016 Global Crop Diversity Trust, www.croptrust.org
*
* Licensed 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.croptrust.excel.templates.impl;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
public class SpreadsheetUtil {
/**
* Cell value.
*
* @param cell the cell
* @return the object
*/
static Object cellValue(final Cell cell) {
if (cell == null) {
return null;
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue();
case Cell.CELL_TYPE_NUMERIC:
return cell.getNumericCellValue();
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
case Cell.CELL_TYPE_BLANK:
return null;
case Cell.CELL_TYPE_ERROR:
ExcelToJSON2Impl.LOG.debug("Cell contains #ERROR: {}", cell.getErrorCellValue());
return null;
case Cell.CELL_TYPE_FORMULA:
ExcelToJSON2Impl.LOG.debug("Cell contains a formula");
final FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper()
.createFormulaEvaluator();
return cellValue(evaluator.evaluateInCell(cell));
default:
ExcelToJSON2Impl.LOG.warn("Unsupported cell type {}", cell.getCellType());
return null;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment