Mercurial > dive4elements > river
view flys-backend/src/main/java/de/intevation/flys/importer/parsers/LineParser.java @ 4768:99f9e371371b
Move distance calculation to Coordinate class. Use inheritance instead of composition in Anchor class. Made Anchor class static. Use epsilon equal comparision when checking for same station: Boy, do you ever learn that sharp equal comparison of doubles is not a clever idea!?
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Fri, 04 Jan 2013 15:35:51 +0100 |
parents | b0b1008c07e9 |
children | 53fcc0d5a03e |
line wrap: on
line source
package de.intevation.flys.importer.parsers; import java.io.File; import java.text.DateFormat; import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.io.IOException; import java.io.LineNumberReader; import java.io.FileInputStream; import java.io.InputStreamReader; import org.apache.log4j.Logger; /** * Base-Class for prasers for line-based file formats. * Calls reset(), then read line by line, calling handleLine() for each, * then calls finish(). */ public abstract class LineParser { /** Private logger. */ private static final Logger log = Logger.getLogger(LineParser.class); public static final String ENCODING = "ISO-8859-1"; public static final Locale DEFAULT_LOCALE = Locale.GERMAN; public static final String START_META_CHAR = "#"; public static final String SEPERATOR_CHAR = ";"; protected abstract void handleLine(int lineNum, String line); protected abstract void reset(); protected abstract void finish(); /** Name of file parsed. */ protected String fileName; /** * This method reads each line of <i>file</i>. At the beginning, * <i>reset()</i> is called; afterwards for each line <i>handleLine()</i> is * called; at the end <i>finish</i> is called. * * @param file The file which should be parsed. */ public void parse(File file) throws IOException { log.info("Parsing file '" + file + "'"); fileName = file.getName(); reset(); LineNumberReader in = null; try { in = new LineNumberReader( new InputStreamReader( new FileInputStream(file), ENCODING)); String line = null; int lineNum = 1; while ((line = in.readLine()) != null) { if ((line = line.trim()).length() == 0) { lineNum++; continue; } handleLine(lineNum++, line); } } finally { if (in != null) { in.close(); } } finish(); } /** Returns the name of the file parsed. */ protected String getFileName() { return fileName; } protected static String stripMetaLine(String line) { String tmp = line.substring(1, line.length()); if (tmp.startsWith(" ")) { return tmp.substring(1, tmp.length()); } else { return tmp; } } public static double getDouble(String doubleString) throws ParseException { NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); Number value = nf.parse(doubleString); return value.doubleValue(); } public static Date getDate(String dateString) throws ParseException { DateFormat df = SimpleDateFormat.getDateInstance( SimpleDateFormat.MEDIUM, DEFAULT_LOCALE); return df.parse(dateString); } public static Date getDateFromYear(int year) { Calendar cal = Calendar.getInstance(); cal.set(year, 0, 1); return cal.getTime(); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :