comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/LineParser.java @ 2815:3febaed762b8

Added new parser (stub) to read MINFO sediment density files; prepared import process to handle those files. flys-backend/trunk@4232 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 13 Apr 2012 09:57:37 +0000
parents
children f63b39799d2d
comparison
equal deleted inserted replaced
2814:bfd6a7ef0ad5 2815:3febaed762b8
1 package de.intevation.flys.importer.parsers;
2
3 import java.io.File;
4
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.Locale;
8
9 import java.io.IOException;
10 import java.io.LineNumberReader;
11 import java.io.FileInputStream;
12 import java.io.InputStreamReader;
13
14 import org.apache.log4j.Logger;
15
16
17 public abstract class LineParser {
18
19 private static final Logger log = Logger.getLogger(LineParser.class);
20
21 public static final String ENCODING = "ISO-8859-1";
22
23 public static final Locale DEFAULT_LOCALE = Locale.GERMAN;
24
25 public static final String START_META_CHAR = "#";
26 public static final String SEPERATOR_CHAR = ";";
27
28
29 protected abstract void handleLine(String line);
30
31 protected abstract void reset();
32
33 protected abstract void finish();
34
35
36 /**
37 * This method reads each line of <i>file</i>. At the beginning,
38 * <i>reset()</i> is called; afterwars for each line <i>handleLine()</i> is
39 * called; at the end <i>finish</i> is called.
40 *
41 * @param file The file which should be parsed.
42 */
43 public void parse(File file) throws IOException {
44 log.info("Parsing file '" + file + "'");
45
46 reset();
47
48 LineNumberReader in = null;
49 try {
50 in =
51 new LineNumberReader(
52 new InputStreamReader(
53 new FileInputStream(file), ENCODING));
54
55 String line = null;
56 while ((line = in.readLine()) != null) {
57 if ((line = line.trim()).length() == 0) {
58 continue;
59 }
60
61 handleLine(line);
62 }
63 }
64 finally {
65 if (in != null) {
66 in.close();
67 }
68 }
69
70 finish();
71 }
72
73
74 protected static String stripMetaLine(String line) {
75 String tmp = line.substring(1, line.length());
76
77 if (tmp.startsWith(" ")) {
78 return tmp.substring(1, tmp.length());
79 }
80 else {
81 return tmp;
82 }
83 }
84
85
86 public static Date getDateFromYear(int year) {
87 Calendar cal = Calendar.getInstance();
88 cal.set(year, 0, 1);
89
90 return cal.getTime();
91 }
92 }
93 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org