All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.io.AbstractTableParser Maven / Gradle / Ivy

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 static com.google.common.base.Preconditions.checkNotNull;
import br.com.objectos.comuns.io.Line;
import br.com.objectos.comuns.io.csv.CsvConverter;
import br.com.objectos.comuns.io.csv.CsvFile;
import br.com.objectos.comuns.io.xls.XlsConverter;
import br.com.objectos.comuns.io.xls.XlsFile;

/**
 * @author [email protected] (Marcio Endo)
 */
public abstract class AbstractTableParser {

  private XlsFile xls;
  private CsvFile csv;

  public abstract T parseLine(Sheet sheet, Line line);

  public T parseLine(Line line) {
    return parseLine(null, line);
  }

  protected void configure() {
  }

  protected  ConverterBuilder convert(Class clazz) {
    return new ConverterBuilder(clazz);
  }

  static Sheet sheetNamed(String name) {
    return new Sheet(name);
  }

  final synchronized void configureXls(XlsFile xls) {
    this.xls = checkNotNull(xls);

    try {
      configure();
    } finally {
      this.xls = null;
    }
  }

  final synchronized void configureCsv(CsvFile csv) {
    this.csv = checkNotNull(csv);

    try {
      configure();
    } finally {
      this.csv = null;
    }
  }

  protected class ConverterBuilder {

    private final Class clazz;

    public ConverterBuilder(Class clazz) {
      this.clazz = clazz;
    }

    public void with(XlsConverter xlsConverter) {
      xls.withConverter(clazz, xlsConverter);
    }

    public void with(CsvConverter csvConverter) {
      csv.withConverter(clazz, csvConverter);
    }

  }

  protected static class Sheet {

    private final String name;

    public Sheet(String name) {
      this.name = name;
    }

    public String getName() {
      return name;
    }

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy