comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/W80Parser.java @ 4728:ccae8b43e527

Initial empty version of W80Parser.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 28 Dec 2012 11:59:01 +0100
parents
children 0df1cac6c4b5
comparison
equal deleted inserted replaced
4727:c6d70560285f 4728:ccae8b43e527
1 package de.intevation.flys.importer.parsers;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.List;
7 import java.util.TreeMap;
8
9 import java.util.regex.Pattern;
10 import java.util.regex.Matcher;
11
12 import java.io.File;
13 import java.io.IOException;
14
15 import org.apache.log4j.Logger;
16
17 import de.intevation.flys.importer.XY;
18
19 import de.intevation.artifacts.common.utils.FileTools;
20
21
22 /**
23 * To create cross-sections, generate: Map<double,list<xy>> from files
24 * in da66 format.
25 */
26 public class W80Parser extends LineParser implements CrossSectionParser
27 {
28 /** Private logger. */
29 private static Logger logger = Logger.getLogger(W80Parser.class);
30
31 /** Regex to match lines of files in da66 format. */
32 private static final Pattern LINE_PATTERN =
33 Pattern.compile("[\\p{Alnum} ]{20}" + // ID
34 "[0-9 ]{10} " + // GK-right
35 "[0-9 ]{10} " + // GK-left
36 "[0-9 ]{6} " + // date
37 "[0-9 ]{1} " + // kind of exactness of measurement
38 "[0-9 ]{7} " + // height
39 "[0-9 ]{6} " + // date of height
40 "[0-9 ]{1} " + // kind of exactness of height-measurement
41 "[0-9 ]{3} " + // kind (only for NIV-points)
42 "[\\p{Alnum} ]{6} " + // date of point decline
43 "[\\p{Alnum} ]{8} " + // note for point
44 "[0-9 ]{2} " // actuality
45 );
46
47 // TODO define headers regex, use regex or remove it.
48
49
50 /** The current line to which add points. */
51 private List<XY> currentLine;
52
53
54 /** Data collected so far, last element will be currentLine. */
55 protected Map<Double, List<XY>> data;
56
57
58 /** Trivial constructor. */
59 public W80Parser() {
60 data = new TreeMap<Double, List<XY>>();
61 }
62
63
64 /** Get the description of the cross section parsed. */
65 @Override
66 public String getDescription() {
67 return removeExtension(getFileName());
68 }
69
70
71 /** Get the year of this cross sections measurement. */
72 @Override
73 public Integer getYear() {
74 return null;
75 }
76
77
78 /**
79 * Return the data parsed.
80 * @return map of stations (km) to list of points.
81 */
82 @Override
83 public Map<Double, List<XY>> getData() {
84 return data;
85 }
86
87
88 /** Remove everything after dot from name. */
89 private static final String removeExtension(String name) {
90 int index = name.lastIndexOf('.');
91 return index == -1
92 ? name
93 : name.substring(0, index);
94 }
95
96 public void parseW80s(File root, final Callback callback) {
97
98 // TODO use the removeExtension/guess description and date.
99 FileTools.walkTree(root, new FileTools.FileVisitor() {
100 @Override
101 public boolean visit(File file) {
102 if (file.isFile() && file.canRead()
103 && file.getName().toLowerCase().endsWith(".w80")
104 && (callback == null || callback.accept(file))) {
105 reset();
106 try {
107 parse(file);
108 logger.info("parsing done");
109 if (callback != null) {
110 callback.parsed(W80Parser.this);
111 }
112 }
113 catch (IOException ioe) {
114 logger.error("IOException while parsing file");
115 return false;
116 }
117 }
118 return true;
119 }
120 });
121 }
122
123
124 /** Called before consuming first line of file. */
125 public void reset() {
126 data.clear();
127 currentLine = new ArrayList<XY>();
128 }
129
130
131 /**
132 * Called for each line. Try to extract info from a da66 line.
133 */
134 @Override
135 protected void handleLine(int lineNum, String line) {
136 String pointId = line.substring(0,20);
137 String gkRight = line.substring(20,30);
138 String gkHigh = line.substring(30,40);
139 String date = line.substring(40,46);
140 String locType = line.substring(46,47);
141 String height = line.substring(47,54);
142 String dateH = line.substring(54,60);
143 String typeH = line.substring(60,61);
144 String kindH = line.substring(61,64);
145 String dateDec = line.substring(64,70);
146 String note = line.substring(70,78);
147 String actual = line.substring(78);
148 }
149
150
151 /** Called when file is fully consumed. */
152 @Override
153 protected void finish() {
154 logger.info("Parsed " + data.size() + " lines");
155 }
156
157
158 /** Parses files given as arguments. */
159 public static void main(String [] args) {
160
161 W80Parser parser = new W80Parser();
162
163 logger.warn("Start parsing files.");
164 for (String arg: args) {
165 parser.parseW80s(new File(arg), null);
166 logger.warn("Parsing a file.");
167 }
168 logger.error("Finished parsing files.");
169 }
170 }
171 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org