autocomplete using XML-interface for Switzerland and Belgium (forgot)

git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@301 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
andreas.schildbach 2010-10-16 22:16:36 +00:00
parent 06712bf0eb
commit b990d82f8e
2 changed files with 78 additions and 1 deletions

View file

@ -164,17 +164,33 @@ public final class ParserUtils
}
public static final InputStream scrapeInputStream(final String url) throws IOException
{
return scrapeInputStream(url, null);
}
public static final InputStream scrapeInputStream(final String url, final String postRequest) throws IOException
{
final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setDoOutput(postRequest != null);
connection.setConnectTimeout(SCRAPE_CONNECT_TIMEOUT);
connection.setReadTimeout(SCRAPE_READ_TIMEOUT);
connection.addRequestProperty("User-Agent", SCRAPE_USER_AGENT);
// workaround to disable Vodafone compression
connection.addRequestProperty("Cache-Control", "no-cache");
if (postRequest != null)
{
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.addRequestProperty("Content-Length", Integer.toString(postRequest.length()));
final Writer writer = new OutputStreamWriter(connection.getOutputStream(), SCRAPE_DEFAULT_ENCODING);
writer.write(postRequest);
writer.close();
}
return connection.getInputStream();
}

View file

@ -21,6 +21,67 @@ public final class XmlPullUtil
{
public static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
/**
* directly jumps forward to start tag, ignoring any structure
*/
public static void jump(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
{
if (!jumpToStartTag(pp, null, tagName))
throw new IllegalStateException("cannot find <" + tagName + " />");
}
/**
* enters current tag
* @throws IOException
*/
public static void enter(final XmlPullParser pp) throws XmlPullParserException, IOException
{
if (pp.getEventType() != XmlPullParser.START_TAG)
throw new IllegalStateException("expecting start tag to enter");
if (pp.isEmptyElementTag())
throw new IllegalStateException("cannot enter empty tag");
pp.next();
}
public static void exit(final XmlPullParser pp) throws XmlPullParserException, IOException
{
if (pp.getEventType() != XmlPullParser.END_TAG)
throw new IllegalStateException("expecting end tag to exit");
pp.next();
}
public static boolean test(final XmlPullParser pp, final String tagName) throws XmlPullParserException
{
return pp.getEventType() == XmlPullParser.START_TAG && pp.getName().equals(tagName);
}
public static void skipTree(final XmlPullParser pp) throws XmlPullParserException, IOException
{
skipSubTree(pp);
pp.next();
}
public static void require(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
{
pp.require(XmlPullParser.START_TAG, null, tagName);
}
public static String attr(final XmlPullParser pp, final String attrName)
{
return pp.getAttributeValue(null, attrName);
}
public static int intAttr(final XmlPullParser pp, final String attrName)
{
return Integer.parseInt(pp.getAttributeValue(null, attrName));
}
public static void requireAttr(final XmlPullParser pp, final String attrName, final String requiredValue)
{
if (!requiredValue.equals(attr(pp, attrName)))
throw new IllegalStateException("cannot find " + attrName + "=\"" + requiredValue + "\" />");
}
/**
* Return value of attribute with given name and no namespace.
*/