br.com.objectos.way.io.TableReaderPojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of way-io Show documentation
Show all versions of way-io Show documentation
CSV, XLS and fixed parsers
The newest version!
/*
* 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.Encoding;
import br.com.objectos.comuns.io.csv.CsvFile;
import br.com.objectos.comuns.io.csv.CsvOptions;
import br.com.objectos.comuns.io.xls.XlsFile;
import com.google.common.io.Files;
import com.google.common.io.Resources;
/**
* @author [email protected] (Marcio Endo)
*/
class TableReaderPojo implements TableReader, CsvOptions {
private final int skip;
private final AbstractTableParser parser;
private final AbstractTableFilter filter;
private final char csvChar;
private final boolean quoted;
private final boolean escaped;
private final Encoding encoding;
public TableReaderPojo(TableReaderBuilderPojo builder) {
this.skip = builder.getSkip();
this.parser = builder.getParser();
this.filter = builder.getFilter();
this.csvChar = builder.getCsvChar();
this.quoted = builder.isQuoted();
this.escaped = builder.isEscaped();
this.encoding = builder.getEncoding();
}
@Override
public TableSet 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 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 readStream(InputStream stream) {
try {
XlsFile xls = XlsFile
.parse(stream)
.skipFirstLines(skip);
parser.configureXls(xls);
return new TableReaderXls(xls, parser, filter);
} catch (IllegalArgumentException e) {
CsvFile csv = CsvFile
.parse(stream)
.from(this)
.skipFirstLines(skip);
parser.configureCsv(csv);
return new TableReaderCsv(csv, parser, filter);
}
}
@Override
public char getDelimiter() {
return csvChar;
}
@Override
public boolean isQuoted() {
return quoted;
}
@Override
public boolean isEscaped() {
return escaped;
}
@Override
public Encoding getEncoding() {
return encoding;
}
}