-
Notifications
You must be signed in to change notification settings - Fork 941
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrote both the upload and download methods and requested a new API key due to the disclosure of our last key. Contributions are required as the API key has not been white-listed and does not yet allow for the downloading of data.
- Loading branch information
1 parent
3b4e44b
commit c2dc93b
Showing
4 changed files
with
188 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
app/src/main/java/com/SecUpwN/AIMSICD/utils/StackOverflowXmlParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.SecUpwN.AIMSICD.utils; | ||
|
||
import org.xmlpull.v1.XmlPullParser; | ||
import org.xmlpull.v1.XmlPullParserException; | ||
|
||
import android.util.Xml; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class StackOverflowXmlParser { | ||
// We don't use namespaces | ||
private static final String ns = null; | ||
|
||
public List<Cell> parse(InputStream in) throws XmlPullParserException, IOException { | ||
try { | ||
XmlPullParser parser = Xml.newPullParser(); | ||
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); | ||
parser.setInput(in, null); | ||
parser.nextTag(); | ||
return readCells(parser); | ||
} finally { | ||
in.close(); | ||
} | ||
} | ||
|
||
private List<Cell> readCells(XmlPullParser parser) throws XmlPullParserException, IOException { | ||
List<Cell> cells = new ArrayList<>(); | ||
|
||
parser.require(XmlPullParser.START_TAG, ns, "rsp"); | ||
while (parser.next() != XmlPullParser.END_TAG) { | ||
if (parser.getEventType() != XmlPullParser.START_TAG) { | ||
continue; | ||
} | ||
String name = parser.getName(); | ||
// Starts by looking for the entry tag | ||
if (name.equals("cell")) { | ||
cells.add(readCell(parser)); | ||
} else { | ||
skip(parser); | ||
} | ||
} | ||
return cells; | ||
} | ||
|
||
// Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them off | ||
// to their respective "read" methods for processing. Otherwise, skips the tag. | ||
private Cell readCell(XmlPullParser parser) throws XmlPullParserException, IOException { | ||
Cell cell = new Cell(); | ||
parser.require(XmlPullParser.START_TAG, ns, "cell"); | ||
while (parser.next() != XmlPullParser.END_TAG) { | ||
if (parser.getEventType() != XmlPullParser.START_TAG) { | ||
continue; | ||
} | ||
String name = parser.getName(); | ||
switch (name) { | ||
case "lat": | ||
cell.setLat(readDouble(parser)); | ||
break; | ||
case "lon": | ||
cell.setLon(readDouble(parser)); | ||
break; | ||
case "mcc": | ||
cell.setMCC(readInt(parser)); | ||
break; | ||
case "mnc": | ||
cell.setMNC(readInt(parser)); | ||
break; | ||
case "cellid": | ||
cell.setCID(readInt(parser)); | ||
break; | ||
case "lac": | ||
cell.setLAC(readInt(parser)); | ||
break; | ||
default: | ||
skip(parser); | ||
break; | ||
} | ||
} | ||
return cell; | ||
} | ||
|
||
// For the tags title and summary, extracts their text values. | ||
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException { | ||
String result = ""; | ||
if (parser.next() == XmlPullParser.TEXT) { | ||
result = parser.getText(); | ||
parser.nextTag(); | ||
} | ||
return result; | ||
} | ||
|
||
// For tags containing double values. | ||
private double readDouble(XmlPullParser parser) throws IOException, XmlPullParserException { | ||
String result = ""; | ||
if (parser.next() == XmlPullParser.TEXT) { | ||
result = parser.getText(); | ||
parser.nextTag(); | ||
} | ||
return Double.valueOf(result); | ||
} | ||
|
||
// For tags containing double values. | ||
private int readInt(XmlPullParser parser) throws IOException, XmlPullParserException { | ||
String result = ""; | ||
if (parser.next() == XmlPullParser.TEXT) { | ||
result = parser.getText(); | ||
parser.nextTag(); | ||
} | ||
return Integer.valueOf(result); | ||
} | ||
|
||
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException { | ||
if (parser.getEventType() != XmlPullParser.START_TAG) { | ||
throw new IllegalStateException(); | ||
} | ||
int depth = 1; | ||
while (depth != 0) { | ||
switch (parser.next()) { | ||
case XmlPullParser.END_TAG: | ||
depth--; | ||
break; | ||
case XmlPullParser.START_TAG: | ||
depth++; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters