Description
Read a HashSet from an InputStream containing XML.
License
Apache License
Parameter
Parameter | Description |
---|
in | The InputStream from which to read. |
Exception
Parameter | Description |
---|
XmlPullParserException | an exception |
Return
HashSet The resulting set.
Declaration
public static final HashSet readSetXml(InputStream in)
throws XmlPullParserException, java.io.IOException
Method Source Code
//package com.java2s;
/*//from w ww . j a va2 s .c o m
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class Main {
/**
* Read a HashSet from an InputStream containing XML. The stream can
* previously have been written by writeSetXml().
*
* @param in The InputStream from which to read.
* @return HashSet The resulting set.
* @throws XmlPullParserException
* @throws java.io.IOException
* @see #readValueXml
* @see #readThisSetXml
* @see #writeSetXml
*/
public static final HashSet readSetXml(InputStream in)
throws XmlPullParserException, java.io.IOException {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, null);
return (HashSet) readValueXml(parser, new String[1]);
}
/**
* Read a flattened object from an XmlPullParser. The XML data could
* previously have been written with writeMapXml(), writeListXml(), or
* writeValueXml(). The XmlPullParser must be positioned <em>at</em> the tag
* that defines the value.
*
* @param parser The XmlPullParser from which to read the object.
* @param name An array of one string, used to return the name attribute of
* the value's tag.
* @return Object The newly generated value object.
* @see #readMapXml
* @see #readListXml
* @see #writeValueXml
*/
public static final Object readValueXml(XmlPullParser parser,
String[] name) throws XmlPullParserException,
java.io.IOException {
int eventType = parser.getEventType();
do {
if (eventType == parser.START_TAG) {
return readThisValueXml(parser, name);
} else if (eventType == parser.END_TAG) {
throw new XmlPullParserException("Unexpected end tag at: "
+ parser.getName());
} else if (eventType == parser.TEXT) {
throw new XmlPullParserException("Unexpected text: "
+ parser.getText());
}
eventType = parser.next();
} while (eventType != parser.END_DOCUMENT);
throw new XmlPullParserException("Unexpected end of document");
}
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 (tagName.equals("int")) {
res = Integer.parseInt(parser.getAttributeValue(null, "value"));
} else if (tagName.equals("long")) {
res = Long.valueOf(parser.getAttributeValue(null, "value"));
} else if (tagName.equals("float")) {
res = new Float(parser.getAttributeValue(null, "value"));
} else if (tagName.equals("double")) {
res = new Double(parser.getAttributeValue(null, "value"));
} else if (tagName.equals("boolean")) {
res = Boolean.valueOf(parser.getAttributeValue(null, "value"));
} 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 + ">");
}
/**
* Read an int[] object from an XmlPullParser. The XML data could previously
* have been generated by writeIntArrayXml(). The XmlPullParser must be
* positioned <em>after</em> the tag that begins the list.
*
* @param parser The XmlPullParser from which to read the list data.
* @param endTag Name of the tag that will end the list, usually "list".
* @param name An array of one string, used to return the name attribute of
* the list's tag.
* @return Returns a newly generated int[].
* @see #readListXml
*/
public static final int[] readThisIntArrayXml(XmlPullParser parser,
String endTag, String[] name) throws XmlPullParserException,
java.io.IOException {
int num;
try {
num = Integer.parseInt(parser.getAttributeValue(null, "num"));
} catch (NullPointerException e) {
throw new XmlPullParserException(
"Need num attribute in byte-array");
} catch (NumberFormatException e) {
throw new XmlPullParserException(
"Not a number in num attribute in byte-array");
}
int[] array = new int[num];
int i = 0;
int eventType = parser.getEventType();
do {
if (eventType == parser.START_TAG) {
if (parser.getName().equals("item")) {
try {
array[i] = Integer.parseInt(parser
.getAttributeValue(null, "value"));
} catch (NullPointerException e) {
throw new XmlPullParserException(
"Need value attribute in item");
} catch (NumberFormatException e) {
throw new XmlPullParserException(
"Not a number in value attribute in item");
}
} else {
throw new XmlPullParserException(
"Expected item tag at: " + parser.getName());
}
} else if (eventType == parser.END_TAG) {
if (parser.getName().equals(endTag)) {
return array;
} else if (parser.getName().equals("item")) {
i++;
} else {
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");
}
/**
* Read a HashMap object from an XmlPullParser. The XML data could
* previously have been generated by writeMapXml(). The XmlPullParser must
* be positioned <em>after</em> the tag that begins the map.
*
* @param parser The XmlPullParser from which to read the map data.
* @param endTag Name of the tag that will end the map, usually "map".
* @param name An array of one string, used to return the name attribute of
* the map's tag.
* @return HashMap The newly generated map.
* @see #readMapXml
*/
public static final HashMap readThisMapXml(XmlPullParser parser,
String endTag, String[] name) throws XmlPullParserException,
java.io.IOException {
HashMap map = new HashMap();
int eventType = parser.getEventType();
do {
if (eventType == parser.START_TAG) {
Object val = readThisValueXml(parser, name);
if (name[0] != null) {
// System.out.println("Adding to map: " + name + " -> " +
// val);
map.put(name[0], val);
} else {
throw new XmlPullParserException(
"Map value without name attribute: "
+ parser.getName());
}
} else if (eventType == parser.END_TAG) {
if (parser.getName().equals(endTag)) {
return map;
}
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");
}
/**
* Read an ArrayList object from an XmlPullParser. The XML data could
* previously have been generated by writeListXml(). The XmlPullParser must
* be positioned <em>after</em> the tag that begins the list.
*
* @param parser The XmlPullParser from which to read the list data.
* @param endTag Name of the tag that will end the list, usually "list".
* @param name An array of one string, used to return the name attribute of
* the list's tag.
* @return HashMap The newly generated list.
* @see #readListXml
*/
public static final ArrayList readThisListXml(XmlPullParser parser,
String endTag, String[] name) throws XmlPullParserException,
java.io.IOException {
ArrayList list = new ArrayList();
int eventType = parser.getEventType();
do {
if (eventType == parser.START_TAG) {
Object val = readThisValueXml(parser, name);
list.add(val);
// System.out.println("Adding to list: " + val);
} else if (eventType == parser.END_TAG) {
if (parser.getName().equals(endTag)) {
return list;
}
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");
}
/**
* 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");
}
}
Related
- readMapXml(InputStream in)
- readMapXml(InputStream in)
- readSetXml(InputStream in)
- readSetXml(InputStream in)
- readSetXml(InputStream in)
- InputStreamToDocument(InputStream istr)
- LoadXml(InputStream stream)
- getDocument(InputStream is)
- getPullParser(InputStream in, String encoding)