comparison flys-backend/src/main/java/de/intevation/flys/importer/AnnotationsParser.java @ 186:cf8cbcb6a10d

Added parser to read *.KM files. flys-backend/trunk@1506 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 17 Mar 2011 17:43:57 +0000
parents
children bc3747a371cc
comparison
equal deleted inserted replaced
185:a60edcfe5f53 186:cf8cbcb6a10d
1 package de.intevation.flys.importer;
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 org.apache.log4j.Logger;
15
16 import de.intevation.flys.utils.FileTools;
17
18 public class AnnotationsParser
19 {
20 private static Logger log = Logger.getLogger(AnnotationsParser.class);
21
22 public static final String ENCODING = "ISO-8859-1";
23
24 public static final String [] TO_SCAN = {
25 "Basisdaten",
26 "Streckendaten"
27 };
28
29 protected HashMap<String, ImportAttribute> attributes;
30 protected HashMap<String, ImportPosition> positions;
31 protected TreeSet<ImportAnnotation> annotations;
32
33 public AnnotationsParser() {
34 attributes = new HashMap<String, ImportAttribute>();
35 positions = new HashMap<String, ImportPosition>();
36 annotations = new TreeSet<ImportAnnotation>();
37 }
38
39 public void parseFile(File file) throws IOException {
40 log.info("parsing km file: '" + file + "'");
41 LineNumberReader in = null;
42 try {
43 in =
44 new LineNumberReader(
45 new InputStreamReader(
46 new FileInputStream(file), ENCODING));
47
48 String line = null;
49 while ((line = in.readLine()) != null) {
50 if ((line = line.trim()).length() == 0
51 || line.startsWith("*")) {
52 continue;
53 }
54
55 String [] parts = line.split("\\s*;\\s*");
56
57 if (parts.length < 3) {
58 log.warn("not enough columns in line "
59 + in.getLineNumber());
60 continue;
61 }
62
63 ImportPosition position = positions.get(parts[0]);
64 if (position == null) {
65 position = new ImportPosition(parts[0]);
66 positions.put(parts[0], position);
67 }
68
69 ImportAttribute attribute = attributes.get(parts[1]);
70 if (attribute == null) {
71 attribute = new ImportAttribute(parts[1]);
72 attributes.put(parts[1], attribute);
73 }
74
75 String [] r = parts[2].replace(",", ".").split("\\s*#\\s*");
76
77 Double from, to;
78
79 try {
80 from = Double.valueOf(r[0]);
81 to = r.length < 2 ? null : Double.valueOf(r[1]);
82 if (to != null && from > to) {
83 Double t = from; from = to; to = t;
84 }
85 }
86 catch (NumberFormatException nfe) {
87 log.warn("invalid number in line " + in.getLineNumber());
88 continue;
89 }
90
91 ImportRange range = new ImportRange(from, to);
92
93 ImportAnnotation annotation = new ImportAnnotation(
94 attribute, position, range);
95
96 if (!annotations.add(annotation)) {
97 log.debug("duplicated annotation in line "
98 + in.getLineNumber());
99 }
100 }
101 }
102 finally {
103 if (in != null) {
104 in.close();
105 }
106 }
107 }
108
109 public void parse(File root) throws IOException {
110
111 for (String toScan: TO_SCAN) {
112 File directory = FileTools.repair(new File(root, toScan));
113 if (!directory.isDirectory()) {
114 log.warn("'" + directory + "' is not a directory.");
115 continue;
116 }
117 File [] files = directory.listFiles();
118 if (files == null) {
119 log.warn("cannot list directory '" + directory + "'");
120 continue;
121 }
122
123 for (File file: files) {
124 if (file.isFile() && file.canRead()
125 && file.getName().toLowerCase().endsWith(".km")) {
126 parseFile(file);
127 }
128 }
129 } // for all directories to scan
130 }
131
132 public List<ImportAnnotation> getAnnotations() {
133 return new ArrayList<ImportAnnotation>(annotations);
134 }
135 }
136 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org