@SuppressWarnings("unchecked") private void xmlScan() { // the images can be scaled to 400px width by: // mkdir results // for f in *jpg; do echo "file $f"; convert "$f[400x>]" results/$f; done // parse the XML data for challenges // Get the Android-specific compiled XML parser. ArrayList<Challenge> challenges = null; int nOptions = 3; try { XmlResourceParser xrp = AbcSjovActivity.getAppContext().getResources().getXml(R.xml.q1); while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) { if (xrp.getEventType() == XmlResourceParser.START_TAG) { String s = xrp.getName(); if (s.equals("level")) { challenges = new ArrayList<Challenge>(); String optionStr = xrp.getAttributeValue(null, "options"); nOptions = Integer.parseInt(optionStr); } else if (s.equals("question") && challenges != null) { Challenge ch = new Challenge(); ch.resid = xrp.getAttributeResourceValue(null, "id", 0); ch.answer = xrp.getAttributeValue(null, "answer"); ch.avoid = xrp.getAttributeValue(null, "avoid"); ch.options = xrp.getAttributeValue(null, "options"); ch.nOptions = nOptions; challenges.add(ch); } } else if (xrp.getEventType() == XmlResourceParser.END_TAG) { String s = xrp.getName(); if (s.equals("level")) { challengeMap.add(challenges); } } else if (xrp.getEventType() == XmlResourceParser.TEXT) { // pass } xrp.next(); } xrp.close(); mLastChallengeSet = (ArrayList<Challenge>) challenges.clone(); } catch (XmlPullParserException xppe) { Log.e(TAG, "Failure of .getEventType or .next, probably bad file format"); xppe.toString(); } catch (IOException ioe) { Log.e(TAG, "Unable to read resource file"); ioe.printStackTrace(); } catch (NullPointerException e) { Log.e(TAG, "NullPointerException in scanXml"); e.printStackTrace(); } }
static { try { PARSER_FACTORY = XmlPullParserFactory.newInstance(); PARSER_FACTORY.setNamespaceAware(true); PARSER_FACTORY.setValidating(false); } catch (XmlPullParserException e) { log.severe(e.toString()); } }
private static XmlPullParser getNewParser() { if (parser == null) { try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); parser = factory.newPullParser(); } catch (XmlPullParserException xe) { System.out.println(xe.toString()); } } return parser; }
/** * Called by the Android system in response to a request to run the sync adapter. The work * required to read data from the network, parse it, and store it in the content provider is done * here. Extending AbstractThreadedSyncAdapter ensures that all methods within SyncAdapter run on * a background thread. For this reason, blocking I/O and other long-running tasks can be run * <em>in situ</em>, and you don't have to set up a separate thread for them. . * * <p>This is where we actually perform any work required to perform a sync. {@link * AbstractThreadedSyncAdapter} guarantees that this will be called on a non-UI thread, so it is * safe to peform blocking I/O here. * * <p>The syncResult argument allows you to pass information back to the method that triggered the * sync. */ @Override public void onPerformSync( Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { Log.i(TAG, "Beginning network synchronization"); try { final URL location = new URL(POPULAR_MOVIE_URL); InputStream stream = null; try { Log.i(TAG, "Streaming data from network: " + location); stream = downloadUrl(location); updateMovieDetails(stream, syncResult); // Makes sure that the InputStream is closed after the app is // finished using it. } catch (JsonSyntaxException jse) { jse.printStackTrace(); } finally { if (stream != null) { stream.close(); } } } catch (MalformedURLException e) { Log.wtf(TAG, "Feed URL is malformed", e); syncResult.stats.numParseExceptions++; return; } catch (IOException e) { Log.e(TAG, "Error reading from network: " + e.toString()); syncResult.stats.numIoExceptions++; return; } catch (XmlPullParserException e) { Log.e(TAG, "Error parsing feed: " + e.toString()); syncResult.stats.numParseExceptions++; return; } catch (ParseException e) { Log.e(TAG, "Error parsing feed: " + e.toString()); syncResult.stats.numParseExceptions++; return; } catch (RemoteException e) { Log.e(TAG, "Error updating database: " + e.toString()); syncResult.databaseError = true; return; } catch (OperationApplicationException e) { Log.e(TAG, "Error updating database: " + e.toString()); syncResult.databaseError = true; return; } Log.i(TAG, "Network synchronization complete"); }
public List<FleetEvent> parse(InputStream stream, OgameResources resources) { List<FleetEvent> eventList = new LinkedList<>(); try { XmlPullParserFactory xppfactory = XmlPullParserFactory.newInstance(); XmlPullParser xpp = xppfactory.newPullParser(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); xpp.setInput(reader); xpp.defineEntityReplacementText("ndash", "-"); xpp.defineEntityReplacementText("nbsp", " "); FleetEvent lastScannedEvent = null; // To make the next for loop look easier to read and understand, we get to the first instance // of // <tr class="eventFleet"> int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { String tagName = xpp.getName(); if (tagName != null && tagName.equals("tr") && hasAttrValue(xpp, "class", "eventFleet")) { lastScannedEvent = new FleetEvent(); int attrCount = xpp.getAttributeCount(); for (int index = 0; index < attrCount; index++) { String attrName = xpp.getAttributeName(index); if (attrName.equals("data-mission-type")) { String value = xpp.getAttributeValue(index); lastScannedEvent.data_mission_type = Integer.valueOf(value); } else if (attrName.equals("data-return-flight")) { String value = xpp.getAttributeValue(index); lastScannedEvent.data_return_flight = Boolean.valueOf(value); } else if (attrName.equals("data-arrival-time")) { String value = xpp.getAttributeValue(index); lastScannedEvent.data_arrival_time = Long.valueOf(value); } } // We must call next() here before breaking. Otherwise, the next loop // will add the incomplete FleetEvent object since it will detect // the same <tr...> element and think it's a new event when it's // still the same first event. xpp.next(); break; } try { eventType = xpp.next(); } catch (XmlPullParserException e) { /* For some strange reason, the emulator can reach this catch block with * e set to null. (Why and how?) Might be a debugger bug */ System.out.println("Analysis of an error: " + e + '\n' + e.getMessage()); e.printStackTrace(); } catch (ArrayIndexOutOfBoundsException e) { // This exception occurs near the end of the document, but it is not something that // should stop the app over. System.err.println( "Possibly reached end of document (HTML is painful): " + e + '\n' + e.getMessage()); e.printStackTrace(); eventType = XmlPullParser.END_DOCUMENT; } } // No events scanned. Just return. if (lastScannedEvent == null) return eventList; eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { // Begin parsing for fleet events. if (eventType == XmlPullParser.START_TAG) { String tagName = xpp.getName(); tagName = (tagName == null) ? "" : tagName; if (tagName.equals("tr") && hasAttrValue(xpp, "class", "eventFleet")) { eventList.add(lastScannedEvent); lastScannedEvent = new FleetEvent(); int attrCount = xpp.getAttributeCount(); for (int index = 0; index < attrCount; index++) { String attrName = xpp.getAttributeName(index); if (attrName.equals("data-mission-type")) { String value = xpp.getAttributeValue(index); lastScannedEvent.data_mission_type = Integer.valueOf(value); } else if (attrName.equals("data-return-flight")) { String value = xpp.getAttributeValue(index); lastScannedEvent.data_return_flight = Boolean.valueOf(value); } else if (attrName.equals("data-arrival-time")) { String value = xpp.getAttributeValue(index); lastScannedEvent.data_arrival_time = Long.valueOf(value); } } } else if (tagName.equals("td")) { if (hasAttrValue(xpp, "class", "originFleet")) { /* Example from the extracted response sample: * <td class="originFleet"> <--XPP pointer is here currently <span class="tooltip" title="A Whole New World"> <figure class="planetIcon planet"></figure> A Whole New World </span> </td> */ tagName = xpp.getName(); int htmlevent = 0; int counter = 0; // From the response extract, we need 5 next()'s to get to the text we need. // Set a hard limit just in case. while ((htmlevent != XmlPullParser.END_TAG || !tagName.equalsIgnoreCase("figure")) && counter < 5) { htmlevent = xpp.next(); tagName = xpp.getName(); counter++; } xpp.next(); if (xpp.getEventType() == XmlPullParser.TEXT) { lastScannedEvent.originFleet = xpp.getText(); if (lastScannedEvent.originFleet != null) lastScannedEvent.originFleet = lastScannedEvent.originFleet.trim(); } } else if (hasAttrValue(xpp, "class", "coordsOrigin")) { /* Example: * <td class="coordsOrigin"> <-- XPP pointer here <a href="http://s125-en.ogame.gameforge.com/game/index.php?page=galaxy&galaxy=1&system=373" target="_top"> [1:373:8] </a> </td> */ tagName = xpp.getName(); // We need 2 next()'s to get to the <a> element. Use a hard limit just in case. int htmlevent = 0; int counter = 0; while ((htmlevent != XmlPullParser.START_TAG || !tagName.equalsIgnoreCase("a")) && counter < 2) { htmlevent = xpp.next(); tagName = xpp.getName(); counter++; } xpp.next(); if (xpp.getEventType() == XmlPullParser.TEXT) { lastScannedEvent.coordsOrigin = new LinkHTML(); String coordinates = xpp.getText(); if (coordinates != null) { coordinates = coordinates.trim(); } else { coordinates = ""; } lastScannedEvent.coordsOrigin.text = coordinates; } } else if (hasAttrValue(xpp, "class", "icon_movement") || hasAttrValue(xpp, "class", "icon_movement_reserve")) { // Have to parse another HTML snippet. This HTML is both encoded to not confuse // the parser, so it must be decoded first. Then it must be put through another // XmlPullParser to gather the data. /* Example: * <td class="icon_movement"> <-- xpp point here * <span class="blah blah blah" * title="bunch of escaped HTML we have to unescape" * data-federation-user-id=""> * * </span> * </td> */ tagName = xpp.getName(); int htmlevent = 0; tagName = (tagName == null) ? "" : tagName; while (htmlevent != XmlPullParser.START_TAG || !tagName.equalsIgnoreCase("span")) { htmlevent = xpp.next(); tagName = xpp.getName(); } Map<String, Long> fleetData = null; if (xpp.getEventType() == XmlPullParser.START_TAG) { int attrSize = xpp.getAttributeCount(); String titleValue = null; for (int index = 0; index < attrSize; index++) { String attrName = xpp.getAttributeName(index); if (attrName.equals("title")) { titleValue = xpp.getAttributeValue(index); } } if (titleValue != null) { parseFleetResComposition( titleValue, lastScannedEvent.fleet, lastScannedEvent.resources); } } } else if (hasAttrValue(xpp, "class", "destFleet")) { /* Example: * <td class="destFleet"> <-- XPP pointer here <span class="tooltip" title="Slot 8 unavailable"> <figure class="planetIcon planet"></figure> Slot 8 unavailable </span> </td> */ int counter = 0; int htmlevent = 0; tagName = xpp.getName(); tagName = (tagName == null) ? "" : tagName; while ((htmlevent != XmlPullParser.END_TAG || !tagName.equalsIgnoreCase("figure")) && counter < 5) { htmlevent = xpp.next(); tagName = xpp.getName(); counter++; } xpp.next(); if (xpp.getEventType() == XmlPullParser.TEXT) { lastScannedEvent.destFleet = xpp.getText(); if (lastScannedEvent.destFleet != null) lastScannedEvent.destFleet = lastScannedEvent.destFleet.trim(); } } else if (hasAttrValue(xpp, "class", "destCoords")) { /* Example: * <td class="destCoords"> <--XPP pointer here <a href="http://s125-en.ogame.gameforge.com/game/index.php?page=galaxy&galaxy=1&system=204" target="_top"> [1:204:8] </a> </td> */ int counter = 0; int htmlevent = 0; tagName = xpp.getName(); tagName = (tagName == null) ? "" : tagName; while ((htmlevent != XmlPullParser.START_TAG || !tagName.equalsIgnoreCase("a")) && counter < 2) { htmlevent = xpp.next(); tagName = xpp.getName(); counter++; } xpp.next(); if (xpp.getEventType() == XmlPullParser.TEXT) { lastScannedEvent.destCoords = new LinkHTML(); String coordinates = xpp.getText(); if (coordinates != null) { coordinates = coordinates.trim(); } else { coordinates = ""; } lastScannedEvent.destCoords.text = coordinates; } } } } try { eventType = xpp.next(); } catch (XmlPullParserException e) { // System.out.println("Analysis of an error: " + e + '\n' + e.getMessage()); // e.printStackTrace(); eventType = XmlPullParser.END_DOCUMENT; } catch (ArrayIndexOutOfBoundsException e) { // This exception occurs near the end of the document, but it is not something that // should stop the app over. System.err.println( "Possibly reached end of document (HTML is painful): " + e + '\n' + e.getMessage()); e.printStackTrace(); eventType = XmlPullParser.END_DOCUMENT; } } eventList.add(lastScannedEvent); } catch (XmlPullParserException e) { System.err.println(e.toString() + '\n' + e.getMessage()); e.printStackTrace(); return null; } catch (IOException e) { System.err.println(e.toString() + '\n' + e.getMessage()); e.printStackTrace(); } return eventList; }
/** * Unescapes the HTML-escaped data in the parameter string htmlEncodedData. The string is then * parsed with an XmlPullParser to extract fleet and resource data. The data is inserted into a * Map<String, Long> object. This Map is then returned. * * @param htmlEncodedData - HTML-escaped string containing the details of the fleet breakdown and * composition * @return a Map<String, Long> object containing fleet and resource composition. Keys are listed * in class FleetAndResources */ private void parseFleetResComposition( String htmlEncodedData, Map<String, Long> fleet, FleetResources res) { StringReader strReader = null; XmlPullParser subxpp = null; try { strReader = new StringReader(htmlEncodedData); subxpp = XmlPullParserFactory.newInstance().newPullParser(); subxpp.setInput(strReader); subxpp.defineEntityReplacementText("nbsp", " "); boolean parsingShips = false; boolean parsingRes = false; String currentShip; String currentRes; int eventType = subxpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (subxpp.getEventType() == XmlPullParser.TEXT) { String textData = subxpp.getText(); textData = textData.replaceAll(":", ""); if (textData.equals("Ships")) { parsingShips = true; break; } } try { subxpp.next(); eventType = subxpp.getEventType(); } catch (XmlPullParserException e) { // System.out.println("Caught an exception. Not stopping: " + e + '\n' + // e.getMessage()); // e.printStackTrace(); } } while (parsingShips && eventType != XmlPullParser.END_DOCUMENT) { subxpp.next(); eventType = subxpp.getEventType(); if (eventType == XmlPullParser.TEXT) { String textData = subxpp.getText(); if (textData != null) { textData = textData.trim(); } if (textData != null && textData.length() > 0) { if (textData.equals("Shipment:")) { parsingRes = true; break; } else { textData = textData.substring(0, textData.length() - 1); currentShip = FleetAndResources.getName(textData); } textData = ""; while (textData.length() == 0) { subxpp.next(); eventType = subxpp.getEventType(); if (eventType == XmlPullParser.TEXT) { textData = subxpp.getText(); textData = textData.trim(); } } String numshipstr = textData; numshipstr = numshipstr.replaceAll("\\.", ""); if (currentShip != null && currentShip.length() > 0) { Long numships = Long.valueOf(numshipstr); fleet.put(currentShip, numships); } } } } eventType = subxpp.getEventType(); while (parsingRes && eventType != XmlPullParser.END_DOCUMENT) { subxpp.next(); eventType = subxpp.getEventType(); if (eventType == XmlPullParser.TEXT) { String textData = subxpp.getText(); if (textData != null) { textData = textData.trim(); } if (textData != null && textData.length() > 0) { String resType = subxpp.getText(); if (FleetAndResources.METAL_TAG.equals(resType)) { currentRes = FleetAndResources.METAL; } else if (FleetAndResources.CRYSTAL_TAG.equals(resType)) { currentRes = FleetAndResources.CRYSTAL; } else if (FleetAndResources.DEUT_TAG.equals(resType)) { currentRes = FleetAndResources.DEUT; } else { continue; } textData = ""; while (textData.length() == 0) { subxpp.next(); eventType = subxpp.getEventType(); if (eventType == XmlPullParser.TEXT) { textData = subxpp.getText(); textData = textData.trim(); } } String amount = textData; amount = amount.replaceAll("\\.", ""); if (amount.length() > 0) { long amt = Long.valueOf(amount); if (currentRes == FleetAndResources.METAL) { res.metal = amt; } else if (currentRes == FleetAndResources.CRYSTAL) { res.crystal = amt; } else if (currentRes == FleetAndResources.DEUT) { res.deuterium = amt; } } } } } } catch (XmlPullParserException | IOException e) { System.err.println(e.toString() + '\n' + e.getMessage()); e.printStackTrace(); } finally { if (subxpp != null) { try { subxpp.setInput(null); } catch (XmlPullParserException e) { System.err.println(e.toString() + '\n' + e.getMessage()); e.printStackTrace(); } } if (strReader != null) strReader.close(); } }
@Override void retrieveSupplementalInfo() throws IOException { CharSequence contents = HttpHelper.downloadViaHttp( "https://bsplus.srowen.com/ss?c=" + country + "&t=" + type + "&i=" + productID, HttpHelper.ContentType.XML); String detailPageURL = null; Collection<String> authors = new ArrayList<String>(); String title = null; String formattedNewPrice = null; String formattedUsedPrice = null; boolean error = false; try { XmlPullParser xpp = buildParser(contents); boolean seenItem = false; boolean seenLowestNewPrice = false; boolean seenLowestUsedPrice = false; for (int eventType = xpp.getEventType(); eventType != XmlPullParser.END_DOCUMENT; eventType = xpp.next()) { if (eventType == XmlPullParser.START_TAG) { String name = xpp.getName(); if ("Item".equals(name)) { if (seenItem) { break; } else { seenItem = true; } } else if ("DetailPageURL".equals(name)) { assertTextNext(xpp); detailPageURL = xpp.getText(); } else if ("Author".equals(name)) { assertTextNext(xpp); authors.add(xpp.getText()); } else if ("Title".equals(name)) { assertTextNext(xpp); title = xpp.getText(); } else if ("LowestNewPrice".equals(name)) { seenLowestNewPrice = true; seenLowestUsedPrice = false; } else if ("LowestUsedPrice".equals(name)) { seenLowestNewPrice = false; seenLowestUsedPrice = true; } else if ("FormattedPrice".equals(name)) { if (seenLowestNewPrice || seenLowestUsedPrice) { assertTextNext(xpp); String theText = xpp.getText(); if (seenLowestNewPrice) { formattedNewPrice = theText; } else { formattedUsedPrice = theText; } seenLowestNewPrice = false; seenLowestUsedPrice = false; } } else if ("Errors".equals(name)) { error = true; break; } } } } catch (XmlPullParserException xppe) { throw new IOException(xppe.toString()); } if (error || detailPageURL == null) { return; } Collection<String> newTexts = new ArrayList<String>(); maybeAddText(title, newTexts); maybeAddTextSeries(authors, newTexts); if (formattedNewPrice != null) { maybeAddText(formattedNewPrice, newTexts); } else if (formattedUsedPrice != null) { maybeAddText(formattedUsedPrice, newTexts); } append(productID, "Amazon", newTexts.toArray(new String[newTexts.size()]), detailPageURL); }
public double getDistance(GeoPoint point1, GeoPoint point2) { double distance = -1; InputStream is = null; Uri.Builder uribuilder = new Uri.Builder(); uribuilder.encodedPath(API_URL); uribuilder.appendQueryParameter( "ll1", (point1.getLatitudeE6() / 1E6) + "," + (point1.getLongitudeE6() / 1E6)); uribuilder.appendQueryParameter( "ll2", (point2.getLatitudeE6() / 1E6) + "," + (point2.getLongitudeE6() / 1E6)); String uri = uribuilder.toString(); try { HttpGet httpGet = new HttpGet(); HttpResponse response = null; httpGet.setURI(new URI(uri)); response = mHttpClient.execute(httpGet); if (response == null) { Log.w(TAG, "getDistance:Response is null"); return -1; } int status = response.getStatusLine().getStatusCode(); if (status != HttpStatus.SC_OK) { Log.w(TAG, "getDistance:HttpStatus=" + status); return -1; } is = response.getEntity().getContent(); } catch (ClientProtocolException e) { Log.e(TAG, "getDistance:" + e.toString()); e.printStackTrace(); return -1; } catch (URISyntaxException e) { Log.e(TAG, "getDistance:" + e.toString()); e.printStackTrace(); return -1; } catch (IOException e) { Log.e(TAG, "getDistance:" + e.toString()); e.printStackTrace(); return -1; } try { XmlPullParser parser = Xml.newPullParser(); parser.setInput(is, "UTF-8"); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: String tag = parser.getName(); if (tag.equals("distance")) { distance = Double.parseDouble(parser.nextText()); break; } break; } eventType = parser.next(); } if (distance < 0) { Log.w(TAG, "getDistance: Tag 'distance' not found"); return -1; } } catch (XmlPullParserException e) { Log.e(TAG, "getDistance:" + e.toString()); e.printStackTrace(); return -1; } catch (IOException e) { Log.e(TAG, "getDistance:" + e.toString()); e.printStackTrace(); return -1; } return distance; }