diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/AbstractExcelItemReader.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/AbstractExcelItemReader.java index 91630a92..d38d7bcb 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/AbstractExcelItemReader.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/AbstractExcelItemReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,23 +32,24 @@ * file. It will read the file sheet for sheet and row for row. It is loosy based on * the {@link org.springframework.batch.item.file.FlatFileItemReader} * + * @param Type used for representing a single row, such as an array * @param the type * @author Marten Deinum * @since 0.5.0 */ -public abstract class AbstractExcelItemReader extends AbstractItemCountingItemStreamItemReader implements +public abstract class AbstractExcelItemReader extends AbstractItemCountingItemStreamItemReader implements ResourceAwareItemReaderItemStream, InitializingBean { protected final Log logger = LogFactory.getLog(getClass()); private Resource resource; private int linesToSkip = 0; private int currentSheet = 0; - private RowMapper rowMapper; - private RowCallbackHandler skippedRowsCallback; + private RowMapper rowMapper; + private RowCallbackHandler skippedRowsCallback; private boolean noInput = false; private boolean strict = true; - private RowSetFactory rowSetFactory = new DefaultRowSetFactory(); - private RowSet rs; + private RowSetFactory rowSetFactory = (RowSetFactory) new DefaultRowSetFactory(); + private RowSet rs; public AbstractExcelItemReader() { super(); @@ -117,7 +118,7 @@ protected void doOpen() throws Exception { } private void openSheet() { - final Sheet sheet = this.getSheet(this.currentSheet); + final Sheet sheet = this.getSheet(this.currentSheet); this.rs =rowSetFactory.create(sheet); @@ -195,7 +196,7 @@ public void setStrict(final boolean strict) { * * @param rowMapper the {@code RowMapper} to use. */ - public void setRowMapper(final RowMapper rowMapper) { + public void setRowMapper(final RowMapper rowMapper) { this.rowMapper = rowMapper; } @@ -205,14 +206,14 @@ public void setRowMapper(final RowMapper rowMapper) { * * @param rowSetFactory the {@code RowSetFactory} to use. */ - public void setRowSetFactory(RowSetFactory rowSetFactory) { + public void setRowSetFactory(RowSetFactory rowSetFactory) { this.rowSetFactory = rowSetFactory; } /** * @param skippedRowsCallback will be called for each one of the initial skipped lines before any items are read. */ - public void setSkippedRowsCallback(final RowCallbackHandler skippedRowsCallback) { + public void setSkippedRowsCallback(final RowCallbackHandler skippedRowsCallback) { this.skippedRowsCallback = skippedRowsCallback; } } diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/ExcelFileParseException.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/ExcelFileParseException.java index b9a89286..2ae3a264 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/ExcelFileParseException.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/ExcelFileParseException.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,7 @@ public class ExcelFileParseException extends ParseException { private final String filename; private final String sheet; - private final String[] row; + private final Object row; private final int rowNumber; /** @@ -43,7 +43,7 @@ public class ExcelFileParseException extends ParseException { * @param row the row data as text */ public ExcelFileParseException(final String message, final Throwable cause, final String filename, - final String sheet, final int rowNumber, final String[] row) { + final String sheet, final int rowNumber, final Object row) { super(message, cause); this.filename = filename; this.sheet = sheet; @@ -63,7 +63,7 @@ public int getRowNumber() { return this.rowNumber; } - public String[] getRow() { + public Object getRow() { return this.row; } diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/RowCallbackHandler.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/RowCallbackHandler.java index 8b5e4cb6..da7b826d 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/RowCallbackHandler.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/RowCallbackHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,12 +20,13 @@ /** * Callback to handle skipped lines. Useful for header/footer processing. - * + * + * @param Type used for representing a single row, such as an array * @author Marten Deinum * @since 0.5.0 */ -public interface RowCallbackHandler { +public interface RowCallbackHandler { - void handleRow(RowSet rs); + void handleRow(RowSet rs); } diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/RowMapper.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/RowMapper.java index 03971f8f..8a462ac6 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/RowMapper.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/RowMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2015 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ /** * Map rows from an excel sheet to an object. * - * @param the type + * @param Type used for representing a single row, such as an array + * @param the type to map to * @author Marten Deinum * @since 0.5.0 */ -public interface RowMapper { +public interface RowMapper { /** * Implementations must implement this method to map the provided row to @@ -35,6 +36,6 @@ public interface RowMapper { * @return mapped object of type T * @throws Exception if error occured while parsing. */ - T mapRow(RowSet rs) throws Exception; + T mapRow(RowSet rs) throws Exception; } diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/Sheet.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/Sheet.java index a3d0ce7a..90faaa67 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/Sheet.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/Sheet.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,10 +19,11 @@ /** * Interface to wrap different Excel implementations like JExcel, JXL or Apache POI. * + * @param Type used for representing a single row, such as an array * @author Marten Deinum * @since 0.5.0 */ -public interface Sheet { +public interface Sheet { /** * Get the number of rows in this sheet. @@ -39,12 +40,12 @@ public interface Sheet { String getName(); /** - * Get the row as a String[]. Returns null if the row doesn't exist. + * Get the row as a {@link R}. Returns null if the row doesn't exist. * * @param rowNumber the row number to read. - * @return a String[] or null + * @return a {@link R} or null */ - String[] getRow(int rowNumber); + R getRow(int rowNumber); /** * The number of columns in this sheet. diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlItemReader.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlItemReader.java index 24dfc1a8..ed1cadec 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlItemReader.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlItemReader.java @@ -29,6 +29,7 @@ * file. It will read the file sheet for sheet and row for row. It is based on * the {@link org.springframework.batch.item.file.FlatFileItemReader} * + * @param Type used for representing a single row, such as an array * @param the type * @author Marten Deinum * @since 0.5.0 @@ -36,7 +37,7 @@ * @deprecated since JExcelAPI is an abandoned project (no release since 2009, with serious bugs remaining) */ @Deprecated -public class JxlItemReader extends AbstractExcelItemReader { +public class JxlItemReader extends AbstractExcelItemReader { private Workbook workbook; diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlSheet.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlSheet.java index ed0d6f93..0e4417b4 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlSheet.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlSheet.java @@ -1,6 +1,6 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ * @deprecated since JExcelAPI is an abandoned project (no release since 2009, with serious bugs remaining) */ @Deprecated -public class JxlSheet implements Sheet { +public class JxlSheet implements Sheet { private final jxl.Sheet delegate; private final int numberOfRows; @@ -40,7 +40,7 @@ public class JxlSheet implements Sheet { * * @param delegate the JXL sheet */ - JxlSheet(final jxl.Sheet delegate) { + public JxlSheet(final jxl.Sheet delegate) { super(); this.delegate = delegate; this.numberOfRows = this.delegate.getRows(); diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/mapping/BeanWrapperRowMapper.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/mapping/BeanWrapperRowMapper.java index 1b76ac93..eac60981 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/mapping/BeanWrapperRowMapper.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/mapping/BeanWrapperRowMapper.java @@ -1,5 +1,9 @@ package org.springframework.batch.item.excel.mapping; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + import org.springframework.batch.item.excel.RowMapper; import org.springframework.batch.item.excel.support.rowset.RowSet; import org.springframework.batch.support.DefaultPropertyEditorRegistrar; @@ -12,10 +16,6 @@ import org.springframework.validation.BindException; import org.springframework.validation.DataBinder; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - /** * {@link RowMapper} implementation based on bean property paths. The * {@link RowSet} to be mapped should have field name meta data corresponding @@ -60,10 +60,11 @@ * match is found. If more than one match is found there will be an error. * * + * @param Type used for representing a single row, such as an array * @author Marten Deinum * @since 0.5.0 */ -public class BeanWrapperRowMapper extends DefaultPropertyEditorRegistrar implements RowMapper, BeanFactoryAware, InitializingBean { +public class BeanWrapperRowMapper extends DefaultPropertyEditorRegistrar implements RowMapper, BeanFactoryAware, InitializingBean { private String name; @@ -155,7 +156,7 @@ public void afterPropertiesSet() throws Exception { * @see org.springframework.batch.item.file.mapping.FieldSetMapper#mapFieldSet(org.springframework.batch.item.file.transform.FieldSet) */ @Override - public T mapRow(RowSet rs) throws BindException { + public T mapRow(RowSet rs) throws BindException { T copy = getBean(); DataBinder binder = createBinder(copy); binder.bind(new MutablePropertyValues(getBeanProperties(copy, rs.getProperties()))); diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/mapping/PassThroughRowMapper.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/mapping/PassThroughRowMapper.java index 3987dfcf..fede520b 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/mapping/PassThroughRowMapper.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/mapping/PassThroughRowMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,16 +19,17 @@ import org.springframework.batch.item.excel.support.rowset.RowSet; /** - * Pass through {@link RowMapper} useful for passing the orginal String[] + * Pass through {@link RowMapper} useful for passing the original row * back directly rather than a mapped object. * + * @param Type used for representing a single row, such as an array * @author Marten Deinum * @since 0.5.0 */ -public class PassThroughRowMapper implements RowMapper { +public class PassThroughRowMapper implements RowMapper { @Override - public String[] mapRow(final RowSet rs) throws Exception { + public R mapRow(final RowSet rs) throws Exception { return rs.getCurrentRow(); } diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/mapping/StringArrayPassThroughRowMapper.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/mapping/StringArrayPassThroughRowMapper.java new file mode 100644 index 00000000..56b760fc --- /dev/null +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/mapping/StringArrayPassThroughRowMapper.java @@ -0,0 +1,29 @@ +/* + * Copyright 2006-2017 the original author or authors. + * + * 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.springframework.batch.item.excel.mapping; + +import org.springframework.batch.item.excel.RowMapper; + +/** + * Pass through {@link RowMapper} useful for passing the original String[] + * back directly rather than a mapped object. + * + * @author Mattias Jiderhamn + * @since 0.5.0 + */ +public class StringArrayPassThroughRowMapper extends PassThroughRowMapper { + +} diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/ArrayPoiSheet.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/ArrayPoiSheet.java new file mode 100644 index 00000000..5a57bca0 --- /dev/null +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/ArrayPoiSheet.java @@ -0,0 +1,109 @@ +/* + * Copyright 2006-2017 the original author or authors. + * + * 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.springframework.batch.item.excel.poi; + +import java.util.LinkedList; +import java.util.List; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.Row; + +/** + * Sheet implementation for Apache POI representing each row as a Object[]. + * The type of each value in the array depends on the {@link Cell#getCellType()} of the corresponding cell. + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Cell typeJava type
{@link Cell#CELL_TYPE_BOOLEAN}{@link java.lang.Boolean}
{@link Cell#CELL_TYPE_STRING}{@link java.lang.String}
{@link Cell#CELL_TYPE_BLANK}{@link java.lang.String}
{@link Cell#CELL_TYPE_NUMERIC} + {@link DateUtil#isCellDateFormatted(Cell)} = false{@link java.lang.Double}
{@link Cell#CELL_TYPE_NUMERIC} + {@link DateUtil#isCellDateFormatted(Cell)} = true{@link java.util.Date}
{@link Cell#CELL_TYPE_FORMULA}{@link java.lang.String} (evaluated)
+ * + * @author Mattias Jiderhamn + * @since 0.5.0 + */ +public class ArrayPoiSheet extends PoiSheet { + + /** + * Constructor which takes the delegate sheet. + * + * @param delegate the apache POI sheet + */ + public ArrayPoiSheet(final org.apache.poi.ss.usermodel.Sheet delegate) { + super(delegate); + } + + /** + * {@inheritDoc} + */ + @Override + public Object[] getRow(final int rowNumber) { + final Row row = this.delegate.getRow(rowNumber); + if (row == null) { + return null; + } + final List cells = new LinkedList(); + + for (int i = 0; i < getNumberOfColumns(); i++) { + Cell cell = row.getCell(i); + switch (cell.getCellType()) { + case Cell.CELL_TYPE_NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + cells.add(cell.getDateCellValue()); + } else { + cells.add(cell.getNumericCellValue()); + } + break; + case Cell.CELL_TYPE_BOOLEAN: + cells.add(cell.getBooleanCellValue()); + break; + case Cell.CELL_TYPE_STRING: + case Cell.CELL_TYPE_BLANK: + cells.add(cell.getStringCellValue()); + break; + case Cell.CELL_TYPE_FORMULA: + cells.add(getFormulaEvaluator().evaluate(cell).formatAsString()); + break; + default: + throw new IllegalArgumentException("Cannot handle cells of type " + cell.getCellType()); + } + } + return cells.toArray(new Object[cells.size()]); + } +} diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/ArrayPoiSheetFactory.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/ArrayPoiSheetFactory.java new file mode 100644 index 00000000..a5ac4620 --- /dev/null +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/ArrayPoiSheetFactory.java @@ -0,0 +1,34 @@ +/* + * Copyright 2006-2017 the original author or authors. + * + * 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.springframework.batch.item.excel.poi; + +/** + * Implementation of {@link PoiSheetFactory} that will use {@link ArrayPoiSheet} to represent each row with as a + * {@code Object[]}. + * + * @author Mattias Jiderhamn + * @since 0.5.0 + */ +public class ArrayPoiSheetFactory implements PoiSheetFactory { + + /** Create new {@link PoiSheet} instance */ + @Override + public PoiSheet newPoiSheet(org.apache.poi.ss.usermodel.Sheet delegate) { + return new ArrayPoiSheet(delegate); + } + +} diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiItemReader.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiItemReader.java index 19a3b9b7..19f242a7 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiItemReader.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiItemReader.java @@ -16,6 +16,10 @@ package org.springframework.batch.item.excel.poi; +import java.io.Closeable; +import java.io.InputStream; +import java.io.PushbackInputStream; + import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; @@ -23,28 +27,36 @@ import org.springframework.batch.item.excel.Sheet; import org.springframework.core.io.Resource; -import java.io.Closeable; -import java.io.InputStream; -import java.io.PushbackInputStream; - /** * {@link org.springframework.batch.item.ItemReader} implementation which uses apache POI to read an Excel * file. It will read the file sheet for sheet and row for row. It is based on * the {@link org.springframework.batch.item.file.FlatFileItemReader} * + * @param Type used for representing a single row, such as an array * @param the type * @author Marten Deinum * @since 0.5.0 */ -public class PoiItemReader extends AbstractExcelItemReader { +public class PoiItemReader extends AbstractExcelItemReader { private Workbook workbook; private InputStream workbookStream; + + private final PoiSheetFactory poiSheetFactory; + + public PoiItemReader(PoiSheetFactory poiSheetFactory) { + this.poiSheetFactory = poiSheetFactory; + } + + /** Create new instance that represents each row as a string array */ + public static PoiItemReader newStringArrayItemInstance() { + return new PoiItemReader(new StringArrayPoiSheetFactory()); + } @Override - protected Sheet getSheet(final int sheet) { - return new PoiSheet(this.workbook.getSheetAt(sheet)); + protected Sheet getSheet(final int sheet) { + return poiSheetFactory.newPoiSheet(this.workbook.getSheetAt(sheet)); } @Override diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiSheet.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiSheet.java index dbdeb145..18e46f68 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiSheet.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiSheet.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,25 +16,19 @@ package org.springframework.batch.item.excel.poi; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.FormulaEvaluator; -import org.apache.poi.ss.usermodel.Row; import org.springframework.batch.item.excel.Sheet; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; - /** * Sheet implementation for Apache POI. * + * @param Type used for representing a single row, such as an array * @author Marten Deinum * @since 0.5.0 */ -public class PoiSheet implements Sheet { +public abstract class PoiSheet implements Sheet { - private final org.apache.poi.ss.usermodel.Sheet delegate; + protected final org.apache.poi.ss.usermodel.Sheet delegate; private final int numberOfRows; private final String name; @@ -46,7 +40,7 @@ public class PoiSheet implements Sheet { * * @param delegate the apache POI sheet */ - PoiSheet(final org.apache.poi.ss.usermodel.Sheet delegate) { + public PoiSheet(final org.apache.poi.ss.usermodel.Sheet delegate) { super(); this.delegate = delegate; this.numberOfRows = this.delegate.getLastRowNum() + 1; @@ -68,47 +62,8 @@ public int getNumberOfRows() { public String getName() { return this.name; } - - /** - * {@inheritDoc} - */ - @Override - public String[] getRow(final int rowNumber) { - final Row row = this.delegate.getRow(rowNumber); - if (row == null) { - return null; - } - final List cells = new LinkedList(); - - for (int i = 0; i < getNumberOfColumns(); i++) { - Cell cell = row.getCell(i); - switch (cell.getCellType()) { - case Cell.CELL_TYPE_NUMERIC: - if (DateUtil.isCellDateFormatted(cell)) { - Date date = cell.getDateCellValue(); - cells.add(String.valueOf(date.getTime())); - } else { - cells.add(String.valueOf(cell.getNumericCellValue())); - } - break; - case Cell.CELL_TYPE_BOOLEAN: - cells.add(String.valueOf(cell.getBooleanCellValue())); - break; - case Cell.CELL_TYPE_STRING: - case Cell.CELL_TYPE_BLANK: - cells.add(cell.getStringCellValue()); - break; - case Cell.CELL_TYPE_FORMULA: - cells.add(getFormulaEvaluator().evaluate(cell).formatAsString()); - break; - default: - throw new IllegalArgumentException("Cannot handle cells of type " + cell.getCellType()); - } - } - return cells.toArray(new String[cells.size()]); - } - - private FormulaEvaluator getFormulaEvaluator() { + + protected FormulaEvaluator getFormulaEvaluator() { if (this.evaluator == null) { this.evaluator = delegate.getWorkbook().getCreationHelper().createFormulaEvaluator(); } diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiSheetFactory.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiSheetFactory.java new file mode 100644 index 00000000..c949971c --- /dev/null +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiSheetFactory.java @@ -0,0 +1,31 @@ +/* + * Copyright 2006-2017 the original author or authors. + * + * 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.springframework.batch.item.excel.poi; + +/** + * Interface for factory that will instantiate {@link PoiSheet}s from Apache POI {@link org.apache.poi.ss.usermodel.Sheet} + * + * @param Type used for representing a single row, such as an array + * @author Mattias Jiderhamn + * @since 0.5.0 + */ +public interface PoiSheetFactory { + + /** Create new {@link PoiSheet} instance */ + PoiSheet newPoiSheet(org.apache.poi.ss.usermodel.Sheet delegate); + +} diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/StringArrayPoiSheet.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/StringArrayPoiSheet.java new file mode 100644 index 00000000..50750466 --- /dev/null +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/StringArrayPoiSheet.java @@ -0,0 +1,83 @@ +/* + * Copyright 2006-2017 the original author or authors. + * + * 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.springframework.batch.item.excel.poi; + +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.Row; + +/** + * Sheet implementation for Apache POI representing each row as a String[]. + * + * @author Marten Deinum + * @author Mattias Jiderhamn + * @since 0.5.0 + */ +public class StringArrayPoiSheet extends PoiSheet { + + /** + * Constructor which takes the delegate sheet. + * + * @param delegate the apache POI sheet + */ + public StringArrayPoiSheet(final org.apache.poi.ss.usermodel.Sheet delegate) { + super(delegate); + } + + /** + * {@inheritDoc} + */ + @Override + public String[] getRow(final int rowNumber) { + final Row row = this.delegate.getRow(rowNumber); + if (row == null) { + return null; + } + final List cells = new LinkedList(); + + for (int i = 0; i < getNumberOfColumns(); i++) { + Cell cell = row.getCell(i); + switch (cell.getCellType()) { + case Cell.CELL_TYPE_NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + Date date = cell.getDateCellValue(); + cells.add(String.valueOf(date.getTime())); + } else { + cells.add(String.valueOf(cell.getNumericCellValue())); + } + break; + case Cell.CELL_TYPE_BOOLEAN: + cells.add(String.valueOf(cell.getBooleanCellValue())); + break; + case Cell.CELL_TYPE_STRING: + case Cell.CELL_TYPE_BLANK: + cells.add(cell.getStringCellValue()); + break; + case Cell.CELL_TYPE_FORMULA: + cells.add(getFormulaEvaluator().evaluate(cell).formatAsString()); + break; + default: + throw new IllegalArgumentException("Cannot handle cells of type " + cell.getCellType()); + } + } + return cells.toArray(new String[cells.size()]); + } +} diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/StringArrayPoiSheetFactory.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/StringArrayPoiSheetFactory.java new file mode 100644 index 00000000..e2014ae1 --- /dev/null +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/StringArrayPoiSheetFactory.java @@ -0,0 +1,34 @@ +/* + * Copyright 2006-2017 the original author or authors. + * + * 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.springframework.batch.item.excel.poi; + +/** + * Implementation of {@link PoiSheetFactory} that will use {@link StringArrayPoiSheet} to represent each row with as a + * {@code String[]}. + * + * @author Mattias Jiderhamn + * @since 0.5.0 + */ +public class StringArrayPoiSheetFactory implements PoiSheetFactory { + + /** Create new {@link PoiSheet} instance */ + @Override + public PoiSheet newPoiSheet(org.apache.poi.ss.usermodel.Sheet delegate) { + return new StringArrayPoiSheet(delegate); + } + +} diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/AbstractRowSet.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/AbstractRowSet.java new file mode 100644 index 00000000..2d51ddd4 --- /dev/null +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/AbstractRowSet.java @@ -0,0 +1,69 @@ +/* + * Copyright 2006-2017 the original author or authors. + * + * 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.springframework.batch.item.excel.support.rowset; + +import org.springframework.batch.item.excel.Sheet; + +/** + * Abstract implementation of the {@code RowSet} interface, providing the ability to traverse rows. + * + * @param Type used for representing a single row, such as an array + * @author Marten Deinum + * @author Mattias Jiderhamn + * @since 0.5.0 + * + * @see DefaultRowSetFactory + */ +public abstract class AbstractRowSet implements RowSet { + + private final Sheet sheet; + private final RowSetMetaData metaData; + + private int currentRowIndex = -1; + private R currentRow; + + public AbstractRowSet(Sheet sheet, RowSetMetaData metaData) { + this.sheet = sheet; + this.metaData = metaData; + } + + @Override + public RowSetMetaData getMetaData() { + return metaData; + } + + @Override + public boolean next() { + currentRow = null; + currentRowIndex++; + if (currentRowIndex < sheet.getNumberOfRows()) { + currentRow = sheet.getRow(currentRowIndex); + return true; + } + return false; + } + + @Override + public int getCurrentRowIndex() { + return this.currentRowIndex; + } + + @Override + public R getCurrentRow() { + return this.currentRow; + } + +} diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/ColumnNameExtractor.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/ColumnNameExtractor.java index fc9ee510..ac589ec7 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/ColumnNameExtractor.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/ColumnNameExtractor.java @@ -31,6 +31,6 @@ public interface ColumnNameExtractor { * @param sheet the sheet * @return the column names */ - String[] getColumnNames(Sheet sheet); + String[] getColumnNames(Sheet sheet); } diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSet.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSet.java index d53b4030..751c2bbd 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSet.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSet.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,10 +15,10 @@ */ package org.springframework.batch.item.excel.support.rowset; -import org.springframework.batch.item.excel.Sheet; - import java.util.Properties; +import org.springframework.batch.item.excel.Sheet; + /** * Default implementation of the {@code RowSet} interface. * @@ -27,58 +27,26 @@ * * @see org.springframework.batch.item.excel.support.rowset.DefaultRowSetFactory */ -public class DefaultRowSet implements RowSet { - - private final Sheet sheet; - private final RowSetMetaData metaData; +public class DefaultRowSet extends AbstractRowSet { - private int currentRowIndex = -1; - private String[] currentRow; - - DefaultRowSet(Sheet sheet, RowSetMetaData metaData) { - this.sheet = sheet; - this.metaData = metaData; - } - - @Override - public RowSetMetaData getMetaData() { - return metaData; - } - - @Override - public boolean next() { - currentRow = null; - currentRowIndex++; - if (currentRowIndex < sheet.getNumberOfRows()) { - currentRow = sheet.getRow(currentRowIndex); - return true; - } - return false; - } - - @Override - public int getCurrentRowIndex() { - return this.currentRowIndex; - } - - @Override - public String[] getCurrentRow() { - return this.currentRow; + DefaultRowSet(Sheet sheet, RowSetMetaData metaData) { + super(sheet, metaData); } @Override public String getColumnValue(int idx) { - return currentRow[idx]; + return getCurrentRow()[idx]; } @Override public Properties getProperties() { - final String[] names = metaData.getColumnNames(); + final String[] names = getMetaData().getColumnNames(); if (names == null) { throw new IllegalStateException("Cannot create properties without meta data"); } Properties props = new Properties(); + final String[] currentRow = getCurrentRow(); for (int i = 0; i < currentRow.length; i++) { String value = currentRow[i]; if (value != null) { diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSetFactory.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSetFactory.java index 88e5fe37..9c49aa52 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSetFactory.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSetFactory.java @@ -25,12 +25,12 @@ * @author Marten Deinum * @since 0.5.0 */ -public class DefaultRowSetFactory implements RowSetFactory { +public class DefaultRowSetFactory implements RowSetFactory { private ColumnNameExtractor columnNameExtractor = new RowNumberColumnNameExtractor(); @Override - public RowSet create(Sheet sheet) { + public RowSet create(Sheet sheet) { DefaultRowSetMetaData metaData = new DefaultRowSetMetaData(sheet); metaData.setColumnNameExtractor(columnNameExtractor); return new DefaultRowSet(sheet, metaData); diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSetMetaData.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSetMetaData.java index 3fb02edd..3ce1b29e 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSetMetaData.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/DefaultRowSetMetaData.java @@ -28,11 +28,11 @@ */ public class DefaultRowSetMetaData implements RowSetMetaData { - private final Sheet sheet; + private final Sheet sheet; private ColumnNameExtractor columnNameExtractor; - DefaultRowSetMetaData(Sheet sheet) { + public DefaultRowSetMetaData(Sheet sheet) { this.sheet = sheet; } diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowNumberColumnNameExtractor.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowNumberColumnNameExtractor.java index 2766f3ba..9f4a028b 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowNumberColumnNameExtractor.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowNumberColumnNameExtractor.java @@ -29,7 +29,7 @@ public class RowNumberColumnNameExtractor implements ColumnNameExtractor { private int headerRowNumber; @Override - public String[] getColumnNames(final Sheet sheet) { + public String[] getColumnNames(final Sheet sheet) { return sheet.getRow(headerRowNumber); } diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowSet.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowSet.java index aa236c41..eb8f6505 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowSet.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowSet.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ /** * Used by the {@code org.springframework.batch.item.excel.AbstractExcelItemReader} to abstract away * the complexities of the underlying Excel API implementations. - * + * + * @param Type used for representing a single row, such as an array * @author Marten Deinum * @since 0.5.0 */ -public interface RowSet { +public interface RowSet { /** * Retrieves the meta data (name of the sheet, number of columns, names) of this row set. @@ -49,11 +50,11 @@ public interface RowSet { int getCurrentRowIndex(); /** - * Return the current row as a String[]. + * Return the current row represented as {@link R}. * - * @return the row as a String[] + * @return the row represented as {@link R} */ - String[] getCurrentRow(); + R getCurrentRow(); /** * Retrieves the value of the indicated column in the current row as a String object. diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowSetFactory.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowSetFactory.java index 774b8e17..0606ee5a 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowSetFactory.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/support/rowset/RowSetFactory.java @@ -20,10 +20,11 @@ /** * Contract for factories which will construct a {@code RowSet} implementation. * + * @param Type used for representing a single row, such as an array * @author Marten Deinum * @since 0.5.0 */ -public interface RowSetFactory { +public interface RowSetFactory { /** * Create a rowset instance. @@ -31,5 +32,5 @@ public interface RowSetFactory { * @param sheet an Excel sheet. * @return a RowSet instance. */ - RowSet create(Sheet sheet); + RowSet create(Sheet sheet); } diff --git a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/AbstractExcelItemReaderTests.java b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/AbstractExcelItemReaderTests.java index 3947f953..a0e5c3c1 100644 --- a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/AbstractExcelItemReaderTests.java +++ b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/AbstractExcelItemReaderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ import org.junit.Before; import org.junit.Test; import org.springframework.batch.item.ExecutionContext; -import org.springframework.batch.item.excel.mapping.PassThroughRowMapper; +import org.springframework.batch.item.excel.mapping.StringArrayPassThroughRowMapper; import org.springframework.batch.item.excel.support.rowset.RowSet; import org.springframework.core.io.ClassPathResource; import org.springframework.test.util.ReflectionTestUtils; @@ -47,10 +47,10 @@ public void setup() throws Exception { this.itemReader = createExcelItemReader(); this.itemReader.setLinesToSkip(1); //First line is column names this.itemReader.setResource(new ClassPathResource("org/springframework/batch/item/excel/player.xls")); - this.itemReader.setRowMapper(new PassThroughRowMapper()); - this.itemReader.setSkippedRowsCallback(new RowCallbackHandler() { + this.itemReader.setRowMapper(new StringArrayPassThroughRowMapper()); + this.itemReader.setSkippedRowsCallback(new RowCallbackHandler() { - public void handleRow(RowSet rs) { + public void handleRow(RowSet rs) { logger.info("Skipping: " + StringUtils.arrayToCommaDelimitedString(rs.getCurrentRow())); } }); diff --git a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/BeanPropertyItemReaderTest.java b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/BeanPropertyItemReaderTest.java index 21b1ca9d..4d791df0 100644 --- a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/BeanPropertyItemReaderTest.java +++ b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/BeanPropertyItemReaderTest.java @@ -1,14 +1,14 @@ package org.springframework.batch.item.excel; +import java.util.ArrayList; +import java.util.List; + import org.junit.Before; import org.junit.Test; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.Player; import org.springframework.batch.item.excel.mapping.BeanWrapperRowMapper; -import java.util.ArrayList; -import java.util.List; - import static org.junit.Assert.*; /** @@ -32,7 +32,7 @@ public void setup() throws Exception { reader = new MockExcelItemReader(sheet); - BeanWrapperRowMapper rowMapper = new BeanWrapperRowMapper(); + BeanWrapperRowMapper rowMapper = new BeanWrapperRowMapper(); rowMapper.setTargetType(Player.class); rowMapper.afterPropertiesSet(); diff --git a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/BeanPropertyWithStaticHeaderItemReaderTest.java b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/BeanPropertyWithStaticHeaderItemReaderTest.java index a5d88e89..eed84fe0 100644 --- a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/BeanPropertyWithStaticHeaderItemReaderTest.java +++ b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/BeanPropertyWithStaticHeaderItemReaderTest.java @@ -1,5 +1,8 @@ package org.springframework.batch.item.excel; +import java.util.ArrayList; +import java.util.List; + import org.junit.Before; import org.junit.Test; import org.springframework.batch.item.ExecutionContext; @@ -8,9 +11,6 @@ import org.springframework.batch.item.excel.support.rowset.DefaultRowSetFactory; import org.springframework.batch.item.excel.support.rowset.StaticColumnNameExtractor; -import java.util.ArrayList; -import java.util.List; - import static org.junit.Assert.*; /** @@ -33,7 +33,7 @@ public void setup() throws Exception { reader = new MockExcelItemReader(sheet); - BeanWrapperRowMapper rowMapper = new BeanWrapperRowMapper(); + BeanWrapperRowMapper rowMapper = new BeanWrapperRowMapper(); rowMapper.setTargetType(Player.class); rowMapper.afterPropertiesSet(); diff --git a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/MockExcelItemReader.java b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/MockExcelItemReader.java index 7a658f7d..2c7cb72b 100644 --- a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/MockExcelItemReader.java +++ b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/MockExcelItemReader.java @@ -1,15 +1,15 @@ package org.springframework.batch.item.excel; -import org.springframework.core.io.ByteArrayResource; -import org.springframework.core.io.Resource; - import java.util.Collections; import java.util.List; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.Resource; + /** * Created by in329dei on 17-9-2014. */ -public class MockExcelItemReader extends AbstractExcelItemReader { +public class MockExcelItemReader extends AbstractExcelItemReader { private final List sheets; @@ -24,7 +24,7 @@ public MockExcelItemReader(List sheets) { } @Override - protected Sheet getSheet(int sheet) { + protected Sheet getSheet(int sheet) { return sheets.get(sheet); } diff --git a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/MockSheet.java b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/MockSheet.java index 9501bf38..1ab6670c 100644 --- a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/MockSheet.java +++ b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/MockSheet.java @@ -1,8 +1,5 @@ package org.springframework.batch.item.excel; -import jxl.Cell; -import org.springframework.batch.item.excel.jxl.JxlUtils; - import java.util.List; /** @@ -11,7 +8,7 @@ * @author Marten Deinum * @since 0.5.0 */ -public class MockSheet implements Sheet { +public class MockSheet implements Sheet { private final List rows; private final String name; diff --git a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/mapping/BeanWrapperRowMapperTest.java b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/mapping/BeanWrapperRowMapperTest.java index 2db94699..47e3403b 100644 --- a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/mapping/BeanWrapperRowMapperTest.java +++ b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/mapping/BeanWrapperRowMapperTest.java @@ -1,5 +1,8 @@ package org.springframework.batch.item.excel.mapping; +import java.util.ArrayList; +import java.util.List; + import org.junit.Test; import org.springframework.batch.item.Player; import org.springframework.batch.item.excel.MockSheet; @@ -11,9 +14,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; -import java.util.ArrayList; -import java.util.List; - import static org.junit.Assert.*; /** @@ -30,7 +30,7 @@ public void givenNoNameWhenInitCompleteThenIllegalStateShouldOccur() throws Exce @Test public void givenAValidRowWhenMappingThenAValidPlayerShouldBeConstructed() throws Exception { - BeanWrapperRowMapper mapper = new BeanWrapperRowMapper(); + BeanWrapperRowMapper mapper = new BeanWrapperRowMapper(); mapper.setTargetType(Player.class); mapper.afterPropertiesSet(); @@ -40,7 +40,7 @@ public void givenAValidRowWhenMappingThenAValidPlayerShouldBeConstructed() throw MockSheet sheet = new MockSheet("players", rows); - RowSet rs = new DefaultRowSetFactory().create(sheet); + RowSet rs = new DefaultRowSetFactory().create(sheet); rs.next(); rs.next(); @@ -60,7 +60,7 @@ public void givenAValidRowWhenMappingThenAValidPlayerShouldBeConstructed() throw public void givenAValidRowWhenMappingThenAValidPlayerShouldBeConstructedBasedOnPrototype() throws Exception { ApplicationContext ctx = new AnnotationConfigApplicationContext(TestConfig.class); - BeanWrapperRowMapper mapper = new BeanWrapperRowMapper(); + BeanWrapperRowMapper mapper = new BeanWrapperRowMapper(); mapper.setPrototypeBeanName("player"); mapper.setBeanFactory(ctx); mapper.afterPropertiesSet(); @@ -70,7 +70,7 @@ public void givenAValidRowWhenMappingThenAValidPlayerShouldBeConstructedBasedOnP rows.add( new String[]{"AbduKa00", "Abdul-Jabbar", "Karim", "rb", "1974", "1996"}); MockSheet sheet = new MockSheet("players", rows); - RowSet rs = new DefaultRowSetFactory().create(sheet); + RowSet rs = new DefaultRowSetFactory().create(sheet); rs.next(); rs.next(); Player p = mapper.mapRow(rs); diff --git a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/mapping/PassThroughRowMapperTest.java b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/mapping/StringArrayPassThroughRowMapperTest.java similarity index 83% rename from spring-batch-excel/src/test/java/org/springframework/batch/item/excel/mapping/PassThroughRowMapperTest.java rename to spring-batch-excel/src/test/java/org/springframework/batch/item/excel/mapping/StringArrayPassThroughRowMapperTest.java index 7c4948d2..6a6d7bd3 100644 --- a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/mapping/PassThroughRowMapperTest.java +++ b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/mapping/StringArrayPassThroughRowMapperTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,13 +15,13 @@ */ package org.springframework.batch.item.excel.mapping; +import java.util.Collections; + import org.junit.Test; import org.springframework.batch.item.excel.MockSheet; import org.springframework.batch.item.excel.support.rowset.DefaultRowSetFactory; import org.springframework.batch.item.excel.support.rowset.RowSet; -import java.util.Collections; - import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertTrue; @@ -30,16 +30,16 @@ * * @author Marten Deinum */ -public class PassThroughRowMapperTest { +public class StringArrayPassThroughRowMapperTest { - private final PassThroughRowMapper rowMapper = new PassThroughRowMapper(); + private final StringArrayPassThroughRowMapper rowMapper = new StringArrayPassThroughRowMapper(); @Test public void mapRowShouldReturnSameValues() throws Exception { final String[] row = new String[]{"foo", "bar", "baz"}; MockSheet sheet = new MockSheet("mock", Collections.singletonList( row)); - RowSet rs = new DefaultRowSetFactory().create(sheet); + RowSet rs = new DefaultRowSetFactory().create(sheet); assertTrue(rs.next()); assertArrayEquals(row, this.rowMapper.mapRow(rs)); } diff --git a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/poi/PoiItemReaderXlsTest.java b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/poi/PoiItemReaderXlsTest.java index ef27200c..2f880f75 100644 --- a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/poi/PoiItemReaderXlsTest.java +++ b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/poi/PoiItemReaderXlsTest.java @@ -15,8 +15,6 @@ */ package org.springframework.batch.item.excel.poi; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.springframework.batch.item.excel.AbstractExcelItemReader; import org.springframework.batch.item.excel.AbstractExcelItemReaderTests; @@ -24,7 +22,7 @@ public class PoiItemReaderXlsTest extends AbstractExcelItemReaderTests { @Override protected AbstractExcelItemReader createExcelItemReader() { - return new PoiItemReader(); + return PoiItemReader.newStringArrayItemInstance(); } } diff --git a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/poi/PoiItemReaderXlsxTest.java b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/poi/PoiItemReaderXlsxTest.java index b0a98f1f..b51e8302 100644 --- a/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/poi/PoiItemReaderXlsxTest.java +++ b/spring-batch-excel/src/test/java/org/springframework/batch/item/excel/poi/PoiItemReaderXlsxTest.java @@ -28,6 +28,6 @@ protected void configureItemReader(AbstractExcelItemReader itemReader) { @Override protected AbstractExcelItemReader createExcelItemReader() { - return new PoiItemReader(); + return PoiItemReader.newStringArrayItemInstance(); } }