br.com.objectos.comuns.io.xls.XlsFile 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 2011 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.comuns.io.xls;
import static com.google.common.collect.Maps.newHashMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import br.com.objectos.comuns.io.ColumnKey;
import br.com.objectos.comuns.io.ComunsIOException;
import br.com.objectos.comuns.io.ParsedLines;
import br.com.objectos.comuns.util.Booleano;
import br.com.objectos.way.base.br.Estado;
import com.google.common.base.Optional;
/**
* @author [email protected] (Marcio Endo)
*/
public class XlsFile implements ParsedLines.Builder {
private static final XlsConverter BOOLEAN = new BooleanXlsConverter("true", "false");
private static final XlsConverter BOOLEANO = new BooleanoXlsConverter("true", "false");
private static final XlsConverter DOUBLE = new DoubleXlsConverter();
private static final XlsConverter ESTADO = new EstadoXlsConverter();
private static final XlsConverter INTEGER = new IntegerXlsConverter();
private static final XlsConverter LOCAL_DATE = new LocalDateXlsConverter();
private static final XlsConverter LOCAL_DATE_TIME = new LocalDateTimeXlsConverter();
private static final XlsConverter LONG = new LongXlsConverter();
private static final XlsConverter STRING = new StringXlsConverter();
private final Map, XlsConverter>> converterMap = newHashMap();
private final Workbook workbook;
private Sheet sheet;
private int skipLines = 0;
private XlsFile(Workbook workbook) {
this.workbook = workbook;
bindDefaultConverters();
}
private void bindDefaultConverters() {
converterMap.put(ColumnKey.of(Boolean.class), BOOLEAN);
converterMap.put(ColumnKey.of(Booleano.class), BOOLEANO);
converterMap.put(ColumnKey.of(Double.class), DOUBLE);
converterMap.put(ColumnKey.of(Estado.class), ESTADO);
converterMap.put(ColumnKey.of(Integer.class), INTEGER);
converterMap.put(ColumnKey.of(LocalDate.class), LOCAL_DATE);
converterMap.put(ColumnKey.of(LocalDateTime.class), LOCAL_DATE_TIME);
converterMap.put(ColumnKey.of(Long.class), LONG);
converterMap.put(ColumnKey.of(String.class), STRING);
}
public static XlsFile parse(InputStream inputStream) {
try {
Workbook workbook = WorkbookFactory.create(inputStream);
return new XlsFile(workbook);
} catch (IOException e) {
throw new ComunsIOException(e);
} catch (InvalidFormatException e) {
throw new ComunsIOException(e);
}
}
public static XlsFile parse(File file) {
try {
FileInputStream inputStream = new FileInputStream(file);
return parse(inputStream);
} catch (FileNotFoundException e) {
String msg = String.format("Cannot build XlsFile. File %s does not exist.", file);
throw new IllegalArgumentException(msg, e);
}
}
@Override
public ParsedLines getLines() {
Sheet sheet = this.sheet != null ? this.sheet : workbook.getSheetAt(0);
return new XlsParsedLines(converterMap, sheet, skipLines);
}
public XlsFile withConverter(ColumnKey key, XlsConverter converter) {
this.converterMap.put(key, converter);
return this;
}
public XlsFile withConverter(Class type, XlsConverter converter) {
return withConverter(ColumnKey.of(type), converter);
}
public XlsFile skipFirstLines(int skipLines) {
this.skipLines = skipLines;
return this;
}
public XlsFile withSheetIndex(int index) {
this.sheet = workbook.getSheetAt(index);
return this;
}
public XlsFile withSheetName(String name) {
try {
Sheet nullable = workbook.getSheet(name);
Optional optional = Optional.fromNullable(nullable);
this.sheet = optional.get();
return this;
} catch (IllegalStateException e) {
String msg = String.format("The sheet %s does not exist.", name);
throw new IllegalArgumentException(msg);
}
}
}