From b990d82f8e25742e53a219a521fe96a56077546c Mon Sep 17 00:00:00 2001 From: "andreas.schildbach" Date: Sat, 16 Oct 2010 22:16:36 +0000 Subject: [PATCH] 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 --- src/de/schildbach/pte/util/ParserUtils.java | 18 +++++- src/de/schildbach/pte/util/XmlPullUtil.java | 61 +++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/de/schildbach/pte/util/ParserUtils.java b/src/de/schildbach/pte/util/ParserUtils.java index 35b324d6..1ffa06e1 100644 --- a/src/de/schildbach/pte/util/ParserUtils.java +++ b/src/de/schildbach/pte/util/ParserUtils.java @@ -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(); } diff --git a/src/de/schildbach/pte/util/XmlPullUtil.java b/src/de/schildbach/pte/util/XmlPullUtil.java index 4b21e8f0..9a879047 100644 --- a/src/de/schildbach/pte/util/XmlPullUtil.java +++ b/src/de/schildbach/pte/util/XmlPullUtil.java @@ -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. */