comparison flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java @ 1196:46127af605ba

Added parser for PRF files. TODO: Extract data and km. flys-backend/trunk@2296 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 06 Jul 2011 17:54:49 +0000
parents
children ce3dacc6ea92
comparison
equal deleted inserted replaced
1195:2b4de678e29a 1196:46127af605ba
1 package de.intevation.flys.importer;
2
3 import java.util.Map;
4 import java.util.Stack;
5 import java.util.TreeMap;
6
7 import java.util.regex.Pattern;
8 import java.util.regex.Matcher;
9
10 import java.io.File;
11 import java.io.InputStreamReader;
12 import java.io.LineNumberReader;
13 import java.io.FileInputStream;
14 import java.io.IOException;
15
16 import org.apache.log4j.Logger;
17
18 public class PRFParser
19 {
20 private static Logger log = Logger.getLogger(PRFParser.class);
21
22 public static final String ENCODING =
23 System.getProperty("flys.backend.prf.encoding", "ISO-8859-1");
24
25 public static final Pattern DATA_PATTERN =
26 Pattern.compile(
27 "\\((\\d+)x\\s*,\\s*(\\d+)\\(" +
28 "\\s*f(\\d+)\\.(\\d+)\\s*,\\s*f(\\d+)\\.(\\d+)\\s*\\)?\\)?");
29
30 public static final Pattern KM_PATTERN =
31 Pattern.compile("\\((\\d+)x\\s*,\\s*f(\\d+)\\.(\\d+)\\s*\\)?");
32
33 public static class DataFormat {
34
35 protected int deleteChars;
36 protected int maxRepetitions;
37 protected int firstIntegerPlaces;
38 protected int firstFractionPlaces;
39 protected int secondIntegerPlaces;
40 protected int secondFractionPlaces;
41
42 public DataFormat() {
43 }
44
45 public DataFormat(Matcher m) {
46 deleteChars = Integer.parseInt(m.group(1));
47 maxRepetitions = Integer.parseInt(m.group(2));
48 firstIntegerPlaces = Integer.parseInt(m.group(3));
49 firstFractionPlaces = Integer.parseInt(m.group(4));
50 secondIntegerPlaces = Integer.parseInt(m.group(5));
51 secondFractionPlaces = Integer.parseInt(m.group(6));
52 }
53
54 public void extractData(String line, Map<Double, Double> dest) {
55 //TODO: Implement me!
56 }
57 } // class DataFormat
58
59 public static class KMFormat {
60 protected int deleteChars;
61 protected int integerPlaces;
62 protected int fractionPlaces;
63
64 public KMFormat() {
65 }
66
67 public KMFormat(Matcher m) {
68 deleteChars = Integer.parseInt(m.group(1));
69 integerPlaces = Integer.parseInt(m.group(2));
70 fractionPlaces = Integer.parseInt(m.group(3));
71 }
72
73 public double extractKm(String line) {
74 // TODO: Implement me!
75 return 0d;
76 }
77 } // class KMFormat
78
79 protected Map<Double, Map<Double, Double>> data;
80
81 public PRFParser() {
82 data = new TreeMap<Double, Map<Double, Double>>();
83 }
84
85
86 public boolean parse(File file) {
87
88 if (!(file.isFile() && file.canRead())) {
89 log.warn("cannot open file '" + file + "'");
90 return false;
91 }
92
93 log.info("parsing PRF file: '" + file + "'");
94
95 LineNumberReader in = null;
96
97 try {
98 in =
99 new LineNumberReader(
100 new InputStreamReader(
101 new FileInputStream(file), ENCODING));
102
103 String line = in.readLine();
104
105 if (line == null || (line = line.trim()).length() == 0) {
106 log.warn("file is empty.");
107 return false;
108 }
109
110 Matcher m = DATA_PATTERN.matcher(line);
111
112 if (!m.matches()) {
113 log.warn("First line does not look like a PRF data pattern.");
114 return false;
115 }
116
117 DataFormat dataFormat = new DataFormat(m);
118
119 if ((line = in.readLine()) == null) {
120 log.warn("premature EOF. Expected integer in line 2");
121 return false;
122 }
123
124 try {
125 if (Integer.parseInt(line) != dataFormat.maxRepetitions) {
126 log.warn("Expected " +
127 dataFormat.maxRepetitions + " in line 2");
128 return false;
129 }
130 }
131 catch (NumberFormatException nfe) {
132 log.warn("invalid integer in line 2", nfe);
133 return false;
134 }
135
136 if ((line = in.readLine()) == null) {
137 log.warn(
138 "premature EOF. Expected pattern for km extraction");
139 return false;
140 }
141
142 m = KM_PATTERN.matcher(line);
143
144 if (!m.matches()) {
145 log.warn(
146 "line 4 does not look like a PRF km extraction pattern.");
147 return false;
148 }
149
150 while ((line = in.readLine()) != null) {
151 if (line.length() == 0) {
152 continue;
153 }
154 // TODO: Do data and km extraction
155 }
156 }
157 catch (IOException ioe) {
158 log.error(ioe);
159 return false;
160 }
161 finally {
162 if (in != null) {
163 try {
164 in.close();
165 }
166 catch (IOException ioe) {
167 log.error(ioe);
168 }
169 }
170 }
171
172 return true;
173 }
174
175 public void reset() {
176 data.clear();
177 }
178
179 public static void parsePRFs(File root) {
180
181 PRFParser parser = new PRFParser();
182
183 Stack<File> stack = new Stack<File>();
184 stack.push(root);
185
186 while (!stack.empty()) {
187 File file = stack.pop();
188 if (file.isDirectory()) {
189 File [] files = file.listFiles();
190 if (files != null) {
191 for (File f: files) {
192 stack.push(f);
193 }
194 }
195 }
196 else if (file.isFile()
197 && file.getName().toLowerCase().endsWith(".prf")
198 ) {
199 parser.reset();
200 boolean success = parser.parse(file);
201 log.info("parsing " + (success ? "succeeded" : "failed"));
202 }
203 }
204 }
205
206 public static void main(String [] args) {
207
208 for (String arg: args) {
209 parsePRFs(new File(arg));
210 }
211 }
212 }
213 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org