/*
* Copyright 2010, 2011 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.schildbach.pte;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;
import de.schildbach.pte.dto.Line;
import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.LocationType;
import de.schildbach.pte.dto.NearbyStationsResult;
import de.schildbach.pte.dto.QueryDeparturesResult;
/**
* @author Andreas Schildbach
*/
public class NasaProvider extends AbstractHafasProvider
{
public static final NetworkId NETWORK_ID = NetworkId.NASA;
public static final String OLD_NETWORK_ID = "www.nasa.de";
private static final String API_BASE = "http://reiseauskunft.insa.de/bin/";
public NasaProvider()
{
super(API_BASE + "query.exe/dn", 8, null);
}
public NetworkId id()
{
return NETWORK_ID;
}
public boolean hasCapabilities(final Capability... capabilities)
{
for (final Capability capability : capabilities)
if (capability == Capability.AUTOCOMPLETE_ONE_LINE || capability == Capability.DEPARTURES || capability == Capability.CONNECTIONS)
return true;
return false;
}
@Override
protected void setProductBits(final StringBuilder productBits, final char product)
{
if (product == 'I')
{
productBits.setCharAt(0, '1'); // ICE
productBits.setCharAt(1, '1'); // IC/EC
}
else if (product == 'R')
{
productBits.setCharAt(3, '1'); // RE/RB
productBits.setCharAt(7, '1'); // Tourismus-Züge
productBits.setCharAt(2, '1'); // undokumentiert
}
else if (product == 'S' || product == 'U')
{
productBits.setCharAt(4, '1'); // S/U
}
else if (product == 'T')
{
productBits.setCharAt(5, '1'); // Straßenbahn
}
else if (product == 'B' || product == 'P')
{
productBits.setCharAt(6, '1'); // Bus
}
else if (product == 'F' || product == 'C')
{
}
else
{
throw new IllegalArgumentException("cannot handle: " + product);
}
}
private static final String[] PLACES = { "Leipzig", "Halle (Saale)", "Halle" };
@Override
protected String[] splitPlaceAndName(final String name)
{
for (final String place : PLACES)
{
if (name.startsWith(place + " ") || name.startsWith(place + "-"))
return new String[] { place, name.substring(place.length() + 1) };
else if (name.startsWith(place + ", "))
return new String[] { place, name.substring(place.length() + 2) };
}
return super.splitPlaceAndName(name);
}
public NearbyStationsResult queryNearbyStations(final Location location, final int maxDistance, final int maxStations) throws IOException
{
final StringBuilder uri = new StringBuilder(API_BASE);
if (location.type == LocationType.STATION && location.hasId())
{
uri.append("stboard.exe/dn?near=Anzeigen");
uri.append("&distance=").append(maxDistance != 0 ? maxDistance / 1000 : 50);
uri.append("&input=").append(location.id);
return htmlNearbyStations(uri.toString());
}
else
{
throw new IllegalArgumentException("cannot handle: " + location.toDebugString());
}
}
public QueryDeparturesResult queryDepartures(final int stationId, final int maxDepartures, final boolean equivs) throws IOException
{
final StringBuilder uri = new StringBuilder();
uri.append(API_BASE).append("stboard.exe/dn");
uri.append("?productsFilter=").append(allProductsString());
uri.append("&boardType=dep");
uri.append("&disableEquivs=yes"); // don't use nearby stations
uri.append("&maxJourneys=50"); // ignore maxDepartures because result contains other stations
uri.append("&start=yes");
uri.append("&L=vs_java3");
uri.append("&input=").append(stationId);
return xmlQueryDepartures(uri.toString(), stationId);
}
public List<Location> autocompleteStations(final CharSequence constraint) throws IOException
{
return xmlMLcReq(constraint);
}
private static final Pattern P_LINE_NUMBER = Pattern.compile("\\d{4,}");
@Override
protected Line parseLineWithoutType(final String line)
{
if (P_LINE_NUMBER.matcher(line).matches())
return newLine('?' + line);
return super.parseLineWithoutType(line);
}
@Override
protected Line parseLineAndType(final String line)
{
return parseLineWithoutType(line);
}
@Override
protected char normalizeType(String type)
{
final String ucType = type.toUpperCase();
if ("ECW".equals(ucType))
return 'I';
if ("IXB".equals(ucType)) // ICE International
return 'I';
if ("DPF".equals(ucType)) // mit Dampflok bespannter Zug
return 'R';
if ("DAM".equals(ucType)) // Harzer Schmalspurbahnen: mit Dampflok bespannter Zug
return 'R';
if ("TW".equals(ucType)) // Harzer Schmalspurbahnen: Triebwagen
return 'R';
if ("RR".equals(ucType)) // Polen
return 'R';
if ("BAHN".equals(ucType))
return 'R';
if ("ZUGBAHN".equals(ucType))
return 'R';
if ("DAMPFZUG".equals(ucType))
return 'R';
if ("E".equals(ucType)) // Stadtbahn Karlsruhe: S4/S31/xxxxx
return 'S';
if ("BSV".equals(ucType))
return 'B';
if ("RUFBUS".equals(ucType)) // Rufbus
return 'B';
if ("RBS".equals(ucType)) // Rufbus
return 'B';
final char t = super.normalizeType(type);
if (t != 0)
return t;
return 0;
}
}