1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-26 08:12:05 +01:00

Change DataFormProvider to new style provider design

By using e.g. string-switch/case.
This commit is contained in:
Florian Schmaus 2014-11-21 09:38:37 +01:00
parent bf557d498b
commit 848d2dbf13

View file

@ -42,122 +42,155 @@ public class DataFormProvider extends PacketExtensionProvider<DataForm> {
@Override @Override
public DataForm parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, public DataForm parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
SmackException { SmackException {
boolean done = false;
DataForm dataForm = new DataForm(parser.getAttributeValue("", "type")); DataForm dataForm = new DataForm(parser.getAttributeValue("", "type"));
while (!done) { outerloop: while (true) {
int eventType = parser.next(); int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) { switch (eventType) {
if (parser.getName().equals("instructions")) { case XmlPullParser.START_TAG:
String name = parser.getName();
String namespace = parser.getNamespace();
switch (name) {
case "instructions":
dataForm.addInstruction(parser.nextText()); dataForm.addInstruction(parser.nextText());
} break;
else if (parser.getName().equals("title")) { case "title":
dataForm.setTitle(parser.nextText()); dataForm.setTitle(parser.nextText());
} break;
else if (parser.getName().equals("field")) { case "field":
dataForm.addField(parseField(parser)); dataForm.addField(parseField(parser));
} break;
else if (parser.getName().equals("item")) { case "item":
dataForm.addItem(parseItem(parser)); dataForm.addItem(parseItem(parser));
} break;
else if (parser.getName().equals("reported")) { case "reported":
dataForm.setReportedData(parseReported(parser)); dataForm.setReportedData(parseReported(parser));
} break;
// See XEP-133 Example 32 for a corner case where the data form contains this extension. // See XEP-133 Example 32 for a corner case where the data form contains this extension.
else if (parser.getName().equals(RosterPacket.ELEMENT) && parser.getNamespace().equals(RosterPacket.NAMESPACE)) { case RosterPacket.ELEMENT:
if (namespace.equals(RosterPacket.NAMESPACE)) {
dataForm.addExtensionElement(RosterPacketProvider.INSTANCE.parse(parser)); dataForm.addExtensionElement(RosterPacketProvider.INSTANCE.parse(parser));
} }
break;
// See XEP-141 Data Forms Layout // See XEP-141 Data Forms Layout
else if (parser.getName().equals(DataLayout.ELEMENT) && parser.getNamespace().equals(DataLayout.NAMESPACE)) { case DataLayout.ELEMENT:
if (namespace.equals(DataLayout.NAMESPACE)) {
dataForm.addExtensionElement(DataLayoutProvider.parse(parser)); dataForm.addExtensionElement(DataLayoutProvider.parse(parser));
} }
} else if (eventType == XmlPullParser.END_TAG) { break;
if (parser.getName().equals(dataForm.getElementName())) {
done = true;
} }
break;
case XmlPullParser.END_TAG:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
} }
} }
return dataForm; return dataForm;
} }
private FormField parseField(XmlPullParser parser) throws XmlPullParserException, IOException { private FormField parseField(XmlPullParser parser) throws XmlPullParserException, IOException {
boolean done = false; final int initialDepth = parser.getDepth();
FormField formField = new FormField(parser.getAttributeValue("", "var")); FormField formField = new FormField(parser.getAttributeValue("", "var"));
formField.setLabel(parser.getAttributeValue("", "label")); formField.setLabel(parser.getAttributeValue("", "label"));
formField.setType(parser.getAttributeValue("", "type")); formField.setType(parser.getAttributeValue("", "type"));
while (!done) { outerloop: while (true) {
int eventType = parser.next(); int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) { switch (eventType) {
if (parser.getName().equals("desc")) { case XmlPullParser.START_TAG:
String name = parser.getName();
switch (name) {
case "desc":
formField.setDescription(parser.nextText()); formField.setDescription(parser.nextText());
} break;
else if (parser.getName().equals("value")) { case "value":
formField.addValue(parser.nextText()); formField.addValue(parser.nextText());
} break;
else if (parser.getName().equals("required")) { case "required":
formField.setRequired(true); formField.setRequired(true);
} break;
else if (parser.getName().equals("option")) { case "option":
formField.addOption(parseOption(parser)); formField.addOption(parseOption(parser));
break;
} }
} else if (eventType == XmlPullParser.END_TAG) { case XmlPullParser.END_TAG:
if (parser.getName().equals("field")) { if (parser.getDepth() == initialDepth) {
done = true; break outerloop;
} }
break;
} }
} }
return formField; return formField;
} }
private DataForm.Item parseItem(XmlPullParser parser) throws XmlPullParserException, IOException { private DataForm.Item parseItem(XmlPullParser parser) throws XmlPullParserException, IOException {
boolean done = false; final int initialDepth = parser.getDepth();
List<FormField> fields = new ArrayList<FormField>(); List<FormField> fields = new ArrayList<FormField>();
while (!done) { outerloop: while (true) {
int eventType = parser.next(); int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) { switch (eventType) {
if (parser.getName().equals("field")) { case XmlPullParser.START_TAG:
String name = parser.getName();
switch (name) {
case "field":
fields.add(parseField(parser)); fields.add(parseField(parser));
break;
} }
} else if (eventType == XmlPullParser.END_TAG) { break;
if (parser.getName().equals("item")) { case XmlPullParser.END_TAG:
done = true; if (parser.getDepth() == initialDepth) {
break outerloop;
} }
break;
} }
} }
return new DataForm.Item(fields); return new DataForm.Item(fields);
} }
private DataForm.ReportedData parseReported(XmlPullParser parser) throws XmlPullParserException, IOException { private DataForm.ReportedData parseReported(XmlPullParser parser) throws XmlPullParserException, IOException {
boolean done = false; final int initialDepth = parser.getDepth();
List<FormField> fields = new ArrayList<FormField>(); List<FormField> fields = new ArrayList<FormField>();
while (!done) { outerloop: while (true) {
int eventType = parser.next(); int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) { switch (eventType) {
if (parser.getName().equals("field")) { case XmlPullParser.START_TAG:
String name = parser.getName();
switch (name) {
case "field":
fields.add(parseField(parser)); fields.add(parseField(parser));
break;
} }
} else if (eventType == XmlPullParser.END_TAG) { break;
if (parser.getName().equals("reported")) { case XmlPullParser.END_TAG:
done = true; if (parser.getDepth() == initialDepth) {
break outerloop;
} }
break;
} }
} }
return new DataForm.ReportedData(fields); return new DataForm.ReportedData(fields);
} }
private FormField.Option parseOption(XmlPullParser parser) throws XmlPullParserException, IOException { private FormField.Option parseOption(XmlPullParser parser) throws XmlPullParserException, IOException {
boolean done = false; final int initialDepth = parser.getDepth();
FormField.Option option = null; FormField.Option option = null;
String label = parser.getAttributeValue("", "label"); String label = parser.getAttributeValue("", "label");
while (!done) { outerloop: while (true) {
int eventType = parser.next(); int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) { switch (eventType) {
if (parser.getName().equals("value")) { case XmlPullParser.START_TAG:
String name = parser.getName();
switch (name) {
case "value":
option = new FormField.Option(label, parser.nextText()); option = new FormField.Option(label, parser.nextText());
break;
} }
} else if (eventType == XmlPullParser.END_TAG) { break;
if (parser.getName().equals("option")) { case XmlPullParser.END_TAG:
done = true; if (parser.getDepth() == initialDepth) {
break outerloop;
} }
break;
} }
} }
return option; return option;