Mercurial > lada > lada-server
view src/main/java/de/intevation/lada/data/importer/LAFFormat.java @ 409:183f8116d9a6
Added license header to source files.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Mon, 20 Jan 2014 12:27:00 +0100 |
parents | 567ce7697fc7 |
children |
line wrap: on
line source
/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz * Software engineering by Intevation GmbH * * This file is Free Software under the GNU GPL (v>=3) * and comes with ABSOLUTELY NO WARRANTY! Check out * the documentation coming with IMIS-Labordaten-Application for details. */ 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; } } }