Commit 1bb8a080 authored by Martin LOISEAU's avatar Martin LOISEAU 🎄
Browse files

Continue parsing

parent 0eecc43e
......@@ -46,7 +46,6 @@ public class ScenarioLoader {
this.scenarioPath = scenarioPath;
}
private ArrayList<Node> XMLScenario(MainActivity mainActivity) throws XmlPullParserException, IOException {
/*
* based on this guide https://developer.android.com/training/basics/network-ops/xml#java
......@@ -58,39 +57,41 @@ public class ScenarioLoader {
Log.d("Debug2", "XML FILE :" + in.toString());
// InputStream in = new FileInputStream(new File(scenarioPath));
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
// return readFeed(parser);
{
ScenarioNodes = new ArrayList<Node>();
parser.require(XmlPullParser.START_TAG, ns, "liste");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("node")) {
Log.d("Debug2", "Node tag detected");
ScenarioNodes.add(readNode(parser));
} else {
skip(parser);
}
}
}
return readList(parser);
} finally {
in.close();
}
}
return HardCodedScenario(mainActivity);
private ArrayList<Node> readList(XmlPullParser parser) throws IOException, XmlPullParserException {
ScenarioNodes = new ArrayList<Node>();
parser.require(XmlPullParser.START_TAG, ns, "liste");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the node tag
if (name.equals("node")) {
Log.d("Debug2", "Node tag detected");
ScenarioNodes.add(readNode(parser));
Log.d("Debug2", "Added node to scenario");
} else {
throw new Error("A non-node tag was found in list. name: " + name);
}
}
Log.d("Debug2", "Reading finished");
return ScenarioNodes;
}
private Node readNode(XmlPullParser parser) throws XmlPullParserException, IOException {
......@@ -105,36 +106,120 @@ public class ScenarioLoader {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("id")) {
ID = readID(parser);
ID = readInt(parser, "id");
Log.d("Debug2", "ID read in XML: " + Integer.toString(ID));
// } else if (name.equals("required_atoms")) {
// Conditions = readAtoms(parser);
// } else if (name.equals("action_list")) {
// Actions = readActions(parser);
} else if (name.equals("required_atoms")) {
Log.d("Debug2", "Conditions found in node " + Integer.toString(ID) + " in XML");
//Conditions = readAtoms(parser);
} else if (name.equals("action_list")) {
Log.d("Debug2", "Actions found in node " + Integer.toString(ID) + " in XML");
//Actions = readActions(parser, ID);
} else {
Log.d("Debug2", "skipped a non id balise ");
skip(parser);
throw new Error("An unexpected tag was found in node " + Integer.toString(ID) + " XML definition");
}
}
Log.d("Debug2", "finished reading node " + Integer.toString(ID) + ": " + Integer.toString(Conditions.size()) + " atoms and " + Actions.size() + " actions");
return new Node(mainActivity, ID, Actions, Conditions);
}
/*
private ArrayList<Action> readActions(XmlPullParser parser) {
private ArrayList<Action> readActions(XmlPullParser parser, int nodeID) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "action_list");
ArrayList<Action> Actions = new ArrayList<Action>();
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("AddAtom")) {
Actions.add(new AddAtom(mainActivity, nodeID, readAtom(parser)));
// parser.next();
}
else if (name.equals("TTSReading")) {
Actions.add(new TTSReading(mainActivity, nodeID, readText(parser, "text_to_read")));
// parser.next();
}
else if (name.equals("ClearNodes")) {
Actions.add(new ClearNodes(mainActivity, nodeID));
parser.nextTag();
}
else if (name.equals("AddNode")) {
Actions.add(new AddNode(mainActivity, nodeID, readInt(parser, "node_id")));
// parser.next();
} else {
throw new Error("Unexpected action tag found in node " + Integer.toString(nodeID) + " XML definition");
// skip(parser);
}
}
return Actions;
}
private ArrayList<Atom> readAtoms(XmlPullParser parser) {
private ArrayList<Atom> readAtoms(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "required_atoms");
ArrayList<Atom> Atoms = new ArrayList<Atom>();
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
}*/
String name = parser.getName();
if (name.equals("atom")) {
Atoms.add(readAtom(parser));
} else {
throw new Error("A non-atom tag was found in atom list in XML file");
}
}
return Atoms;
}
private Atom readAtom(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "atom");
private int readID(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "id");
int ID = readNumber(parser);
parser.require(XmlPullParser.END_TAG, ns, "id");
return ID;
String type = "", Content = "";
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("content")) {
Content = readText(parser, "content");
Log.d("Debug2", "atom ID read in XML: " + Content);
} else if (name.equals("type")) {
type = readText(parser, "type");
}
else
{
throw new Error("An unexpected tag was found in an atom definition (other than type or content)");
}
}
if(type.equals("QRAtom")) return new QRAtom(Content);
else if(type.equals("SpeechAtom")) return new SpeechAtom(Content);
else return new ItemAtom(Integer.parseInt(Content));
}
private String readText(XmlPullParser parser, String tag) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, tag);
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
parser.require(XmlPullParser.END_TAG, ns, tag);
return result;
}
private int readInt(XmlPullParser parser, String tag)throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, tag);
int _int = readNumber(parser);
parser.require(XmlPullParser.END_TAG, ns, tag);
return _int;
}
private int readNumber(XmlPullParser parser) throws IOException, XmlPullParserException {
......@@ -146,6 +231,7 @@ public class ScenarioLoader {
return result;
}
/*
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
......@@ -161,7 +247,7 @@ public class ScenarioLoader {
break;
}
}
}
}*/
......
<?xml version="1.0" encoding="utf-8"?>
<liste>
<node>
<id>1555</id>
<required_atoms>
</required_atoms>
<action_list>
<TTSReading>
<text_to_read type="text">"Bonjour 3" <text_to_read>
</TTSReading>
<ClearNodes> </ClearNodes>
<action>
</action_list>
</node>
<id>1</id>
<node>
<id>12</id>
<required_atoms>
</required_atoms>
<action_list>
</action_list>
<!--<action_list>
<TTSReading>
<text_to_read>Bonjour<text_to_read>
</TTSReading>
<ClearNodes> </ClearNodes>
<AddAtom>
<atom>
<type>ItemAtom</type>
<content>12</content>
</atom>
</AddAtom>
<AddNode>
<node_id>24</node_id>
</AddNode>
</action_list>-->
</node>
<node>
<id>91</id>
<!-- <id>24</id> -->
<required_atoms><!--
<atom>
<type>ItemAtom</type>
<content>12</content>
<atom>-->
</required_atoms>
<!--
<action_list><!--
<TTSReading>
<text_to_read>Au revoir<text_to_read>
</TTSReading>
</action_list>-->
</node>
</liste>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment