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