android: Add a parser for XML remediation instructions
authorTobias Brunner <tobias@strongswan.org>
Fri, 17 May 2013 11:08:54 +0000 (13:08 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 8 Jul 2013 16:49:28 +0000 (18:49 +0200)
src/frontends/android/src/org/strongswan/android/logic/imc/RemediationInstruction.java [new file with mode: 0644]

diff --git a/src/frontends/android/src/org/strongswan/android/logic/imc/RemediationInstruction.java b/src/frontends/android/src/org/strongswan/android/logic/imc/RemediationInstruction.java
new file mode 100644 (file)
index 0000000..8c7f175
--- /dev/null
@@ -0,0 +1,229 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+package org.strongswan.android.logic.imc;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+
+import android.util.Xml;
+
+public class RemediationInstruction
+{
+       private String mTitle;
+       private String mDescription;
+       private String mHeader;
+       private final List<String> mItems = new LinkedList<String>();
+
+       public String getTitle()
+       {
+               return mTitle;
+       }
+
+       private void setTitle(String title)
+       {
+               mTitle = title;
+       }
+
+       public String getDescription()
+       {
+               return mDescription;
+       }
+
+       private void setDescription(String description)
+       {
+               mDescription = description;
+       }
+
+       public String getHeader()
+       {
+               return mHeader;
+       }
+
+       private void setHeader(String header)
+       {
+               mHeader = header;
+       }
+
+       public List<String> getItems()
+       {
+               return Collections.unmodifiableList(mItems);
+       }
+
+       private void addItem(String item)
+       {
+               mItems.add(item);
+       }
+
+       /**
+        * Create a list of RemediationInstruction objects from the given XML data.
+        *
+        * @param xml XML data
+        * @return list of RemediationInstruction objects
+        */
+       public static List<RemediationInstruction> fromXml(String xml)
+       {
+               List<RemediationInstruction> instructions = new LinkedList<RemediationInstruction>();
+               XmlPullParser parser = Xml.newPullParser();
+               try
+               {
+                       parser.setInput(new StringReader(xml));
+                       parser.nextTag();
+                       readInstructions(parser, instructions);
+               }
+               catch (XmlPullParserException e)
+               {
+                       e.printStackTrace();
+               }
+               catch (IOException e)
+               {
+                       e.printStackTrace();
+               }
+               return instructions;
+       }
+
+       /**
+        * Read a &lt;remediationinstructions&gt; element and store the extracted
+        * RemediationInstruction objects in the given list.
+        *
+        * @param parser
+        * @param instructions
+        * @throws XmlPullParserException
+        * @throws IOException
+        */
+       private static void readInstructions(XmlPullParser parser, List<RemediationInstruction> instructions) throws XmlPullParserException, IOException
+       {
+               parser.require(XmlPullParser.START_TAG, null, "remediationinstructions");
+               while (parser.next() != XmlPullParser.END_TAG)
+               {
+                       if (parser.getEventType() != XmlPullParser.START_TAG)
+                       {
+                               continue;
+                       }
+                       if (parser.getName().equals("instruction"))
+                       {
+                               RemediationInstruction instruction = new RemediationInstruction();
+                               readInstruction(parser, instruction);
+                               instructions.add(instruction);
+                       }
+                       else
+                       {
+                               skipTag(parser);
+                       }
+               }
+       }
+
+       /**
+        * Read an &lt;instruction&gt; element and store the information in the
+        * given RemediationInstruction object.
+        *
+        * @param parser
+        * @param instruction
+        * @throws XmlPullParserException
+        * @throws IOException
+        */
+       private static void readInstruction(XmlPullParser parser, RemediationInstruction instruction) throws XmlPullParserException, IOException
+       {
+               parser.require(XmlPullParser.START_TAG, null, "instruction");
+               while (parser.next() != XmlPullParser.END_TAG)
+               {
+                       if (parser.getEventType() != XmlPullParser.START_TAG)
+                       {
+                               continue;
+                       }
+                       String name = parser.getName();
+                       if (name.equals("title"))
+                       {
+                               instruction.setTitle(parser.nextText());
+                       }
+                       else if (name.equals("description"))
+                       {
+                               instruction.setDescription(parser.nextText());
+                       }
+                       else if (name.equals("itemsheader"))
+                       {
+                               instruction.setHeader(parser.nextText());
+                       }
+                       else if (name.equals("items"))
+                       {
+                               readItems(parser, instruction);
+                       }
+                       else
+                       {
+                               skipTag(parser);
+                       }
+               }
+       }
+
+       /**
+        * Read all items of an &lt;items&gt; node and add them to the given
+        * RemediationInstruction object.
+        *
+        * @param parser
+        * @param instruction
+        * @throws XmlPullParserException
+        * @throws IOException
+        */
+       private static void readItems(XmlPullParser parser, RemediationInstruction instruction) throws XmlPullParserException, IOException
+       {
+               while (parser.next() != XmlPullParser.END_TAG)
+               {
+                       if (parser.getEventType() != XmlPullParser.START_TAG)
+                       {
+                               continue;
+                       }
+                       if (parser.getName().equals("item"))
+                       {
+                               instruction.addItem(parser.nextText());
+                       }
+                       else
+                       {
+                               skipTag(parser);
+                       }
+               }
+       }
+
+       /**
+        * Skip the current tag and all child elements.
+        *
+        * @param parser
+        * @throws XmlPullParserException
+        * @throws IOException
+        */
+       private static void skipTag(XmlPullParser parser) throws XmlPullParserException, IOException
+       {
+               int depth = 1;
+
+               parser.require(XmlPullParser.START_TAG, null, null);
+               while (depth != 0)
+               {
+                       switch (parser.next())
+                       {
+                               case XmlPullParser.END_TAG:
+                                       depth--;
+                                       break;
+                               case XmlPullParser.START_TAG:
+                                       depth++;
+                                       break;
+                       }
+               }
+       }
+}