comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/AnnotationsParser.java @ 1211:f08fe480092c

Moved file parsers to separate package. flys-backend/trunk@2337 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 15 Jul 2011 13:07:45 +0000
parents
children c5c48f52dc7b
comparison
equal deleted inserted replaced
1210:31d8638760b1 1211:f08fe480092c
1 package de.intevation.flys.importer.parsers;
2
3 import java.util.HashMap;
4 import java.util.TreeSet;
5 import java.util.List;
6 import java.util.ArrayList;
7
8 import java.io.IOException;
9 import java.io.File;
10 import java.io.LineNumberReader;
11 import java.io.InputStreamReader;
12 import java.io.FileInputStream;
13
14 import java.math.BigDecimal;
15
16 import org.apache.log4j.Logger;
17
18 import de.intevation.flys.utils.FileTools;
19
20 import de.intevation.flys.importer.ImportAnnotation;
21 import de.intevation.flys.importer.ImportRange;
22 import de.intevation.flys.importer.ImportEdge;
23 import de.intevation.flys.importer.ImportAnnotationType;
24 import de.intevation.flys.importer.ImportAttribute;
25 import de.intevation.flys.importer.ImportPosition;
26
27 public class AnnotationsParser
28 {
29 private static Logger log = Logger.getLogger(AnnotationsParser.class);
30
31 public static final String ENCODING = "ISO-8859-1";
32
33 public static final String [] TO_SCAN = {
34 "Basisdaten",
35 "Streckendaten"
36 };
37
38 protected HashMap<String, ImportAttribute> attributes;
39 protected HashMap<String, ImportPosition> positions;
40 protected TreeSet<ImportAnnotation> annotations;
41 protected AnnotationClassifier classifier;
42
43 public AnnotationsParser() {
44 this(null);
45 }
46
47 public AnnotationsParser(AnnotationClassifier classifier) {
48 attributes = new HashMap<String, ImportAttribute>();
49 positions = new HashMap<String, ImportPosition>();
50 annotations = new TreeSet<ImportAnnotation>();
51 this.classifier = classifier;
52 }
53
54 public void parseFile(File file) throws IOException {
55 log.info("parsing km file: '" + file + "'");
56
57 ImportAnnotationType defaultIAT = null;
58
59 if (classifier != null) {
60 defaultIAT = classifier.classifyFile(
61 file.getName(),
62 classifier.getDefaultType());
63 }
64
65 LineNumberReader in = null;
66 try {
67 in =
68 new LineNumberReader(
69 new InputStreamReader(
70 new FileInputStream(file), ENCODING));
71
72 String line = null;
73 while ((line = in.readLine()) != null) {
74 if ((line = line.trim()).length() == 0
75 || line.startsWith("*")) {
76 continue;
77 }
78
79 String [] parts = line.split("\\s*;\\s*");
80
81 if (parts.length < 3) {
82 log.warn("not enough columns in line "
83 + in.getLineNumber());
84 continue;
85 }
86
87 ImportPosition position = positions.get(parts[0]);
88 if (position == null) {
89 position = new ImportPosition(parts[0]);
90 positions.put(parts[0], position);
91 }
92
93 ImportAttribute attribute = attributes.get(parts[1]);
94 if (attribute == null) {
95 attribute = new ImportAttribute(parts[1]);
96 attributes.put(parts[1], attribute);
97 }
98
99 String [] r = parts[2].replace(",", ".").split("\\s*#\\s*");
100
101 BigDecimal from, to;
102
103 try {
104 from = new BigDecimal(r[0]);
105 to = r.length < 2 ? null : new BigDecimal(r[1]);
106 if (to != null && from.compareTo(to) > 0) {
107 BigDecimal t = from; from = to; to = t;
108 }
109 }
110 catch (NumberFormatException nfe) {
111 log.warn("invalid number in line " + in.getLineNumber());
112 continue;
113 }
114
115 ImportEdge edge = null;
116
117 if (parts.length == 4) { // Only 'Unterkante'
118 try {
119 edge = new ImportEdge(
120 null,
121 new BigDecimal(parts[3].trim().replace(',', '.')));
122 }
123 catch (NumberFormatException nfe) {
124 log.warn("cannot parse 'Unterkante' in line " +
125 in.getLineNumber());
126 }
127 }
128 else if (parts.length > 4) { // 'Unterkante' and 'Oberkante'
129 String bottom = parts[3].trim().replace(',', '.');
130 String top = parts[4].trim().replace(',', '.');
131 try {
132 BigDecimal b = bottom.length() == 0
133 ? null
134 : new BigDecimal(bottom);
135 BigDecimal t = top.length() == 0
136 ? null
137 : new BigDecimal(top);
138 edge = new ImportEdge(t, b);
139 }
140 catch (NumberFormatException nfe) {
141 log.warn(
142 "cannot parse 'Unterkante' or 'Oberkante' in line "
143 + in.getLineNumber());
144 }
145 }
146
147 ImportRange range = new ImportRange(from, to);
148
149 ImportAnnotationType type = classifier != null
150 ? classifier.classifyDescription(line, defaultIAT)
151 : null;
152
153 ImportAnnotation annotation = new ImportAnnotation(
154 attribute, position, range, edge, type);
155
156 if (!annotations.add(annotation)) {
157 log.warn("duplicated annotation '" + parts[0] +
158 "' in line " + in.getLineNumber());
159 }
160 }
161 }
162 finally {
163 if (in != null) {
164 in.close();
165 }
166 }
167 }
168
169 public void parse(File root) throws IOException {
170
171 for (String toScan: TO_SCAN) {
172 File directory = FileTools.repair(new File(root, toScan));
173 if (!directory.isDirectory()) {
174 log.warn("'" + directory + "' is not a directory.");
175 continue;
176 }
177 File [] files = directory.listFiles();
178 if (files == null) {
179 log.warn("cannot list directory '" + directory + "'");
180 continue;
181 }
182
183 for (File file: files) {
184 if (file.isFile() && file.canRead()
185 && file.getName().toLowerCase().endsWith(".km")) {
186 parseFile(file);
187 }
188 }
189 } // for all directories to scan
190 }
191
192 public List<ImportAnnotation> getAnnotations() {
193 return new ArrayList<ImportAnnotation>(annotations);
194 }
195 }
196 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org