annotate src/main/java/de/intevation/lada/importer/laf/LafFormat.java @ 711:3ec358698b4d

Code style and documentation.
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 22 Jul 2015 16:03:03 +0200
parents 9e733f44d8b0
children
rev   line source
626
9e733f44d8b0 Code style and comments.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 610
diff changeset
1 /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
9e733f44d8b0 Code style and comments.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 610
diff changeset
2 * Software engineering by Intevation GmbH
9e733f44d8b0 Code style and comments.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 610
diff changeset
3 *
9e733f44d8b0 Code style and comments.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 610
diff changeset
4 * This file is Free Software under the GNU GPL (v>=3)
9e733f44d8b0 Code style and comments.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 610
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out
9e733f44d8b0 Code style and comments.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 610
diff changeset
6 * the documentation coming with IMIS-Labordaten-Application for details.
9e733f44d8b0 Code style and comments.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 610
diff changeset
7 */
610
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
8 package de.intevation.lada.importer.laf;
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 java.io.IOException;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
11 import java.io.InputStream;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
12 import java.io.StringReader;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
13 import java.util.LinkedList;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
14 import java.util.List;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
15 import java.util.regex.Pattern;
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 import javax.json.Json;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
18 import javax.json.JsonArray;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
19 import javax.json.JsonException;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
20 import javax.json.JsonObject;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
21 import javax.json.JsonReader;
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 /**
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
24 * The LAFFormat reads the config file
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
25 * (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
26 * objects for each entry.
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 * @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
29 */
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
30 public class LafFormat
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
31 {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
32 private JsonObject fileContent;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
33
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
34 /**
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
35 * Reads the config file.
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
36 *
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
37 * @param fileName Path to the config file.
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
38 * @return success
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
39 */
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
40 public boolean readConfigFile(String fileName) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
41 try {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
42 InputStream inputStream =
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
43 LafFormat.class.getResourceAsStream(fileName);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
44 int ch;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
45 StringBuilder builder = new StringBuilder();
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
46 while((ch = inputStream.read()) != -1) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
47 builder.append((char)ch);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
48 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
49 JsonReader reader =
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
50 Json.createReader(new StringReader(builder.toString()));
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
51 fileContent = reader.readObject();
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
52 return true;
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 catch (IOException ioe) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
55 return false;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
56 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
57 catch (JsonException je) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
58 return false;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
59 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
60 }
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 /**
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
63 * 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
64 * The Entity type can be one of:
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
65 * * "probe"
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
66 * * "messung"
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
67 * * "ort"
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
68 *
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
69 * @param dataType The entity type
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
70 * @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
71 */
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
72 public List<EntryFormat> getFormat(String dataType) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
73 List<EntryFormat> formats = new LinkedList<EntryFormat>();
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
74 try {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
75 JsonArray block = fileContent.getJsonArray(dataType);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
76 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
77 JsonObject jEntry = block.getJsonObject(i);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
78 EntryFormat entry = new EntryFormat();
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
79 entry.setKey(jEntry.getString("key"));
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
80 Pattern pattern =
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
81 Pattern.compile(
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
82 jEntry.getString("regex"),
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
83 Pattern.MULTILINE);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
84 entry.setPattern(pattern);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
85 entry.setDefaultValue(jEntry.get("default"));
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
86 formats.add(entry);
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
87 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
88 return formats;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
89 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
90 catch (JsonException e) {
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
91 return null;
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
92 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
93 }
374a2e78cec5 Added importer impl for laf file format.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
94 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)