Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/data/importer/LAFFormat.java @ 310:821557a17e5e
First version of the LAF importer.
The importer module currently only runs with the test application (comming in the next commit)!
* LAF Parser:
- Uses a small implementation of a state machine.
- Extracts the keys with its value or multi value.
- Uses the producer interface to generate objects.
* Attribute mapper:
- Maps the attributes defined in the configuration file to object attributes.
- Generates objects from multi value attributes.
* LAF format:
- Reads the config file
* LAF importer:
- Implemetation of the importer interface for LAF format.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Tue, 20 Aug 2013 16:13:17 +0200 |
parents | |
children | 5844d7457dde |
comparison
equal
deleted
inserted
replaced
309:ae4bf396bd3b | 310:821557a17e5e |
---|---|
1 package de.intevation.lada.data.importer; | |
2 | |
3 import java.io.IOException; | |
4 import java.nio.ByteBuffer; | |
5 import java.nio.charset.Charset; | |
6 import java.nio.file.Files; | |
7 import java.nio.file.Paths; | |
8 import java.util.LinkedList; | |
9 import java.util.List; | |
10 import java.util.regex.Pattern; | |
11 | |
12 import javax.ejb.Singleton; | |
13 | |
14 import org.json.JSONArray; | |
15 import org.json.JSONException; | |
16 import org.json.JSONObject; | |
17 | |
18 | |
19 public class LAFFormat | |
20 { | |
21 private JSONObject fileContent; | |
22 | |
23 public boolean readConfigFile(String fileName) { | |
24 try { | |
25 byte[] encoded = Files.readAllBytes(Paths.get(fileName)); | |
26 Charset encoding = Charset.defaultCharset(); | |
27 String content = | |
28 encoding.decode(ByteBuffer.wrap(encoded)).toString(); | |
29 fileContent = new JSONObject(content); | |
30 return true; | |
31 } | |
32 catch (IOException ioe) { | |
33 return false; | |
34 } | |
35 catch (JSONException je) { | |
36 return false; | |
37 } | |
38 } | |
39 | |
40 public List<EntryFormat> getFormat(String dataType) { | |
41 List<EntryFormat> formats = new LinkedList<EntryFormat>(); | |
42 try { | |
43 JSONArray block = fileContent.getJSONArray(dataType); | |
44 for (int i = 0; i < block.length(); i++) { | |
45 JSONObject jEntry = block.getJSONObject(i); | |
46 EntryFormat entry = new EntryFormat(); | |
47 entry.setKey(jEntry.getString("key")); | |
48 Pattern pattern = | |
49 Pattern.compile( | |
50 jEntry.getString("regex"), | |
51 Pattern.MULTILINE); | |
52 entry.setPattern(pattern); | |
53 entry.setDefaultValue(jEntry.get("default")); | |
54 formats.add(entry); | |
55 } | |
56 return formats; | |
57 } | |
58 catch (JSONException e) { | |
59 return null; | |
60 } | |
61 } | |
62 } |