Package com.evasion.plugin.geoloc.dataimport

Source Code of com.evasion.plugin.geoloc.dataimport.AbstractFileParser

/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
package com.evasion.plugin.geoloc.dataimport;

import com.evasion.EntityJPA;
import com.evasion.entity.geolocation.City;
import com.evasion.entity.geolocation.Country;
import com.evasion.entity.geolocation.AdminDivision;
import com.evasion.exception.EvasionException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author glon-56610
*/
public abstract class AbstractFileParser {

    /**
     * LOGGER.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(
            AbstractFileParser.class);

    private File file = null;

    private BufferedReader reader = null;

    private String nextLine = null;

    private int lineNumber = 0;

    public AbstractFileParser(File file) {
        if (file == null) {
            throw new IllegalArgumentException("file name can not be Null");
        }
        LOGGER.info("Traitement du fichier GIS {}", file.getName());
        this.file = file;
    }

    public void initParser() throws EvasionException {
        try {
            if (file.getName().endsWith(".zip") || file.getName().endsWith(".ZIP")) {

                ZipFile zf = new ZipFile(file);
                reader = new BufferedReader(extractFileToZip(zf));
            } else {
                reader = new BufferedReader(new FileReader(file));
            }
            nextLine = reader.readLine();
            nextLine = reader.readLine();
        } catch (ZipException ex) {
            throw new EvasionException("Error on read Zip File", ex);
        } catch (IOException ex) {
            throw new EvasionException("Error on init parser", ex);
        }
    }

    private InputStreamReader extractFileToZip(ZipFile zf) throws EvasionException {
        ZipEntry zipentry;
        try {
            Enumeration entries = zf.entries();
            while (entries.hasMoreElements()) {
                zipentry = (ZipEntry) entries.nextElement();
                //for each entry to be extracted
                String entryName = zipentry.getName();

                File newFile = new File(entryName);
                String directory = newFile.getParent();

                if (directory == null && newFile.isDirectory()) {
                    break;
                }
                return new InputStreamReader(zf.getInputStream(zipentry));
            }
        } catch (Exception e) {
            LOGGER.error("Extraction zip file error", e);
            close();

        }
        throw new EvasionException("Extraction zip file error");
    }

    public abstract int getNbrColomnRef();

    public abstract String separateurColumn();

    public EntityJPA next() throws EvasionException {
        try {
            EntityJPA entity = null;
            if (hasMoreElement()) {
                String[] tabLigne = nextLine.split(separateurColumn());
                if (tabLigne.length != getNbrColomnRef()) {
                    LOGGER.warn("Format de la ligne {} non valide dans le fichier ", lineNumber);
                    //return null;
                    //throw new ImportFormatException("Format de ligne  non valide.");
                } else {
                    entity = traiterLigne(tabLigne);
                }
                // préparation de la future ligne
                nextLine = reader.readLine();
                lineNumber++;
            }
            return entity;
        } catch (IOException ex) {
            LOGGER.error("IO Exception sur un readline.", ex);
            close();
            throw new EvasionException("IO Exception sur un readline.", ex);
        }
    }

    public void close() {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException ex) {
            LOGGER.error("Can not close zipFile or reader", ex);
        } finally {
            reader = null;
        }
    }

    public boolean hasMoreElement() {
        return nextLine != null && !nextLine.isEmpty();
    }

    protected abstract EntityJPA traiterLigne(String[] ligne);

    public Date formatStringToDate(String date) {
        String[] dates = date.split("-");
        return new GregorianCalendar(Integer.parseInt(dates[0]), Integer.parseInt(dates[1]), Integer.parseInt(dates[2])).getTime();
    }

    public int getLineNumber() {
        return this.lineNumber;
    }
}
TOP

Related Classes of com.evasion.plugin.geoloc.dataimport.AbstractFileParser

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.