annotate src/main/java/de/intevation/lada/importer/laf/LafFormat.java @ 610:374a2e78cec5

Added importer impl for laf file format.
author Raimund Renkert <raimund.renkert@intevation.de>
date Thu, 16 Apr 2015 15:49:04 +0200
parents
children 9e733f44d8b0
rev   line source
610
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
1 package de.intevation.lada.importer.laf;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
2
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
3 import java.io.IOException;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
4 import java.io.InputStream;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
5 import java.io.StringReader;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
6 import java.util.LinkedList;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
7 import java.util.List;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
8 import java.util.regex.Pattern;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
9
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
10 import javax.json.Json;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
11 import javax.json.JsonArray;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
12 import javax.json.JsonException;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
13 import javax.json.JsonObject;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
14 import javax.json.JsonReader;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
15
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
16 /**
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
17 * The LAFFormat reads the config file
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
18 * (https://bfs-intern.intevation.de/Server/Importer) and creates format
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
19 * objects for each entry.
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
20 *
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
21 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
22 */
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
23 public class LafFormat
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
24 {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
25 private JsonObject fileContent;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
26
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
27 /**
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
28 * Reads the config file.
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
29 *
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
30 * @param fileName Path to the config file.
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
31 * @return success
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
32 */
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
33 public boolean readConfigFile(String fileName) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
34 try {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
35 InputStream inputStream =
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
36 LafFormat.class.getResourceAsStream(fileName);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
37 int ch;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
38 StringBuilder builder = new StringBuilder();
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
39 while((ch = inputStream.read()) != -1) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
40 builder.append((char)ch);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
41 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
42 JsonReader reader =
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
43 Json.createReader(new StringReader(builder.toString()));
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
44 fileContent = reader.readObject();
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
45 return true;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
46 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
47 catch (IOException ioe) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
48 return false;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
49 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
50 catch (JsonException je) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
51 return false;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
52 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
53 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
54
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
55 /**
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
56 * Returns a List of EntryFormat for the requested entity type.
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
57 * The Entity type can be one of:
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
58 * * "probe"
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
59 * * "messung"
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
60 * * "ort"
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
61 *
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
62 * @param dataType The entity type
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
63 * @return List of entry formats defined for the requested type.
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
64 */
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
65 public List<EntryFormat> getFormat(String dataType) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
66 List<EntryFormat> formats = new LinkedList<EntryFormat>();
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
67 try {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
68 JsonArray block = fileContent.getJsonArray(dataType);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
69 for (int i = 0; i < block.size(); i++) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
70 JsonObject jEntry = block.getJsonObject(i);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
71 EntryFormat entry = new EntryFormat();
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
72 entry.setKey(jEntry.getString("key"));
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
73 Pattern pattern =
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
74 Pattern.compile(
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
75 jEntry.getString("regex"),
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
76 Pattern.MULTILINE);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
77 entry.setPattern(pattern);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
78 entry.setDefaultValue(jEntry.get("default"));
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
79 formats.add(entry);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
80 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
81 return formats;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
82 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
83 catch (JsonException e) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
84 return null;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
85 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
86 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
87 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)