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