Jerusalem

This commit is contained in:
Andreas Schildbach 2012-06-20 16:06:39 +02:00
parent f7f3c5080d
commit 9ac2970908
3 changed files with 286 additions and 0 deletions

View file

@ -0,0 +1,192 @@
/*
* Copyright 2013 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.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
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.Product;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.dto.QueryTripsContext;
import de.schildbach.pte.dto.QueryTripsResult;
/**
* Jesuralem? JET = Jerusalem Eternal Tours?
*
* @author Andreas Schildbach
*/
public class JetProvider extends AbstractHafasProvider
{
public static final NetworkId NETWORK_ID = NetworkId.JET;
private static final String API_BASE = "http://planner.jet.org.il/bin/";
public JetProvider()
{
super(API_BASE + "stboard.bin/yn", API_BASE + "ajax-getstop.bin/yn", API_BASE + "query.bin/yn", 4, UTF_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.TRIPS)
return true;
return false;
}
@Override
protected char intToProduct(final int value)
{
if (value == 4)
return 'T';
if (value == 8)
return 'B';
throw new IllegalArgumentException("cannot handle: " + value);
}
@Override
protected void setProductBits(final StringBuilder productBits, final Product product)
{
if (product == Product.HIGH_SPEED_TRAIN)
{
}
else if (product == Product.REGIONAL_TRAIN)
{
}
else if (product == Product.SUBURBAN_TRAIN)
{
}
else if (product == Product.SUBWAY)
{
}
else if (product == Product.TRAM)
{
productBits.setCharAt(2, '1'); // Stadtbahn
}
else if (product == Product.BUS)
{
productBits.setCharAt(3, '1'); // Bus
}
else if (product == Product.ON_DEMAND)
{
}
else if (product == Product.FERRY)
{
}
else if (product == Product.CABLECAR)
{
}
else
{
throw new IllegalArgumentException("cannot handle: " + product);
}
}
public NearbyStationsResult queryNearbyStations(final Location location, final int maxDistance, final int maxStations) throws IOException
{
if (location.hasLocation())
{
final StringBuilder uri = new StringBuilder(queryEndpoint);
uri.append(jsonNearbyStationsParameters(location, maxDistance, maxStations));
return jsonNearbyStations(uri.toString());
}
else if (location.type == LocationType.STATION && location.hasId())
{
final StringBuilder uri = new StringBuilder(stationBoardEndpoint);
uri.append("?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(stationBoardEndpoint);
uri.append(xmlQueryDeparturesParameters(stationId));
return xmlQueryDepartures(uri.toString(), stationId);
}
public List<Location> autocompleteStations(final CharSequence constraint) throws IOException
{
final StringBuilder uri = new StringBuilder(getStopEndpoint);
uri.append(jsonGetStopsParameters(constraint));
return jsonGetStops(uri.toString());
}
@Override
protected void appendCustomTripsQueryBinaryUri(final StringBuilder uri)
{
uri.append("&h2g-direct=11");
}
@Override
public QueryTripsResult queryTrips(final Location from, final Location via, final Location to, final Date date, final boolean dep,
final int numTrips, final Collection<Product> products, final WalkSpeed walkSpeed, final Accessibility accessibility,
final Set<Option> options) throws IOException
{
return queryTripsBinary(from, via, to, date, dep, numTrips, products, walkSpeed, accessibility, options);
}
@Override
public QueryTripsResult queryMoreTrips(final QueryTripsContext contextObj, final boolean later, final int numTrips) throws IOException
{
return queryMoreTripsBinary(contextObj, later, numTrips);
}
private static final Pattern P_NORMALIZE_BUS = Pattern.compile("([א]?\\d{1,3})#");
@Override
protected Line parseLineAndType(final String lineAndType)
{
if ("רק1#".equals(lineAndType))
return newLine('T', "רק1", null);
if ("א 11#".equals(lineAndType))
return newLine('B', "א11", null);
final Matcher mBus = P_NORMALIZE_BUS.matcher(lineAndType);
if (mBus.matches())
return newLine('B', mBus.group(1), null);
throw new IllegalStateException("cannot normalize line#type '" + lineAndType + "'");
}
}

View file

@ -70,6 +70,9 @@ public enum NetworkId
// United Arab Emirates
DUB,
// Israel
JET,
// United States
SF, SEPTA,

View file

@ -0,0 +1,91 @@
/*
* Copyright 2013 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.live;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import de.schildbach.pte.JetProvider;
import de.schildbach.pte.NetworkProvider.Accessibility;
import de.schildbach.pte.NetworkProvider.WalkSpeed;
import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.LocationType;
import de.schildbach.pte.dto.NearbyStationsResult;
import de.schildbach.pte.dto.Product;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.dto.QueryTripsResult;
/**
* @author Andreas Schildbach
*/
public class JetProviderLiveTest extends AbstractProviderLiveTest
{
public JetProviderLiveTest()
{
super(new JetProvider());
}
@Test
public void nearbyStations() throws Exception
{
final NearbyStationsResult result = provider.queryNearbyStations(new Location(LocationType.STATION, 1055), 0, 0);
System.out.println(result.status + " " + result.stations.size() + " " + result.stations);
}
@Test
public void nearbyStationsByCoordinate() throws Exception
{
final NearbyStationsResult result = provider.queryNearbyStations(new Location(LocationType.ADDRESS, 31769757, 35213506), 0, 0);
print(result);
}
@Test
public void queryDepartures() throws Exception
{
final QueryDeparturesResult result1 = provider.queryDepartures(568, 0, false);
print(result1);
final QueryDeparturesResult result2 = provider.queryDepartures(1055, 0, false);
print(result2);
final QueryDeparturesResult result3 = provider.queryDepartures(90010, 0, false);
print(result3);
}
@Test
public void autoComplete() throws Exception
{
final List<Location> autocompletes = provider.autocompleteStations("הנשיא - מוזיאון האיסלם, ירושלים");
print(autocompletes);
}
@Test
public void shortConnection() throws Exception
{
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, 1055, null, null), null, new Location(
LocationType.STATION, 90010, null, null), new Date(), true, Product.ALL, WalkSpeed.NORMAL, Accessibility.NEUTRAL);
System.out.println(result);
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
System.out.println(laterResult);
}
}