comparison flys-backend/src/main/java/org/dive4elements/river/importer/parsers/LineParser.java @ 5828:dfb26b03b179

Moved directories to org.dive4elements.river
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:53:11 +0200
parents flys-backend/src/main/java/de/intevation/flys/importer/parsers/LineParser.java@53fcc0d5a03e
children 18619c1e7c2a
comparison
equal deleted inserted replaced
5827:e308d4ecd35a 5828:dfb26b03b179
1 package de.intevation.flys.importer.parsers;
2
3 import java.io.File;
4
5 import java.text.DateFormat;
6 import java.text.NumberFormat;
7 import java.text.ParseException;
8 import java.text.SimpleDateFormat;
9 import java.util.Calendar;
10 import java.util.Date;
11 import java.util.Locale;
12
13 import java.io.IOException;
14 import java.io.LineNumberReader;
15 import java.io.FileInputStream;
16 import java.io.InputStreamReader;
17
18 import org.apache.log4j.Logger;
19
20
21 /**
22 * Base-Class for prasers for line-based file formats.
23 * Calls reset(), then read line by line, calling handleLine() for each,
24 * then calls finish().
25 */
26 public abstract class LineParser {
27
28 /** Private logger. */
29 private static final Logger log = Logger.getLogger(LineParser.class);
30
31 public static final String ENCODING = "ISO-8859-1";
32
33 public static final Locale DEFAULT_LOCALE = Locale.GERMAN;
34
35 public static final String START_META_CHAR = "#";
36 public static final String SEPERATOR_CHAR = ";";
37
38
39 protected abstract void handleLine(int lineNum, String line);
40
41 protected abstract void reset();
42
43 protected abstract void finish();
44
45 /** Name of file parsed. */
46 protected String fileName;
47
48 protected File inputFile;
49
50
51 /**
52 * This method reads each line of <i>file</i>. At the beginning,
53 * <i>reset()</i> is called; afterwards for each line <i>handleLine()</i> is
54 * called; at the end <i>finish</i> is called.
55 *
56 * @param file The file which should be parsed.
57 */
58 public void parse(File file) throws IOException {
59 log.info("Parsing file '" + file + "'");
60
61 inputFile = file;
62
63 fileName = file.getName();
64
65 reset();
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 int lineNum = 1;
76 while ((line = in.readLine()) != null) {
77 if ((line = line.trim()).length() == 0) {
78 lineNum++;
79 continue;
80 }
81
82 handleLine(lineNum++, line);
83 }
84 }
85 finally {
86 if (in != null) {
87 in.close();
88 }
89 }
90
91 finish();
92 }
93
94
95 /** Returns the name of the file parsed. */
96 protected String getFileName() {
97 return fileName;
98 }
99
100 /** Returns the file currently parsed. */
101 protected File getInputFile() {
102 return inputFile;
103 }
104
105
106 protected static String stripMetaLine(String line) {
107 String tmp = line.substring(1, line.length());
108
109 if (tmp.startsWith(" ")) {
110 return tmp.substring(1, tmp.length());
111 }
112 else {
113 return tmp;
114 }
115 }
116
117 public static double getDouble(String doubleString) throws ParseException {
118 NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
119 Number value = nf.parse(doubleString);
120
121 return value.doubleValue();
122 }
123
124 public static Date getDate(String dateString) throws ParseException {
125 DateFormat df = SimpleDateFormat.getDateInstance(
126 SimpleDateFormat.MEDIUM, DEFAULT_LOCALE);
127
128 return df.parse(dateString);
129 }
130
131
132 public static Date getDateFromYear(int year) {
133 Calendar cal = Calendar.getInstance();
134 cal.set(year, 0, 1);
135
136 return cal.getTime();
137 }
138 }
139 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org