view src/main/java/de/intevation/lada/data/importer/LAFFormat.java @ 366:567ce7697fc7 0.5

Code documentation.
author Raimund Renkert <rrenkert@intevation.de>
date Tue, 10 Sep 2013 15:55:54 +0200
parents 5844d7457dde
children 183f8116d9a6
line wrap: on
line source
package de.intevation.lada.data.importer;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * The LAFFormat reads the config file
 * (https://bfs-intern.intevation.de/Server/Importer) and creates format
 * objects for each entry.
 *
 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
 */
public class LAFFormat
{
    private JSONObject fileContent;

    /**
     * Reads the config file.
     *
     * @param fileName  Path to the config file.
     * @return success
     */
    public boolean readConfigFile(String fileName) {
        try {
            byte[] encoded = Files.readAllBytes(Paths.get(fileName));
            Charset encoding = Charset.defaultCharset();
            String content =
                encoding.decode(ByteBuffer.wrap(encoded)).toString();
            fileContent = new JSONObject(content);
            return true;
        }
        catch (IOException ioe) {
            return false;
        }
        catch (JSONException je) {
            return false;
        }
    }

    /**
     * Returns a List of EntryFormat for the requested entity type.
     * The Entity type can be one of:
     * * "probe"
     * * "messung"
     * * "ort"
     *
     * @param dataType The entity type
     * @return List of entry formats defined for the requested type.
     */
    public List<EntryFormat> getFormat(String dataType) {
        List<EntryFormat> formats = new LinkedList<EntryFormat>();
        try {
            JSONArray block = fileContent.getJSONArray(dataType);
            for (int i = 0; i < block.length(); i++) {
                JSONObject jEntry = block.getJSONObject(i);
                EntryFormat entry = new EntryFormat();
                entry.setKey(jEntry.getString("key"));
                Pattern pattern =
                    Pattern.compile(
                        jEntry.getString("regex"),
                        Pattern.MULTILINE);
                entry.setPattern(pattern);
                entry.setDefaultValue(jEntry.get("default"));
                formats.add(entry);
            }
            return formats;
        }
        catch (JSONException e) {
            return null;
        }
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)