]> git.ipfire.org Git - thirdparty/strongswan.git/blob
5435ad88c40e7b5bf046668561e6a40843ac7eab
[thirdparty/strongswan.git] /
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 package org.strongswan.android.logic.imc;
17
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;
23
24 import org.xmlpull.v1.XmlPullParser;
25 import org.xmlpull.v1.XmlPullParserException;
26
27 import android.os.Parcel;
28 import android.os.Parcelable;
29 import android.util.Xml;
30
31 public class RemediationInstruction implements Parcelable
32 {
33 private String mTitle;
34 private String mDescription;
35 private String mHeader;
36 private final List<String> mItems = new LinkedList<String>();
37
38 @Override
39 public int describeContents()
40 {
41 return 0;
42 }
43
44 @Override
45 public void writeToParcel(Parcel dest, int flags)
46 {
47 dest.writeString(mTitle);
48 dest.writeString(mDescription);
49 dest.writeString(mHeader);
50 dest.writeStringList(mItems);
51 }
52
53 public static final Parcelable.Creator<RemediationInstruction> CREATOR = new Creator<RemediationInstruction>() {
54
55 @Override
56 public RemediationInstruction[] newArray(int size)
57 {
58 return new RemediationInstruction[size];
59 }
60
61 @Override
62 public RemediationInstruction createFromParcel(Parcel source)
63 {
64 return new RemediationInstruction(source);
65 }
66 };
67
68 private RemediationInstruction()
69 {
70 }
71
72 private RemediationInstruction(Parcel source)
73 {
74 mTitle = source.readString();
75 mDescription = source.readString();
76 mHeader = source.readString();
77 source.readStringList(mItems);
78 }
79
80 public String getTitle()
81 {
82 return mTitle;
83 }
84
85 private void setTitle(String title)
86 {
87 mTitle = title;
88 }
89
90 public String getDescription()
91 {
92 return mDescription;
93 }
94
95 private void setDescription(String description)
96 {
97 mDescription = description;
98 }
99
100 public String getHeader()
101 {
102 return mHeader;
103 }
104
105 private void setHeader(String header)
106 {
107 mHeader = header;
108 }
109
110 public List<String> getItems()
111 {
112 return Collections.unmodifiableList(mItems);
113 }
114
115 private void addItem(String item)
116 {
117 mItems.add(item);
118 }
119
120 /**
121 * Create a list of RemediationInstruction objects from the given XML data.
122 *
123 * @param xml XML data
124 * @return list of RemediationInstruction objects
125 */
126 public static List<RemediationInstruction> fromXml(String xml)
127 {
128 List<RemediationInstruction> instructions = new LinkedList<RemediationInstruction>();
129 XmlPullParser parser = Xml.newPullParser();
130 try
131 {
132 parser.setInput(new StringReader(xml));
133 parser.nextTag();
134 readInstructions(parser, instructions);
135 }
136 catch (XmlPullParserException e)
137 {
138 e.printStackTrace();
139 }
140 catch (IOException e)
141 {
142 e.printStackTrace();
143 }
144 return instructions;
145 }
146
147 /**
148 * Read a &lt;remediationinstructions&gt; element and store the extracted
149 * RemediationInstruction objects in the given list.
150 *
151 * @param parser
152 * @param instructions
153 * @throws XmlPullParserException
154 * @throws IOException
155 */
156 private static void readInstructions(XmlPullParser parser, List<RemediationInstruction> instructions) throws XmlPullParserException, IOException
157 {
158 parser.require(XmlPullParser.START_TAG, null, "remediationinstructions");
159 while (parser.next() != XmlPullParser.END_TAG)
160 {
161 if (parser.getEventType() != XmlPullParser.START_TAG)
162 {
163 continue;
164 }
165 if (parser.getName().equals("instruction"))
166 {
167 RemediationInstruction instruction = new RemediationInstruction();
168 readInstruction(parser, instruction);
169 instructions.add(instruction);
170 }
171 else
172 {
173 skipTag(parser);
174 }
175 }
176 }
177
178 /**
179 * Read an &lt;instruction&gt; element and store the information in the
180 * given RemediationInstruction object.
181 *
182 * @param parser
183 * @param instruction
184 * @throws XmlPullParserException
185 * @throws IOException
186 */
187 private static void readInstruction(XmlPullParser parser, RemediationInstruction instruction) throws XmlPullParserException, IOException
188 {
189 parser.require(XmlPullParser.START_TAG, null, "instruction");
190 while (parser.next() != XmlPullParser.END_TAG)
191 {
192 if (parser.getEventType() != XmlPullParser.START_TAG)
193 {
194 continue;
195 }
196 String name = parser.getName();
197 if (name.equals("title"))
198 {
199 instruction.setTitle(parser.nextText());
200 }
201 else if (name.equals("description"))
202 {
203 instruction.setDescription(parser.nextText());
204 }
205 else if (name.equals("itemsheader"))
206 {
207 instruction.setHeader(parser.nextText());
208 }
209 else if (name.equals("items"))
210 {
211 readItems(parser, instruction);
212 }
213 else
214 {
215 skipTag(parser);
216 }
217 }
218 }
219
220 /**
221 * Read all items of an &lt;items&gt; node and add them to the given
222 * RemediationInstruction object.
223 *
224 * @param parser
225 * @param instruction
226 * @throws XmlPullParserException
227 * @throws IOException
228 */
229 private static void readItems(XmlPullParser parser, RemediationInstruction instruction) throws XmlPullParserException, IOException
230 {
231 while (parser.next() != XmlPullParser.END_TAG)
232 {
233 if (parser.getEventType() != XmlPullParser.START_TAG)
234 {
235 continue;
236 }
237 if (parser.getName().equals("item"))
238 {
239 instruction.addItem(parser.nextText());
240 }
241 else
242 {
243 skipTag(parser);
244 }
245 }
246 }
247
248 /**
249 * Skip the current tag and all child elements.
250 *
251 * @param parser
252 * @throws XmlPullParserException
253 * @throws IOException
254 */
255 private static void skipTag(XmlPullParser parser) throws XmlPullParserException, IOException
256 {
257 int depth = 1;
258
259 parser.require(XmlPullParser.START_TAG, null, null);
260 while (depth != 0)
261 {
262 switch (parser.next())
263 {
264 case XmlPullParser.END_TAG:
265 depth--;
266 break;
267 case XmlPullParser.START_TAG:
268 depth++;
269 break;
270 }
271 }
272 }
273 }