Skip to content

Commit

Permalink
Adjust interfaces to allow representing Excel rows as other types tha…
Browse files Browse the repository at this point in the history
…n String[]. Fixes spring-projects#38
  • Loading branch information
Mattias Jiderhamn committed Mar 13, 2017
1 parent 6e6f2dc commit b21a754
Show file tree
Hide file tree
Showing 32 changed files with 379 additions and 194 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 <R> Type used for representing a single row, such as an array
* @param <T> the type
* @author Marten Deinum
* @since 0.5.0
*/
public abstract class AbstractExcelItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements
public abstract class AbstractExcelItemReader<R,T> extends AbstractItemCountingItemStreamItemReader<T> implements
ResourceAwareItemReaderItemStream<T>, InitializingBean {

protected final Log logger = LogFactory.getLog(getClass());
private Resource resource;
private int linesToSkip = 0;
private int currentSheet = 0;
private RowMapper<T> rowMapper;
private RowCallbackHandler skippedRowsCallback;
private RowMapper<R,T> rowMapper;
private RowCallbackHandler<R> skippedRowsCallback;
private boolean noInput = false;
private boolean strict = true;
private RowSetFactory rowSetFactory = new DefaultRowSetFactory();
private RowSet rs;
private RowSetFactory<R> rowSetFactory = (RowSetFactory<R>) new DefaultRowSetFactory();
private RowSet<R> rs;

public AbstractExcelItemReader() {
super();
Expand Down Expand Up @@ -117,7 +118,7 @@ protected void doOpen() throws Exception {
}

private void openSheet() {
final Sheet sheet = this.getSheet(this.currentSheet);
final Sheet<R> sheet = this.getSheet(this.currentSheet);
this.rs =rowSetFactory.create(sheet);


Expand Down Expand Up @@ -195,7 +196,7 @@ public void setStrict(final boolean strict) {
*
* @param rowMapper the {@code RowMapper} to use.
*/
public void setRowMapper(final RowMapper<T> rowMapper) {
public void setRowMapper(final RowMapper<R,T> rowMapper) {
this.rowMapper = rowMapper;
}

Expand All @@ -205,14 +206,14 @@ public void setRowMapper(final RowMapper<T> rowMapper) {
*
* @param rowSetFactory the {@code RowSetFactory} to use.
*/
public void setRowSetFactory(RowSetFactory rowSetFactory) {
public void setRowSetFactory(RowSetFactory<R> 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<R> skippedRowsCallback) {
this.skippedRowsCallback = skippedRowsCallback;
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;

/**
Expand All @@ -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;
Expand All @@ -63,7 +63,7 @@ public int getRowNumber() {
return this.rowNumber;
}

public String[] getRow() {
public Object getRow() {
return this.row;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,12 +20,13 @@

/**
* Callback to handle skipped lines. Useful for header/footer processing.
*
*
* @param <R> Type used for representing a single row, such as an array
* @author Marten Deinum
* @since 0.5.0
*/
public interface RowCallbackHandler {
public interface RowCallbackHandler<R> {

void handleRow(RowSet rs);
void handleRow(RowSet<R> rs);

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,11 +20,12 @@
/**
* Map rows from an excel sheet to an object.
*
* @param <T> the type
* @param <R> Type used for representing a single row, such as an array
* @param <T> the type to map to
* @author Marten Deinum
* @since 0.5.0
*/
public interface RowMapper<T> {
public interface RowMapper<R,T> {

/**
* Implementations must implement this method to map the provided row to
Expand All @@ -35,6 +36,6 @@ public interface RowMapper<T> {
* @return mapped object of type T
* @throws Exception if error occured while parsing.
*/
T mapRow(RowSet rs) throws Exception;
T mapRow(RowSet<R> rs) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,10 +19,11 @@
/**
* Interface to wrap different Excel implementations like JExcel, JXL or Apache POI.
*
* @param <R> Type used for representing a single row, such as an array
* @author Marten Deinum
* @since 0.5.0
*/
public interface Sheet {
public interface Sheet<R> {

/**
* Get the number of rows in this sheet.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@
* 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 <R> Type used for representing a single row, such as an array
* @param <T> the type
* @author Marten Deinum
* @since 0.5.0
*
* @deprecated since JExcelAPI is an abandoned project (no release since 2009, with serious bugs remaining)
*/
@Deprecated
public class JxlItemReader<T> extends AbstractExcelItemReader<T> {
public class JxlItemReader<R,T> extends AbstractExcelItemReader<R,T> {

private Workbook workbook;

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<String[]> {

private final jxl.Sheet delegate;
private final int numberOfRows;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -60,10 +60,11 @@
* match is found. If more than one match is found there will be an error.
*
*
* @param <R> Type used for representing a single row, such as an array
* @author Marten Deinum
* @since 0.5.0
*/
public class BeanWrapperRowMapper<T> extends DefaultPropertyEditorRegistrar implements RowMapper<T>, BeanFactoryAware, InitializingBean {
public class BeanWrapperRowMapper<R,T> extends DefaultPropertyEditorRegistrar implements RowMapper<R,T>, BeanFactoryAware, InitializingBean {

private String name;

Expand Down Expand Up @@ -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<R> rs) throws BindException {
T copy = getBean();
DataBinder binder = createBinder(copy);
binder.bind(new MutablePropertyValues(getBeanProperties(copy, rs.getProperties())));
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 <R> Type used for representing a single row, such as an array
* @author Marten Deinum
* @since 0.5.0
*/
public class PassThroughRowMapper implements RowMapper<String[]> {
public class PassThroughRowMapper<R> implements RowMapper<R, R> {

@Override
public String[] mapRow(final RowSet rs) throws Exception {
public R mapRow(final RowSet<R> rs) throws Exception {
return rs.getCurrentRow();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String[]> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,47 @@

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;
import org.springframework.batch.item.excel.AbstractExcelItemReader;
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 <R> Type used for representing a single row, such as an array
* @param <T> the type
* @author Marten Deinum
* @since 0.5.0
*/
public class PoiItemReader<T> extends AbstractExcelItemReader<T> {
public class PoiItemReader<R,T> extends AbstractExcelItemReader<R,T> {

private Workbook workbook;

private InputStream workbookStream;

private final PoiSheetFactory<R> poiSheetFactory;

public PoiItemReader(PoiSheetFactory<R> poiSheetFactory) {
this.poiSheetFactory = poiSheetFactory;
}

/** Create new instance that represents each row as a string array */
public static <T> PoiItemReader<String[], T> newStringArrayItemInstance() {
return new PoiItemReader<String[], T>(new StringArrayPoiSheetFactory());
}

@Override
protected Sheet getSheet(final int sheet) {
return new PoiSheet(this.workbook.getSheetAt(sheet));
protected Sheet<R> getSheet(final int sheet) {
return poiSheetFactory.newPoiSheet(this.workbook.getSheetAt(sheet));
}

@Override
Expand Down
Loading

0 comments on commit b21a754

Please sign in to comment.