/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.way.io;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import br.com.objectos.comuns.io.xls.XlsFile;
import com.google.common.io.Files;
import com.google.common.io.Resources;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class TableReaderPojo<T> implements TableReader<T> {
private final int skip;
private final AbstractTableParser<T> parser;
private final AbstractTableFilter<T> filter;
public TableReaderPojo(int skip, AbstractTableParser<T> parser, AbstractTableFilter<T> filter) {
this.skip = skip;
this.parser = parser;
this.filter = filter;
}
@Override
public TableSet<T> readFile(File file) {
try {
InputStream stream = Files.asByteSource(file).openStream();
return readStream(stream);
} catch (IOException e) {
throw new WayIOException("Could not read table at " + file.getAbsolutePath(), e);
}
}
@Override
public TableSet<T> readResource(String resourceName) {
try {
URL url = Resources.getResource(getClass(), resourceName);
InputStream stream = Resources.asByteSource(url).openStream();
return readStream(stream);
} catch (IOException e) {
throw new WayIOException("Could not read table at " + resourceName, e);
}
}
@Override
public TableSet<T> readStream(InputStream stream) {
XlsFile xls = XlsFile
.parse(stream)
.skipFirstLines(skip);
parser.configureXls(xls);
return new TableReaderXls<T>(xls, parser, filter);
}
}