annotate 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
rev   line source
310
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
1 package de.intevation.lada.data.importer;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
2
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
3 import java.io.IOException;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
4 import java.nio.ByteBuffer;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
5 import java.nio.charset.Charset;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
6 import java.nio.file.Files;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
7 import java.nio.file.Paths;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
8 import java.util.LinkedList;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
9 import java.util.List;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
10 import java.util.regex.Pattern;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
11
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
12 import org.json.JSONArray;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
13 import org.json.JSONException;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
14 import org.json.JSONObject;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
15
366
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
16 /**
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
17 * The LAFFormat reads the config file
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
18 * (https://bfs-intern.intevation.de/Server/Importer) and creates format
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
19 * objects for each entry.
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
20 *
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
21 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
22 */
310
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
23 public class LAFFormat
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
24 {
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
25 private JSONObject fileContent;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
26
366
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
27 /**
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
28 * Reads the config file.
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
29 *
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
30 * @param fileName Path to the config file.
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
31 * @return success
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
32 */
310
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
33 public boolean readConfigFile(String fileName) {
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
34 try {
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
35 byte[] encoded = Files.readAllBytes(Paths.get(fileName));
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
36 Charset encoding = Charset.defaultCharset();
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
37 String content =
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
38 encoding.decode(ByteBuffer.wrap(encoded)).toString();
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
39 fileContent = new JSONObject(content);
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
40 return true;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
41 }
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
42 catch (IOException ioe) {
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
43 return false;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
44 }
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
45 catch (JSONException je) {
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
46 return false;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
47 }
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
48 }
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
49
366
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
50 /**
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
51 * Returns a List of EntryFormat for the requested entity type.
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
52 * The Entity type can be one of:
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
53 * * "probe"
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
54 * * "messung"
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
55 * * "ort"
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
56 *
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
57 * @param dataType The entity type
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
58 * @return List of entry formats defined for the requested type.
567ce7697fc7 Code documentation.
Raimund Renkert <rrenkert@intevation.de>
parents: 321
diff changeset
59 */
310
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
60 public List<EntryFormat> getFormat(String dataType) {
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
61 List<EntryFormat> formats = new LinkedList<EntryFormat>();
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
62 try {
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
63 JSONArray block = fileContent.getJSONArray(dataType);
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
64 for (int i = 0; i < block.length(); i++) {
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
65 JSONObject jEntry = block.getJSONObject(i);
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
66 EntryFormat entry = new EntryFormat();
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
67 entry.setKey(jEntry.getString("key"));
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
68 Pattern pattern =
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
69 Pattern.compile(
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
70 jEntry.getString("regex"),
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
71 Pattern.MULTILINE);
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
72 entry.setPattern(pattern);
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
73 entry.setDefaultValue(jEntry.get("default"));
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
74 formats.add(entry);
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
75 }
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
76 return formats;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
77 }
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
78 catch (JSONException e) {
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
79 return null;
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
80 }
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
81 }
821557a17e5e First version of the LAF importer.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
82 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)