Package br.com.objectos.way.io

Source Code of br.com.objectos.way.io.TableReaderPojo

/*
* 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 marcio.endo@objectos.com.br (Marcio Endo)
*/
class TableReaderPojo<T> implements TableReader<T>, CsvOptions {

  private final int skip;
  private final AbstractTableParser<T> parser;
  private final AbstractTableFilter<T> filter;

  private final char csvChar;
  private final boolean quoted;
  private final boolean escaped;
  private final Encoding encoding;

  public TableReaderPojo(TableReaderBuilderPojo<T> 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<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) {
    try {
      XlsFile xls = XlsFile
          .parse(stream)
          .skipFirstLines(skip);

      parser.configureXls(xls);
      return new TableReaderXls<T>(xls, parser, filter);

    } catch (IllegalArgumentException e) {
      CsvFile csv = CsvFile
          .parse(stream)
          .from(this)
          .skipFirstLines(skip);

      parser.configureCsv(csv);
      return new TableReaderCsv<T>(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;
  }

}
TOP

Related Classes of br.com.objectos.way.io.TableReaderPojo

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.