use of org.xmlpull.v1.XmlPullParser in project android_frameworks_base by ParanoidAndroid.
the class Xml method newPullParser.
/**
* Returns a new pull parser with namespace support.
*/
public static XmlPullParser newPullParser() {
try {
KXmlParser parser = new KXmlParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, true);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
return parser;
} catch (XmlPullParserException e) {
throw new AssertionError();
}
}
use of org.xmlpull.v1.XmlPullParser in project android_frameworks_base by ParanoidAndroid.
the class ActivityChooserModel method readHistoricalDataImpl.
/**
* Command for reading the historical records from a file off the UI thread.
*/
private void readHistoricalDataImpl() {
FileInputStream fis = null;
try {
fis = mContext.openFileInput(mHistoryFileName);
} catch (FileNotFoundException fnfe) {
if (DEBUG) {
Log.i(LOG_TAG, "Could not open historical records file: " + mHistoryFileName);
}
return;
}
try {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(fis, null);
int type = XmlPullParser.START_DOCUMENT;
while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
type = parser.next();
}
if (!TAG_HISTORICAL_RECORDS.equals(parser.getName())) {
throw new XmlPullParserException("Share records file does not start with " + TAG_HISTORICAL_RECORDS + " tag.");
}
List<HistoricalRecord> historicalRecords = mHistoricalRecords;
historicalRecords.clear();
while (true) {
type = parser.next();
if (type == XmlPullParser.END_DOCUMENT) {
break;
}
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
String nodeName = parser.getName();
if (!TAG_HISTORICAL_RECORD.equals(nodeName)) {
throw new XmlPullParserException("Share records file not well-formed.");
}
String activity = parser.getAttributeValue(null, ATTRIBUTE_ACTIVITY);
final long time = Long.parseLong(parser.getAttributeValue(null, ATTRIBUTE_TIME));
final float weight = Float.parseFloat(parser.getAttributeValue(null, ATTRIBUTE_WEIGHT));
HistoricalRecord readRecord = new HistoricalRecord(activity, time, weight);
historicalRecords.add(readRecord);
if (DEBUG) {
Log.i(LOG_TAG, "Read " + readRecord.toString());
}
}
if (DEBUG) {
Log.i(LOG_TAG, "Read " + historicalRecords.size() + " historical records.");
}
} catch (XmlPullParserException xppe) {
Log.e(LOG_TAG, "Error reading historical recrod file: " + mHistoryFileName, xppe);
} catch (IOException ioe) {
Log.e(LOG_TAG, "Error reading historical recrod file: " + mHistoryFileName, ioe);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException ioe) {
/* ignore */
}
}
}
}
use of org.xmlpull.v1.XmlPullParser in project android_frameworks_base by ParanoidAndroid.
the class XmlUtils method readThisValueXml.
private static final Object readThisValueXml(XmlPullParser parser, String[] name) throws XmlPullParserException, java.io.IOException {
final String valueName = parser.getAttributeValue(null, "name");
final String tagName = parser.getName();
//System.out.println("Reading this value tag: " + tagName + ", name=" + valueName);
Object res;
if (tagName.equals("null")) {
res = null;
} else if (tagName.equals("string")) {
String value = "";
int eventType;
while ((eventType = parser.next()) != parser.END_DOCUMENT) {
if (eventType == parser.END_TAG) {
if (parser.getName().equals("string")) {
name[0] = valueName;
//System.out.println("Returning value for " + valueName + ": " + value);
return value;
}
throw new XmlPullParserException("Unexpected end tag in <string>: " + parser.getName());
} else if (eventType == parser.TEXT) {
value += parser.getText();
} else if (eventType == parser.START_TAG) {
throw new XmlPullParserException("Unexpected start tag in <string>: " + parser.getName());
}
}
throw new XmlPullParserException("Unexpected end of document in <string>");
} else if ((res = readThisPrimitiveValueXml(parser, tagName)) != null) {
// all work already done by readThisPrimitiveValueXml
} else if (tagName.equals("int-array")) {
parser.next();
res = readThisIntArrayXml(parser, "int-array", name);
name[0] = valueName;
//System.out.println("Returning value for " + valueName + ": " + res);
return res;
} else if (tagName.equals("map")) {
parser.next();
res = readThisMapXml(parser, "map", name);
name[0] = valueName;
//System.out.println("Returning value for " + valueName + ": " + res);
return res;
} else if (tagName.equals("list")) {
parser.next();
res = readThisListXml(parser, "list", name);
name[0] = valueName;
//System.out.println("Returning value for " + valueName + ": " + res);
return res;
} else if (tagName.equals("set")) {
parser.next();
res = readThisSetXml(parser, "set", name);
name[0] = valueName;
//System.out.println("Returning value for " + valueName + ": " + res);
return res;
} else {
throw new XmlPullParserException("Unknown tag: " + tagName);
}
// Skip through to end tag.
int eventType;
while ((eventType = parser.next()) != parser.END_DOCUMENT) {
if (eventType == parser.END_TAG) {
if (parser.getName().equals(tagName)) {
name[0] = valueName;
//System.out.println("Returning value for " + valueName + ": " + res);
return res;
}
throw new XmlPullParserException("Unexpected end tag in <" + tagName + ">: " + parser.getName());
} else if (eventType == parser.TEXT) {
throw new XmlPullParserException("Unexpected text in <" + tagName + ">: " + parser.getName());
} else if (eventType == parser.START_TAG) {
throw new XmlPullParserException("Unexpected start tag in <" + tagName + ">: " + parser.getName());
}
}
throw new XmlPullParserException("Unexpected end of document in <" + tagName + ">");
}
use of org.xmlpull.v1.XmlPullParser in project android_frameworks_base by ParanoidAndroid.
the class XmlUtils method readThisSetXml.
/**
* Read a HashSet object from an XmlPullParser. The XML data could previously
* have been generated by writeSetXml(). The XmlPullParser must be positioned
* <em>after</em> the tag that begins the set.
*
* @param parser The XmlPullParser from which to read the set data.
* @param endTag Name of the tag that will end the set, usually "set".
* @param name An array of one string, used to return the name attribute
* of the set's tag.
*
* @return HashSet The newly generated set.
*
* @throws XmlPullParserException
* @throws java.io.IOException
*
* @see #readSetXml
*/
public static final HashSet readThisSetXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, java.io.IOException {
HashSet set = new HashSet();
int eventType = parser.getEventType();
do {
if (eventType == parser.START_TAG) {
Object val = readThisValueXml(parser, name);
set.add(val);
//System.out.println("Adding to set: " + val);
} else if (eventType == parser.END_TAG) {
if (parser.getName().equals(endTag)) {
return set;
}
throw new XmlPullParserException("Expected " + endTag + " end tag at: " + parser.getName());
}
eventType = parser.next();
} while (eventType != parser.END_DOCUMENT);
throw new XmlPullParserException("Document ended before " + endTag + " end tag");
}
use of org.xmlpull.v1.XmlPullParser in project android_frameworks_base by ParanoidAndroid.
the class XmlUtils method readListXml.
/**
* Read an ArrayList from an InputStream containing XML. The stream can
* previously have been written by writeListXml().
*
* @param in The InputStream from which to read.
*
* @return ArrayList The resulting list.
*
* @see #readMapXml
* @see #readValueXml
* @see #readThisListXml
* @see #writeListXml
*/
public static final ArrayList readListXml(InputStream in) throws XmlPullParserException, java.io.IOException {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, null);
return (ArrayList) readValueXml(parser, new String[1]);
}
Aggregations