Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/data/importer/LAFParser.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 | 2adf28ac7fe0 |
comparison
equal
deleted
inserted
replaced
309:ae4bf396bd3b | 310:821557a17e5e |
---|---|
1 package de.intevation.lada.data.importer; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 | |
6 import javax.inject.Inject; | |
7 import javax.inject.Named; | |
8 | |
9 import de.intevation.lada.model.LKommentarM; | |
10 import de.intevation.lada.model.LKommentarP; | |
11 import de.intevation.lada.model.LMessung; | |
12 import de.intevation.lada.model.LMesswert; | |
13 import de.intevation.lada.model.LOrt; | |
14 import de.intevation.lada.model.LProbe; | |
15 | |
16 | |
17 public class LAFParser { | |
18 | |
19 private static final String PROBE_NEXT = "\n%PROBE%"; | |
20 | |
21 private boolean dryRun; | |
22 | |
23 //@Inject | |
24 //@Named("lafproducer") | |
25 private Producer producer; | |
26 | |
27 List<LProbe> proben; | |
28 List<LMessung> messungen; | |
29 List<LOrt> orte; | |
30 List<LMesswert> messwerte; | |
31 List<LKommentarP> probeKommentare; | |
32 List<LKommentarM> messungKommentare; | |
33 | |
34 public LAFParser() { | |
35 this.setDryRun(false); | |
36 this.producer = new LAFProducer(); | |
37 this.proben = new ArrayList<LProbe>(); | |
38 this.messungen = new ArrayList<LMessung>(); | |
39 this.orte = new ArrayList<LOrt>(); | |
40 this.messwerte = new ArrayList<LMesswert>(); | |
41 this.probeKommentare = new ArrayList<LKommentarP>(); | |
42 this.messungKommentare = new ArrayList<LKommentarM>(); | |
43 } | |
44 | |
45 public boolean parse(String laf) | |
46 throws LAFParserException | |
47 { | |
48 boolean parsed = false; | |
49 while (laf.startsWith("%PROBE%\n")) { | |
50 parsed = true; | |
51 int nextPos = laf.indexOf(PROBE_NEXT); | |
52 String single = ""; | |
53 if (nextPos > 0) { | |
54 single = laf.substring(0, nextPos + 1); | |
55 laf = laf.substring(nextPos + 1); | |
56 readAll(single); | |
57 } | |
58 else { | |
59 readAll(laf); | |
60 laf = ""; | |
61 } | |
62 if (!this.dryRun) { | |
63 proben.add(producer.getProbe()); | |
64 messungen.addAll(producer.getMessungen()); | |
65 orte.addAll(producer.getOrte()); | |
66 messwerte.addAll(producer.getMesswerte()); | |
67 probeKommentare.addAll(producer.getProbenKommentare()); | |
68 messungKommentare.addAll(producer.getMessungsKommentare()); | |
69 producer.reset(); | |
70 } | |
71 } | |
72 if (!parsed) { | |
73 throw new LAFParserException("No %PROBE% at the begining."); | |
74 } | |
75 return parsed; | |
76 } | |
77 | |
78 private void readAll(String content) | |
79 throws LAFParserException | |
80 { | |
81 boolean key = false; | |
82 boolean value = false; | |
83 boolean header = false; | |
84 boolean white = false; | |
85 boolean string = false; | |
86 boolean multiValue = false; | |
87 String keyString = ""; | |
88 String valueString = ""; | |
89 String headerString = ""; | |
90 for (int i = 0; i < content.length(); i++) { | |
91 char current = content.charAt(i); | |
92 | |
93 if ((current == '"' || (current == ' ' && !string)) && | |
94 value && | |
95 i < content.length() - 1 && | |
96 (content.charAt(i + 1) != '\n' && | |
97 content.charAt(i + 1) != '\r')) { | |
98 multiValue = true; | |
99 } | |
100 | |
101 if (current == '"' && !string) { | |
102 string = true; | |
103 } | |
104 else if (current == '"' && string) { | |
105 string = false; | |
106 } | |
107 | |
108 if (current == ' ' && !value) { | |
109 key = false; | |
110 white = true; | |
111 continue; | |
112 } | |
113 else if (current != ' ' && white) { | |
114 value = true; | |
115 white = false; | |
116 } | |
117 else if (current == '%' && !header && !value) { | |
118 headerString = ""; | |
119 | |
120 key = false; | |
121 header = true; | |
122 } | |
123 else if ((current == '\n' || current == '\r') && header) { | |
124 header = false; | |
125 key = true; | |
126 if (!dryRun) { | |
127 if (headerString.contains("MESSUNG")) { | |
128 producer.newMessung(); | |
129 } | |
130 if (headerString.contains("ORT")) { | |
131 producer.newOrt(); | |
132 } | |
133 } | |
134 continue; | |
135 } | |
136 else if (current == '"' && !value) { | |
137 value = true; | |
138 } | |
139 else if ((current == '\n' || current == '\r') && value && !string) { | |
140 if (!multiValue && valueString.startsWith("\"")) { | |
141 valueString = | |
142 valueString.substring(1, valueString.length() - 1); | |
143 } | |
144 value = false; | |
145 multiValue = false; | |
146 key = true; | |
147 if (!this.dryRun) { | |
148 producer.addData(keyString, valueString); | |
149 } | |
150 keyString = ""; | |
151 valueString = ""; | |
152 continue; | |
153 } | |
154 else if ((current == '\n' || current == '\r') && key) { | |
155 throw new LAFParserException("No value for key: " + keyString); | |
156 } | |
157 | |
158 if (key) { | |
159 keyString += current; | |
160 } | |
161 else if (value) { | |
162 valueString += current; | |
163 } | |
164 else if (header) { | |
165 headerString += current; | |
166 } | |
167 } | |
168 if (!dryRun) { | |
169 this.producer.newMessung(); | |
170 this.producer.newOrt(); | |
171 } | |
172 } | |
173 | |
174 public boolean isDryRun() { | |
175 return dryRun; | |
176 } | |
177 | |
178 public void setDryRun(boolean dryRun) { | |
179 this.dryRun = dryRun; | |
180 } | |
181 } |