From f5210b4dcf847974214fdf15ecce64ca4136ea38 Mon Sep 17 00:00:00 2001 From: "andreas.schildbach" Date: Sun, 17 Oct 2010 17:12:55 +0000 Subject: [PATCH] fix date rollovers more lines git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@306 0924bc21-9374-b0fa-ee44-9ff1593b38f0 --- .../schildbach/pte/AbstractHafasProvider.java | 105 ++++++++++++------ 1 file changed, 73 insertions(+), 32 deletions(-) diff --git a/src/de/schildbach/pte/AbstractHafasProvider.java b/src/de/schildbach/pte/AbstractHafasProvider.java index 95c2d356..e3c6d900 100644 --- a/src/de/schildbach/pte/AbstractHafasProvider.java +++ b/src/de/schildbach/pte/AbstractHafasProvider.java @@ -24,7 +24,9 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Calendar; import java.util.Date; +import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -247,7 +249,8 @@ public abstract class AbstractHafasProvider implements NetworkProvider XmlPullUtil.require(pp, "Overview"); XmlPullUtil.enter(pp); XmlPullUtil.require(pp, "Date"); - final Date currentDate = DATE_FORMAT.parse(XmlPullUtil.text(pp)); + final Calendar currentDate = new GregorianCalendar(); + currentDate.setTime(DATE_FORMAT.parse(XmlPullUtil.text(pp))); XmlPullUtil.exit(pp); XmlPullUtil.require(pp, "ConSectionList"); XmlPullUtil.enter(pp); @@ -278,7 +281,7 @@ public abstract class AbstractHafasProvider implements NetworkProvider XmlPullUtil.require(pp, "Dep"); XmlPullUtil.enter(pp); XmlPullUtil.require(pp, "Time"); - final Date departureTime = ParserUtils.joinDateTime(currentDate, TIME_FORMAT.parse(XmlPullUtil.text(pp).substring(3, 8))); + final Date departureTime = parseTime(currentDate, XmlPullUtil.text(pp)); XmlPullUtil.require(pp, "Platform"); XmlPullUtil.enter(pp); XmlPullUtil.require(pp, "Text"); @@ -307,27 +310,35 @@ public abstract class AbstractHafasProvider implements NetworkProvider XmlPullUtil.enter(pp); String name = null; String category = null; + String longCategory = null; while (XmlPullUtil.test(pp, "JourneyAttribute")) { XmlPullUtil.enter(pp); XmlPullUtil.require(pp, "Attribute"); final String attrName = XmlPullUtil.attr(pp, "type"); XmlPullUtil.enter(pp); - final String attrValue = parseAttributeVariant(pp, "NORMAL"); + final Map attributeVariants = parseAttributeVariants(pp); XmlPullUtil.exit(pp); XmlPullUtil.exit(pp); if ("NAME".equals(attrName)) - name = attrValue; + { + name = attributeVariants.get("NORMAL"); + } else if ("CATEGORY".equals(attrName)) - category = attrValue; + { + category = attributeVariants.get("NORMAL"); + longCategory = attributeVariants.get("LONG"); + } else if ("DIRECTION".equals(attrName)) - direction = attrValue; + { + direction = attributeVariants.get("NORMAL"); + } } XmlPullUtil.exit(pp); XmlPullUtil.exit(pp); - line = _normalizeLine(category, name); + line = _normalizeLine(category, name, longCategory); } else if (tag.equals("Walk") || tag.equals("Transfer")) { @@ -362,7 +373,7 @@ public abstract class AbstractHafasProvider implements NetworkProvider XmlPullUtil.require(pp, "Arr"); XmlPullUtil.enter(pp); XmlPullUtil.require(pp, "Time"); - final Date arrivalTime = ParserUtils.joinDateTime(currentDate, TIME_FORMAT.parse(XmlPullUtil.text(pp).substring(3, 8))); + final Date arrivalTime = parseTime(currentDate, XmlPullUtil.text(pp)); XmlPullUtil.require(pp, "Platform"); XmlPullUtil.enter(pp); XmlPullUtil.require(pp, "Text"); @@ -432,26 +443,46 @@ public abstract class AbstractHafasProvider implements NetworkProvider } } - private final String parseAttributeVariant(final XmlPullParser pp, final String type) throws XmlPullParserException, IOException + private final Map parseAttributeVariants(final XmlPullParser pp) throws XmlPullParserException, IOException { - String value = null; + final Map attributeVariants = new HashMap(); while (XmlPullUtil.test(pp, "AttributeVariant")) { - if (type.equals(XmlPullUtil.attr(pp, "type"))) - { - XmlPullUtil.enter(pp); - XmlPullUtil.require(pp, "Text"); - value = XmlPullUtil.text(pp).trim(); - XmlPullUtil.exit(pp); - } - else - { - XmlPullUtil.next(pp); - } + final String type = XmlPullUtil.attr(pp, "type"); + XmlPullUtil.enter(pp); + XmlPullUtil.require(pp, "Text"); + final String value = XmlPullUtil.text(pp).trim(); + XmlPullUtil.exit(pp); + + attributeVariants.put(type, value); } - return value; + return attributeVariants; + } + + private static final Pattern P_TIME = Pattern.compile("(\\d+)d(\\d+):(\\d{2}):(\\d{2})"); + + private Date parseTime(final Calendar currentDate, final String str) + { + final Matcher m = P_TIME.matcher(str); + if (m.matches()) + { + final Calendar c = new GregorianCalendar(); + c.set(Calendar.YEAR, currentDate.get(Calendar.YEAR)); + c.set(Calendar.MONTH, currentDate.get(Calendar.MONTH)); + c.set(Calendar.DAY_OF_MONTH, currentDate.get(Calendar.DAY_OF_MONTH)); + c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(m.group(2))); + c.set(Calendar.MINUTE, Integer.parseInt(m.group(3))); + c.set(Calendar.SECOND, Integer.parseInt(m.group(4))); + c.set(Calendar.MILLISECOND, 0); + c.add(Calendar.DAY_OF_MONTH, Integer.parseInt(m.group(1))); + return c.getTime(); + } + else + { + throw new IllegalArgumentException("cannot parse duration: " + str); + } } private static final Pattern P_DURATION = Pattern.compile("(\\d+):(\\d{2})"); @@ -475,39 +506,49 @@ public abstract class AbstractHafasProvider implements NetworkProvider throw new IllegalArgumentException("cannot handle: " + location.toDebugString()); } - private final String _normalizeLine(final String type, final String name) + private final String _normalizeLine(final String type, final String name, final String longCategory) { final String normalizedType = type.split(" ", 2)[0]; final String normalizedName = normalizeWhitespace(name); - if ("THALYS".equals(normalizedType)) + if ("EN".equals(normalizedType)) + return "I" + normalizedName; + if ("ICE".equals(normalizedType)) // InterCityExpress return "I" + normalizedName; if ("IC".equals(normalizedType)) return "I" + normalizedName; - if ("EN".equals(normalizedType)) + if ("CNL".equals(normalizedType)) // CityNightLine return "I" + normalizedName; if ("OEC".equals(normalizedType)) return "I" + normalizedName; + if ("OIC".equals(normalizedType)) + return "I" + normalizedName; + if ("THALYS".equals(normalizedType)) + return "I" + normalizedName; - if ("R".equals(normalizedType)) + if ("R".equals(normalizedType)) // Regio return "R" + normalizedName; if ("RE".equals(normalizedType)) return "R" + normalizedName; if ("IR".equals(normalizedType)) return "R" + normalizedName; - if ("Tramway".equals(normalizedType)) - return "T" + normalizedName; + if ("S".equals(normalizedType)) // S-Bahn + return "S" + normalizedName; - if ("BUS".equals(normalizedType)) - return "B" + normalizedName; + if ("Tramway".equals(normalizedType)) + return "T" + normalizedName; + + if ("BUS".equals(normalizedType)) + return "B" + normalizedName; if ("L".equals(normalizedType)) return "?" + normalizedName; - if ("NZ".equals(normalizedType)) + if ("NZ".equals(normalizedType)) // Nacht-Zug return "?" + normalizedName; - throw new IllegalStateException("cannot normalize type '" + normalizedType + "' (" + type + ") name '" + normalizedName + "'"); + throw new IllegalStateException("cannot normalize type '" + normalizedType + "' (" + type + ") name '" + normalizedName + "' longCategory '" + + longCategory + "'"); } private final static Pattern P_WHITESPACE = Pattern.compile("\\s+");