diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
commit | 27eda2eccd3729366a5545a21238ee9b2960171c (patch) | |
tree | 998a9af735ff91bb16c05e8411ac282c1fb57b48 /src/com/google/wireless/gdata/data/XmlUtils.java | |
download | gdata-27eda2eccd3729366a5545a21238ee9b2960171c.tar.gz |
Initial Contributionandroid-1.0release-1.0cdma-import
Diffstat (limited to 'src/com/google/wireless/gdata/data/XmlUtils.java')
-rw-r--r-- | src/com/google/wireless/gdata/data/XmlUtils.java | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/src/com/google/wireless/gdata/data/XmlUtils.java b/src/com/google/wireless/gdata/data/XmlUtils.java new file mode 100644 index 0000000..1c067d3 --- /dev/null +++ b/src/com/google/wireless/gdata/data/XmlUtils.java @@ -0,0 +1,133 @@ +// Copyright 2007 The Android Open Source Project + +package com.google.wireless.gdata.data; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; +import org.xmlpull.v1.XmlSerializer; + +import java.io.IOException; + +/** + * Utility class for working with an XmlPullParser. + */ +public final class XmlUtils { + // utility class + private XmlUtils() { + } + + /** + * Extracts the child text for the current element in the pull parser. + * @param parser The XmlPullParser parsing an XML document. + * @return The child text for the current element. May be null, if there + * is no child text. + * @throws XmlPullParserException Thrown if the child text could not be + * parsed. + * @throws IOException Thrown if the InputStream behind the parser cannot + * be read. + */ + public static String extractChildText(XmlPullParser parser) + throws XmlPullParserException, IOException { + // TODO: check that the current node is an element? + int eventType = parser.next(); + if (eventType != XmlPullParser.TEXT) { + return null; + } + return parser.getText(); + } + + public static String extractFirstChildTextIgnoreRest(XmlPullParser parser) + throws XmlPullParserException, IOException { + int parentDepth = parser.getDepth(); + int eventType = parser.next(); + String child = null; + while (eventType != XmlPullParser.END_DOCUMENT) { + int depth = parser.getDepth(); + + if (eventType == XmlPullParser.TEXT) { + if (child == null) { + child = parser.getText(); + } + } else if (eventType == XmlPullParser.END_TAG && depth == parentDepth) { + return child; + } + eventType = parser.next(); + } + throw new XmlPullParserException("End of document reached; never saw expected end tag at " + + "depth " + parentDepth); + } + + public static String nextDirectChildTag(XmlPullParser parser, int parentDepth) + throws XmlPullParserException, IOException { + int targetDepth = parentDepth + 1; + int eventType = parser.next(); + while (eventType != XmlPullParser.END_DOCUMENT) { + int depth = parser.getDepth(); + + if (eventType == XmlPullParser.START_TAG && depth == targetDepth) { + return parser.getName(); + } + + if (eventType == XmlPullParser.END_TAG && depth == parentDepth) { + return null; + } + eventType = parser.next(); + } + throw new XmlPullParserException("End of document reached; never saw expected end tag at " + + "depth " + parentDepth); + } + +// public static void parseChildrenToSerializer(XmlPullParser parser, XmlSerializer serializer) +// throws XmlPullParserException, IOException { +// int parentDepth = parser.getDepth(); +// int eventType = parser.getEventType(); +// while (eventType != XmlPullParser.END_DOCUMENT) { +// // TODO: call parser.nextToken(), so we get all entities, comments, whitespace, etc.? +// // find out if this is necessary. +// eventType = parser.next(); +// int depth = parser.getDepth(); +// String name; +// String ns; +// switch (eventType) { +// case XmlPullParser.START_TAG: +// name = parser.getName(); +// ns = parser.getNamespace(); +// // grab all of the namespace definitions between the previous depth and the +// // current depth (e.g., what was just defined in the start tag). +// int nstackBegin = parser.getNamespaceCount(depth - 1); +// int nstackEnd = parser.getNamespaceCount(depth); +// for (int i = nstackBegin; i < nstackEnd; ++i) { +// serializer.setPrefix(parser.getNamespacePrefix(i), +// parser.getNamespaceUri(i)); +// } +// serializer.startTag(ns, name); +// +// int numAttrs = parser.getAttributeCount(); +// for (int i = 0; i < numAttrs; ++i) { +// String attrNs = parser.getAttributeNamespace(i); +// String attrName = parser.getAttributeName(i); +// String attrValue = parser.getAttributeValue(i); +// serializer.attribute(attrNs, attrName, attrValue); +// } +// break; +// case XmlPullParser.END_TAG: +// if (depth == parentDepth) { +// // we're done. +// return; +// } +// name = parser.getName(); +// ns = parser.getNamespace(); +// serializer.endTag(ns, name); +// break; +// case XmlPullParser.TEXT: +// serializer.text(parser.getText()); +// break; +// default: +// // ignore the rest. +// break; +// } +// } +// throw new XmlPullParserException("End of document reached; never saw expected end tag " +// + "at depth " + parentDepth); +// } +} |