2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 package org.strongswan.android.logic.imc;
18 import java.io.IOException;
19 import java.io.StringReader;
20 import java.util.Collections;
21 import java.util.LinkedList;
22 import java.util.List;
24 import org.xmlpull.v1.XmlPullParser;
25 import org.xmlpull.v1.XmlPullParserException;
27 import android.os.Parcel;
28 import android.os.Parcelable;
29 import android.util.Xml;
31 public class RemediationInstruction implements Parcelable
33 private String mTitle;
34 private String mDescription;
35 private String mHeader;
36 private final List<String> mItems = new LinkedList<String>();
39 public int describeContents()
45 public void writeToParcel(Parcel dest, int flags)
47 dest.writeString(mTitle);
48 dest.writeString(mDescription);
49 dest.writeString(mHeader);
50 dest.writeStringList(mItems);
53 public static final Parcelable.Creator<RemediationInstruction> CREATOR = new Creator<RemediationInstruction>() {
56 public RemediationInstruction[] newArray(int size)
58 return new RemediationInstruction[size];
62 public RemediationInstruction createFromParcel(Parcel source)
64 return new RemediationInstruction(source);
68 private RemediationInstruction()
72 private RemediationInstruction(Parcel source)
74 mTitle = source.readString();
75 mDescription = source.readString();
76 mHeader = source.readString();
77 source.readStringList(mItems);
80 public String getTitle()
85 private void setTitle(String title)
90 public String getDescription()
95 private void setDescription(String description)
97 mDescription = description;
100 public String getHeader()
105 private void setHeader(String header)
110 public List<String> getItems()
112 return Collections.unmodifiableList(mItems);
115 private void addItem(String item)
121 * Create a list of RemediationInstruction objects from the given XML data.
123 * @param xml XML data
124 * @return list of RemediationInstruction objects
126 public static List<RemediationInstruction> fromXml(String xml)
128 List<RemediationInstruction> instructions = new LinkedList<RemediationInstruction>();
129 XmlPullParser parser = Xml.newPullParser();
132 parser.setInput(new StringReader(xml));
134 readInstructions(parser, instructions);
136 catch (XmlPullParserException e)
140 catch (IOException e)
148 * Read a <remediationinstructions> element and store the extracted
149 * RemediationInstruction objects in the given list.
152 * @param instructions
153 * @throws XmlPullParserException
154 * @throws IOException
156 private static void readInstructions(XmlPullParser parser, List<RemediationInstruction> instructions) throws XmlPullParserException, IOException
158 parser.require(XmlPullParser.START_TAG, null, "remediationinstructions");
159 while (parser.next() != XmlPullParser.END_TAG)
161 if (parser.getEventType() != XmlPullParser.START_TAG)
165 if (parser.getName().equals("instruction"))
167 RemediationInstruction instruction = new RemediationInstruction();
168 readInstruction(parser, instruction);
169 instructions.add(instruction);
179 * Read an <instruction> element and store the information in the
180 * given RemediationInstruction object.
184 * @throws XmlPullParserException
185 * @throws IOException
187 private static void readInstruction(XmlPullParser parser, RemediationInstruction instruction) throws XmlPullParserException, IOException
189 parser.require(XmlPullParser.START_TAG, null, "instruction");
190 while (parser.next() != XmlPullParser.END_TAG)
192 if (parser.getEventType() != XmlPullParser.START_TAG)
196 String name = parser.getName();
197 if (name.equals("title"))
199 instruction.setTitle(parser.nextText());
201 else if (name.equals("description"))
203 instruction.setDescription(parser.nextText());
205 else if (name.equals("itemsheader"))
207 instruction.setHeader(parser.nextText());
209 else if (name.equals("items"))
211 readItems(parser, instruction);
221 * Read all items of an <items> node and add them to the given
222 * RemediationInstruction object.
226 * @throws XmlPullParserException
227 * @throws IOException
229 private static void readItems(XmlPullParser parser, RemediationInstruction instruction) throws XmlPullParserException, IOException
231 while (parser.next() != XmlPullParser.END_TAG)
233 if (parser.getEventType() != XmlPullParser.START_TAG)
237 if (parser.getName().equals("item"))
239 instruction.addItem(parser.nextText());
249 * Skip the current tag and all child elements.
252 * @throws XmlPullParserException
253 * @throws IOException
255 private static void skipTag(XmlPullParser parser) throws XmlPullParserException, IOException
259 parser.require(XmlPullParser.START_TAG, null, null);
262 switch (parser.next())
264 case XmlPullParser.END_TAG:
267 case XmlPullParser.START_TAG: