From: Tobias Brunner Date: Fri, 17 May 2013 11:08:54 +0000 (+0200) Subject: android: Add a parser for XML remediation instructions X-Git-Tag: 5.1.0dr2~2^2~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0484989dbda41990dbf269c24425650c4cd39649;p=thirdparty%2Fstrongswan.git android: Add a parser for XML remediation instructions --- 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 index 0000000000..8c7f1753cc --- /dev/null +++ b/src/frontends/android/src/org/strongswan/android/logic/imc/RemediationInstruction.java @@ -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 . + * + * 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 mItems = new LinkedList(); + + 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 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 fromXml(String xml) + { + List instructions = new LinkedList(); + 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 <remediationinstructions> 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 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 <instruction> 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 <items> 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; + } + } + } +}