br.com.objectos.comuns.io.xls.XlsFile.java Source code

Java tutorial

Introduction

Here is the source code for br.com.objectos.comuns.io.xls.XlsFile.java

Source

/*
 * Copyright 2011 Objectos, Fbrica 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 java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

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.jabuticava.Estado;

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;

/**
 * @author Marcio Endo
 */
public class XlsFile implements ParsedLines.Builder {

    private static final XlsConverter<Boolean> BOOLEAN = new BooleanXlsConverter("true", "false");
    private static final XlsConverter<Booleano> BOOLEANO = new BooleanoXlsConverter("true", "false");
    private static final XlsConverter<Double> DOUBLE = new DoubleXlsConverter();
    private static final XlsConverter<Estado> ESTADO = new EstadoXlsConverter();
    private static final XlsConverter<Integer> INTEGER = new IntegerXlsConverter();
    private static final XlsConverter<LocalDate> LOCAL_DATE = new LocalDateXlsConverter();
    private static final XlsConverter<LocalDateTime> LOCAL_DATE_TIME = new LocalDateTimeXlsConverter();
    private static final XlsConverter<Long> LONG = new LongXlsConverter();
    private static final XlsConverter<String> STRING = new StringXlsConverter();

    private final Map<ColumnKey<?>, XlsConverter<?>> converterMap = new HashMap<>();
    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(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);
        }
    }

    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);
        }
    }

    @Override
    public ParsedLines getLines() {
        Sheet sheet = this.sheet != null ? this.sheet : workbook.getSheetAt(0);
        return new XlsParsedLines(converterMap, sheet, skipLines);
    }

    public XlsFile skipFirstLines(int skipLines) {
        this.skipLines = skipLines;
        return this;
    }

    public <T> XlsFile withConverter(ColumnKey<T> key, XlsConverter<T> converter) {
        converterMap.put(key, converter);
        return this;
    }

    public <T> XlsFile withConverter(Class<T> type, XlsConverter<T> converter) {
        return withConverter(ColumnKey.of(type), converter);
    }

    public XlsFile withSheetIndex(int index) {
        sheet = workbook.getSheetAt(index);
        return this;
    }

    public XlsFile withSheetName(String name) {
        try {
            sheet = workbook.getSheet(name);

            if (sheet == null) {
                throw new IllegalArgumentException();
            }

            return this;
        } catch (IllegalStateException e) {
            String msg = String.format("The sheet %s does not exist.", name);
            throw new IllegalArgumentException(msg);
        }
    }

}