|
| 1 | +/** |
| 2 | + * Copyright (C) 2010-2014 eBusiness Information, Excilys Group |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed To in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package org.androidannotations.api.sharedpreferences; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.StringReader; |
| 20 | +import java.io.StringWriter; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.TreeSet; |
| 24 | + |
| 25 | +import org.xmlpull.v1.XmlPullParser; |
| 26 | +import org.xmlpull.v1.XmlPullParserException; |
| 27 | +import org.xmlpull.v1.XmlSerializer; |
| 28 | + |
| 29 | +import android.util.Log; |
| 30 | +import android.util.Xml; |
| 31 | + |
| 32 | +public final class SetXmlSerializer { |
| 33 | + |
| 34 | + private static final String NAMESPACE = ""; |
| 35 | + private static final String STRING_TAG = "AA_string"; |
| 36 | + private static final String SET_TAG = "AA_set"; |
| 37 | + |
| 38 | + private SetXmlSerializer() { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + public static String serialize(Set<String> set) { |
| 43 | + if (set == null) { |
| 44 | + set = Collections.emptySet(); |
| 45 | + } |
| 46 | + |
| 47 | + StringWriter writer = new StringWriter(); |
| 48 | + XmlSerializer serializer = Xml.newSerializer(); |
| 49 | + |
| 50 | + try { |
| 51 | + serializer.setOutput(writer); |
| 52 | + serializer.startTag(NAMESPACE, SET_TAG); |
| 53 | + |
| 54 | + for (String string : set) { |
| 55 | + serializer.startTag(NAMESPACE, STRING_TAG) // |
| 56 | + .text(string) // |
| 57 | + .endTag(NAMESPACE, STRING_TAG); |
| 58 | + } |
| 59 | + |
| 60 | + serializer.endTag(NAMESPACE, SET_TAG) // |
| 61 | + .endDocument(); |
| 62 | + |
| 63 | + } catch (IllegalArgumentException e) { |
| 64 | + |
| 65 | + } catch (IllegalStateException e) { |
| 66 | + |
| 67 | + } catch (IOException e) { |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + return writer.toString(); |
| 72 | + } |
| 73 | + |
| 74 | + public static Set<String> deserialize(String data) { |
| 75 | + Set<String> stringSet = new TreeSet<String>(); |
| 76 | + XmlPullParser parser = Xml.newPullParser(); |
| 77 | + |
| 78 | + try { |
| 79 | + parser.setInput(new StringReader(data)); |
| 80 | + parser.next(); |
| 81 | + parser.require(XmlPullParser.START_TAG, NAMESPACE, SET_TAG); |
| 82 | + |
| 83 | + while (parser.next() != XmlPullParser.END_TAG) { |
| 84 | + parser.require(XmlPullParser.START_TAG, NAMESPACE, STRING_TAG); |
| 85 | + |
| 86 | + parser.next(); |
| 87 | + parser.require(XmlPullParser.TEXT, null, null); |
| 88 | + stringSet.add(parser.getText()); |
| 89 | + |
| 90 | + parser.next(); |
| 91 | + parser.require(XmlPullParser.END_TAG, null, STRING_TAG); |
| 92 | + } |
| 93 | + } catch (XmlPullParserException e) { |
| 94 | + Log.w("getStringSet", e); |
| 95 | + return null; |
| 96 | + } catch (IOException e) { |
| 97 | + Log.w("getStringSet", e); |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + return stringSet; |
| 102 | + } |
| 103 | +} |
0 commit comments