extensible contexts

This commit is contained in:
Andreas Schildbach 2012-03-18 18:47:02 +01:00
parent c5368dbe5b
commit d3162518ea
9 changed files with 105 additions and 15 deletions

View file

@ -55,9 +55,11 @@ import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.LocationType;
import de.schildbach.pte.dto.NearbyStationsResult;
import de.schildbach.pte.dto.Point;
import de.schildbach.pte.dto.QueryConnectionsContext;
import de.schildbach.pte.dto.QueryConnectionsResult;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.dto.ResultHeader;
import de.schildbach.pte.dto.SimpleStringContext;
import de.schildbach.pte.dto.StationDepartures;
import de.schildbach.pte.dto.Stop;
import de.schildbach.pte.exception.ParserException;
@ -1666,8 +1668,10 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
}
}
public QueryConnectionsResult queryMoreConnections(final String commandUri, final boolean later) throws IOException
public QueryConnectionsResult queryMoreConnections(final QueryConnectionsContext contextObj, final boolean later) throws IOException
{
final SimpleStringContext context = (SimpleStringContext) contextObj;
final String commandUri = context.context;
final StringBuilder uri = new StringBuilder(commandUri);
uri.append("&command=").append(later ? "tripNext" : "tripPrev");
@ -1698,7 +1702,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
final XmlPullParser pp = parserFactory.newPullParser();
pp.setInput(is, null);
final ResultHeader header = enterItdRequest(pp);
final String context = header.context;
final Object context = header.context;
if (XmlPullUtil.test(pp, "itdLayoutParams"))
XmlPullUtil.next(pp);
@ -2078,7 +2082,8 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
XmlPullUtil.exit(pp, "itdRouteList");
return new QueryConnectionsResult(header, uri, from, via, to, commandLink(context, requestId), connections);
return new QueryConnectionsResult(header, uri, from, via, to, new SimpleStringContext(commandLink((String) context, requestId)),
connections);
}
else
{

View file

@ -51,9 +51,11 @@ import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.LocationType;
import de.schildbach.pte.dto.NearbyStationsResult;
import de.schildbach.pte.dto.Point;
import de.schildbach.pte.dto.QueryConnectionsContext;
import de.schildbach.pte.dto.QueryConnectionsResult;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.dto.ResultHeader;
import de.schildbach.pte.dto.SimpleStringContext;
import de.schildbach.pte.dto.StationDepartures;
import de.schildbach.pte.dto.Stop;
import de.schildbach.pte.util.ParserUtils;
@ -780,11 +782,13 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
return queryConnections(request.toString(), from, via, to);
}
public QueryConnectionsResult queryMoreConnections(final String context, final boolean later) throws IOException
public QueryConnectionsResult queryMoreConnections(final QueryConnectionsContext contextObj, final boolean later) throws IOException
{
final SimpleStringContext context = (SimpleStringContext) contextObj;
final StringBuilder request = new StringBuilder("<ConScrReq scrDir=\"").append(later ? 'F' : 'B').append("\" nrCons=\"")
.append(NUM_CONNECTIONS).append("\">");
request.append("<ConResCtxt>").append(context).append("</ConResCtxt>");
request.append("<ConResCtxt>").append(context.context).append("</ConResCtxt>");
request.append("</ConScrReq>");
return queryConnections(request.toString(), null, null, null);
@ -1127,7 +1131,7 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
XmlPullUtil.exit(pp);
return new QueryConnectionsResult(header, null, from, via, to, context, connections);
return new QueryConnectionsResult(header, null, from, via, to, new SimpleStringContext(context), connections);
}
catch (final XmlPullParserException x)
{

View file

@ -33,9 +33,11 @@ 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.QueryConnectionsContext;
import de.schildbach.pte.dto.QueryConnectionsResult;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.dto.ResultHeader;
import de.schildbach.pte.dto.SimpleStringContext;
import de.schildbach.pte.exception.SessionExpiredException;
import de.schildbach.pte.util.ParserUtils;
@ -294,8 +296,10 @@ public final class BahnProvider extends AbstractHafasProvider
}
@Override
public QueryConnectionsResult queryMoreConnections(final String uri, final boolean later) throws IOException
public QueryConnectionsResult queryMoreConnections(final QueryConnectionsContext contextObj, final boolean later) throws IOException
{
final SimpleStringContext context = (SimpleStringContext) contextObj;
final String uri = context.context;
final CharSequence page = ParserUtils.scrape(uri);
// TODO handle next/prev
return queryConnections(uri, page);
@ -360,7 +364,7 @@ public final class BahnProvider extends AbstractHafasProvider
}
}
return new QueryConnectionsResult(new ResultHeader(SERVER_PRODUCT), uri, from, null, to, linkLater, connections);
return new QueryConnectionsResult(new ResultHeader(SERVER_PRODUCT), uri, from, null, to, new SimpleStringContext(linkLater), connections);
}
else
{

View file

@ -40,9 +40,11 @@ import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.LocationType;
import de.schildbach.pte.dto.NearbyStationsResult;
import de.schildbach.pte.dto.Point;
import de.schildbach.pte.dto.QueryConnectionsContext;
import de.schildbach.pte.dto.QueryConnectionsResult;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.dto.ResultHeader;
import de.schildbach.pte.dto.SimpleStringContext;
import de.schildbach.pte.dto.StationDepartures;
import de.schildbach.pte.dto.Stop;
import de.schildbach.pte.dto.Style;
@ -604,8 +606,10 @@ public final class BvgProvider extends AbstractHafasProvider
}
@Override
public QueryConnectionsResult queryMoreConnections(final String uri, final boolean later) throws IOException
public QueryConnectionsResult queryMoreConnections(final QueryConnectionsContext contextObj, final boolean later) throws IOException
{
final SimpleStringContext context = (SimpleStringContext) contextObj;
final String uri = context.context;
final CharSequence page = ParserUtils.scrape(uri);
// TODO handle next/prev
return queryConnections(uri, page);
@ -858,7 +862,8 @@ public final class BvgProvider extends AbstractHafasProvider
}
}
return new QueryConnectionsResult(new ResultHeader(SERVER_PRODUCT), firstUri, from, via, to, linkLater, connections);
return new QueryConnectionsResult(new ResultHeader(SERVER_PRODUCT), firstUri, from, via, to, new SimpleStringContext(linkLater),
connections);
}
else
{

View file

@ -25,6 +25,7 @@ import de.schildbach.pte.dto.GetConnectionDetailsResult;
import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.NearbyStationsResult;
import de.schildbach.pte.dto.Point;
import de.schildbach.pte.dto.QueryConnectionsContext;
import de.schildbach.pte.dto.QueryConnectionsResult;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.dto.Style;
@ -128,7 +129,7 @@ public interface NetworkProvider
* @return result object that contains possible connections
* @throws IOException
*/
QueryConnectionsResult queryMoreConnections(String context, boolean later) throws IOException;
QueryConnectionsResult queryMoreConnections(QueryConnectionsContext context, boolean later) throws IOException;
/**
* Get details about a connection

View file

@ -0,0 +1,30 @@
/*
* Copyright 2012 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.dto;
import java.io.Serializable;
/**
* @author Andreas Schildbach
*/
public interface QueryConnectionsContext extends Serializable
{
boolean canQueryLater();
boolean canQueryEarlier();
}

View file

@ -41,11 +41,11 @@ public final class QueryConnectionsResult implements Serializable
public final Location from;
public final Location via;
public final Location to;
public final String context;
public final QueryConnectionsContext context;
public final List<Connection> connections;
public QueryConnectionsResult(final ResultHeader header, final String queryUri, final Location from, final Location via, final Location to,
final String context, final List<Connection> connections)
final QueryConnectionsContext context, final List<Connection> connections)
{
this.header = header;
this.status = Status.OK;

View file

@ -27,7 +27,7 @@ public final class ResultHeader implements Serializable
public final String serverProduct;
public final String serverVersion;
public final long serverTime;
public final String context;
public final Object context;
public ResultHeader(final String serverProduct)
{
@ -37,7 +37,7 @@ public final class ResultHeader implements Serializable
this.context = null;
}
public ResultHeader(final String serverProduct, final String serverVersion, final long serverTime, final String context)
public ResultHeader(final String serverProduct, final String serverVersion, final long serverTime, final Object context)
{
this.serverProduct = serverProduct;
this.serverVersion = serverVersion;

View file

@ -0,0 +1,41 @@
/*
* Copyright 2012 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.dto;
/**
* @author Andreas Schildbach
*/
public class SimpleStringContext implements QueryConnectionsContext
{
public final String context;
public SimpleStringContext(final String context)
{
this.context = context;
}
public boolean canQueryLater()
{
return context != null;
}
public boolean canQueryEarlier()
{
return context != null;
}
}