summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTarun Gupta <tarun1995gupta@gmail.com>2015-06-20 01:02:12 (GMT)
committerTarun Gupta <tarun1995gupta@gmail.com>2015-06-27 07:12:55 (GMT)
commita673d269487fd86efe7f9c5f9b4cd1c00cab556d (patch)
tree10145697d3153a8a170361c5355ad9bccee21776 /src
parent27212e007077418d18014286a46723fa26693864 (diff)
downloadstroke-a673d269487fd86efe7f9c5f9b4cd1c00cab556d.zip
stroke-a673d269487fd86efe7f9c5f9b4cd1c00cab556d.tar.bz2
Adds Form Elements and Version Element.
Adds FormPage, FormReportedRef, FormSection, FormText and Version Elements. Updates Form Elements, its Parser And Serializer. Updates SearchPayload Element, its Parser And Serializer. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Adds Search Payload Parser and Serializer Test. Updates tests for Form Parser and Serializer. Updates InBandRegistrationPayloadSerializer Test and MAMQuerySerializerTest. All tests passes. Change-Id: I8c620a3db39fe433bc9a5478b98d5caeaf9ed40b
Diffstat (limited to 'src')
-rw-r--r--src/com/isode/stroke/elements/Form.java81
-rw-r--r--src/com/isode/stroke/elements/FormField.java3
-rw-r--r--src/com/isode/stroke/elements/FormPage.java134
-rw-r--r--src/com/isode/stroke/elements/FormReportedRef.java16
-rw-r--r--src/com/isode/stroke/elements/FormSection.java125
-rw-r--r--src/com/isode/stroke/elements/FormText.java41
-rw-r--r--src/com/isode/stroke/elements/SearchPayload.java18
-rw-r--r--src/com/isode/stroke/elements/Version.java82
-rw-r--r--src/com/isode/stroke/parser/payloadparsers/FormParser.java163
-rw-r--r--src/com/isode/stroke/parser/payloadparsers/SearchPayloadParser.java78
-rw-r--r--src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java87
-rw-r--r--src/com/isode/stroke/serializer/payloadserializers/SearchPayloadSerializer.java9
12 files changed, 753 insertions, 84 deletions
diff --git a/src/com/isode/stroke/elements/Form.java b/src/com/isode/stroke/elements/Form.java
index 8068f28..a6ff0c2 100644
--- a/src/com/isode/stroke/elements/Form.java
+++ b/src/com/isode/stroke/elements/Form.java
@@ -108,6 +108,10 @@ public class Form extends Payload {
}
}
+ private List<FormReportedRef> reportedRefs_ = new ArrayList<FormReportedRef>();
+ private List<FormText> textElements_ = new ArrayList<FormText>();
+ private List<FormPage> pages_ = new ArrayList<FormPage>();
+ private FormReportedRef reportedRef_;
private List<FormField> fields_ = new ArrayList<FormField>();
private List<FormField> reportedFields_ = new ArrayList<FormField>();
private List<FormItem> items_ = new ArrayList<FormItem>();
@@ -132,6 +136,51 @@ public class Form extends Payload {
}
/**
+ * @param reportedRef, Not Null.
+ */
+ public void addReportedRef(FormReportedRef reportedRef) {
+ assert(reportedRef != null);
+ reportedRefs_.add(reportedRef);
+ }
+
+ /**
+ * @return reportedRef, Not Null.
+ */
+ public List<FormReportedRef> getReportedRefs() {
+ return reportedRefs_;
+ }
+
+ /**
+ * @param text, Not Null.
+ */
+ public void addTextElement(FormText text) {
+ assert(text != null);
+ textElements_.add(text);
+ }
+
+ /**
+ * @return text, Not Null.
+ */
+ public List<FormText> getTextElements() {
+ return textElements_;
+ }
+
+ /**
+ * @return page, Not Null.
+ */
+ public void addPage(FormPage page) {
+ assert(page != null);
+ pages_.add(page);
+ }
+
+ /**
+ * @return pages, Not Null.
+ */
+ public List<FormPage> getPages() {
+ return pages_;
+ }
+
+ /**
* Add to the list of fields for the form.
*
* @param field Field to add, must not be null. The instance of the form
@@ -153,7 +202,11 @@ public class Form extends Payload {
public List<FormField> getFields() {
return new ArrayList<FormField>(fields_);
}
-
+
+ public void clearFields() {
+ fields_.clear();
+ }
+
/**
* Add a reported element to this Form.
* @param reportedField should not be null
@@ -183,7 +236,11 @@ public class Form extends Payload {
}
items_.add(item);
}
-
+
+ public void clearItems() {
+ items_.clear();
+ }
+
/**
* Get the list of FormItem elements for the form.
* @return itemsCopy ArrayList<List<FormItem>>, list of items for the Form,
@@ -295,6 +352,26 @@ public class Form extends Payload {
return null;
}
+ public void clearEmptyTextFields() {
+ List<FormField> populatedFields = new ArrayList<FormField>();
+ for (FormField field : fields_) {
+ if (field.getType() == FormField.Type.TEXT_SINGLE_TYPE) {
+ if (!field.getTextSingleValue().isEmpty()) {
+ populatedFields.add(field);
+ }
+ }
+ else if (field.getType() == FormField.Type.TEXT_MULTI_TYPE) {
+ if (!field.getTextMultiValue().isEmpty()) {
+ populatedFields.add(field);
+ }
+ }
+ else {
+ populatedFields.add(field);
+ }
+ }
+ fields_ = populatedFields;
+ }
+
@Override
public String toString() {
return Form.class.getSimpleName() + "\ntitle: " + title_
diff --git a/src/com/isode/stroke/elements/FormField.java b/src/com/isode/stroke/elements/FormField.java
index 84b6af1..c38772f 100644
--- a/src/com/isode/stroke/elements/FormField.java
+++ b/src/com/isode/stroke/elements/FormField.java
@@ -88,9 +88,6 @@ public class FormField {
public FormField(Type type) {
type_ = type;
required_ = false;
- if (type == Type.BOOLEAN_TYPE) {
- setBoolValue(false);
- }
}
/**
diff --git a/src/com/isode/stroke/elements/FormPage.java b/src/com/isode/stroke/elements/FormPage.java
new file mode 100644
index 0000000..b23a6e2
--- /dev/null
+++ b/src/com/isode/stroke/elements/FormPage.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2015 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.elements;
+
+import com.isode.stroke.base.NotNull;
+import com.isode.stroke.elements.FormField;
+import com.isode.stroke.elements.FormReportedRef;
+import com.isode.stroke.elements.FormText;
+import com.isode.stroke.elements.FormSection;
+import java.util.Vector;
+
+public class FormPage {
+
+ private String label_ = "";
+ private String xmlns_ = "";
+ private Vector<FormText> textElements_ = new Vector<FormText>();
+ private Vector<FormReportedRef> reportedRefs_ = new Vector<FormReportedRef>();
+ private Vector<FormSection> childSections_ = new Vector<FormSection>();
+ private Vector<FormField> fields_ = new Vector<FormField>();
+ private Vector<String> fieldRefs_ = new Vector<String>();
+
+ /**
+ * Default Constructor.
+ */
+ public FormPage() {
+ this.xmlns_ = "http://jabber.org/protocol/xdata-layout";
+ }
+
+ /**
+ * @param label, Not Null.
+ */
+ public void setLabel(String label) {
+ NotNull.exceptIfNull(label, "label");
+ label_ = label;
+ }
+
+ /**
+ * @return label, Not Null.
+ */
+ public String getLabel() {
+ return label_;
+ }
+
+ /**
+ * @return xmlns, Not Null.
+ */
+ public String getXMLNS() {
+ return xmlns_;
+ }
+
+ /**
+ * @param textElement, Not Null.
+ */
+ public void addTextElement(FormText textElement) {
+ NotNull.exceptIfNull(textElement, "textElement");
+ textElements_.add(textElement);
+ }
+
+ /**
+ * @return textElement, Not Null.
+ */
+ public Vector<FormText> getTextElements() {
+ return textElements_;
+ }
+
+ /**
+ * @param reportedRef, Not Null.
+ */
+ public void addReportedRef(FormReportedRef reportedRef) {
+ NotNull.exceptIfNull(reportedRef, "reportedRef");
+ reportedRefs_.add(reportedRef);
+ }
+
+ /**
+ * @return reportedRef, Not Null.
+ */
+ public Vector<FormReportedRef> getReportedRefs() {
+ return reportedRefs_;
+ }
+
+ /**
+ * @param childSection, Not Null.
+ */
+ public void addChildSection(FormSection childSection) {
+ NotNull.exceptIfNull(childSection, "childSection");
+ childSections_.add(childSection);
+ }
+
+ /**
+ * @return childSection, Not Null.
+ */
+ public Vector<FormSection> getChildSections() {
+ return childSections_;
+ }
+
+ /**
+ * @param field, Not Null.
+ */
+ public void addField(FormField field) {
+ NotNull.exceptIfNull(field, "field");
+ fields_.add(field);
+ }
+
+ /**
+ * @return field, Not Null.
+ */
+ public Vector<FormField> getFields() {
+ return fields_;
+ }
+
+ /**
+ * @param ref, Not Null.
+ */
+ public void addFieldRef(String ref) {
+ NotNull.exceptIfNull(ref, "ref");
+ fieldRefs_.add(ref);
+ }
+
+ /**
+ * @return ref, Not Null.
+ */
+ public Vector<String> getFieldRefs() {
+ return fieldRefs_;
+ }
+} \ No newline at end of file
diff --git a/src/com/isode/stroke/elements/FormReportedRef.java b/src/com/isode/stroke/elements/FormReportedRef.java
new file mode 100644
index 0000000..1785ce1
--- /dev/null
+++ b/src/com/isode/stroke/elements/FormReportedRef.java
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2015 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.elements;
+
+public class FormReportedRef {
+
+} \ No newline at end of file
diff --git a/src/com/isode/stroke/elements/FormSection.java b/src/com/isode/stroke/elements/FormSection.java
new file mode 100644
index 0000000..8cc42b3
--- /dev/null
+++ b/src/com/isode/stroke/elements/FormSection.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2015 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.elements;
+
+import com.isode.stroke.base.NotNull;
+import com.isode.stroke.elements.FormField;
+import com.isode.stroke.elements.FormReportedRef;
+import com.isode.stroke.elements.FormText;
+import java.util.Vector;
+
+public class FormSection {
+
+ private String label_ = "";
+ private Vector<FormText> textElements_ = new Vector<FormText>();
+ private Vector<FormReportedRef> reportedRefs_ = new Vector<FormReportedRef>();
+ private Vector<FormSection> childSections_ = new Vector<FormSection>();
+ private Vector<FormField> fields_ = new Vector<FormField>();
+ private Vector<String> fieldRefs_ = new Vector<String>();
+
+ /**
+ * Default Constructor.
+ */
+ public FormSection() {
+
+ }
+
+ /**
+ * @param label, Not Null.
+ */
+ public void setLabel(String label) {
+ NotNull.exceptIfNull(label, "label");
+ label_ = label;
+ }
+
+ /**
+ * @return label, Not Null.
+ */
+ public String getLabel() {
+ return label_;
+ }
+
+ /**
+ * @param textElement, Not Null.
+ */
+ public void addTextElement(FormText textElement) {
+ NotNull.exceptIfNull(textElement, "textElement");
+ textElements_.add(textElement);
+ }
+
+ /**
+ * @return textElement, Not Null.
+ */
+ public Vector<FormText> getTextElements() {
+ return textElements_;
+ }
+
+ /**
+ * @param reportedRef, Not Null.
+ */
+ public void addReportedRef(FormReportedRef reportedRef) {
+ NotNull.exceptIfNull(reportedRef, "reportedRef");
+ reportedRefs_.add(reportedRef);
+ }
+
+ /**
+ * @return reportedRef, Not Null.
+ */
+ public Vector<FormReportedRef> getReportedRefs() {
+ return reportedRefs_;
+ }
+
+ /**
+ * @param childSection, Not Null.
+ */
+ public void addChildSection(FormSection childSection) {
+ NotNull.exceptIfNull(childSection, "childSection");
+ childSections_.add(childSection);
+ }
+
+ /**
+ * @return childSection, Not Null.
+ */
+ public Vector<FormSection> getChildSections() {
+ return childSections_;
+ }
+
+ /**
+ * @param field, Not Null.
+ */
+ public void addField(FormField field) {
+ NotNull.exceptIfNull(field, "field");
+ fields_.add(field);
+ }
+
+ /**
+ * @return field, Not Null.
+ */
+ public Vector<FormField> getFields() {
+ return fields_;
+ }
+
+ /**
+ * @param ref, Not Null.
+ */
+ public void addFieldRef(String ref) {
+ NotNull.exceptIfNull(ref, "ref");
+ fieldRefs_.add(ref);
+ }
+
+ /**
+ * @return ref, Not Null.
+ */
+ public Vector<String> getFieldRefs() {
+ return fieldRefs_;
+ }
+} \ No newline at end of file
diff --git a/src/com/isode/stroke/elements/FormText.java b/src/com/isode/stroke/elements/FormText.java
new file mode 100644
index 0000000..4a400b3
--- /dev/null
+++ b/src/com/isode/stroke/elements/FormText.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2015 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.elements;
+
+import com.isode.stroke.base.NotNull;
+
+public class FormText {
+
+ private String text_ = "";
+
+ /**
+ * Default Constructor.
+ */
+ public FormText() {
+
+ }
+
+ /**
+ * @param text, Not Null.
+ */
+ public void setTextString(String text) {
+ NotNull.exceptIfNull(text, "text");
+ text_ = text;
+ }
+
+ /**
+ * @return text, Not Null.
+ */
+ public String getTextString() {
+ return text_;
+ }
+} \ No newline at end of file
diff --git a/src/com/isode/stroke/elements/SearchPayload.java b/src/com/isode/stroke/elements/SearchPayload.java
index 1568961..8b85841 100644
--- a/src/com/isode/stroke/elements/SearchPayload.java
+++ b/src/com/isode/stroke/elements/SearchPayload.java
@@ -9,6 +9,7 @@
package com.isode.stroke.elements;
import com.isode.stroke.jid.JID;
+import com.isode.stroke.elements.Form;
import java.util.ArrayList;
import java.util.List;
@@ -26,8 +27,20 @@ public class SearchPayload extends Payload {
public SearchPayload() {
}
- //Form::ref getForm() const { return form; } /* Not ported yet */
- //void setForm(Form::ref f) { form = f; } /* Not ported yet */
+ /**
+ * @return Can be null
+ */
+ public Form getForm() {
+ return form;
+ }
+
+ /**
+ * @param v Null means no value.
+ */
+ public void setForm(Form f) {
+ form = f;
+ }
+
/**
* @return Can be null
*/
@@ -121,4 +134,5 @@ public class SearchPayload extends Payload {
private String last;
private String email;
private ArrayList<Item> items = new ArrayList<Item>();
+ private Form form;
}
diff --git a/src/com/isode/stroke/elements/Version.java b/src/com/isode/stroke/elements/Version.java
new file mode 100644
index 0000000..02c6507
--- /dev/null
+++ b/src/com/isode/stroke/elements/Version.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2010-2015 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.elements;
+
+import com.isode.stroke.elements.Payload;
+import com.isode.stroke.base.NotNull;
+
+public class Version extends Payload {
+
+ private String name_;
+ private String version_;
+ private String os_;
+
+ /**
+ * Default Constructor.
+ */
+ public Version() {
+ this("", "", "");
+ }
+
+ /**
+ * Parameterized Constructor.
+ * @param name, Not Null.
+ */
+ public Version(String name) {
+ this(name, "", "");
+ }
+
+ /**
+ * Parameterized Constructor.
+ * @param name, Not Null.
+ * @param version, NotNull.
+ */
+ public Version(String name, String version) {
+ this(name, version, "");
+ }
+
+ /**
+ * Parameterized Constructor.
+ * @param name, Not Null.
+ * @param version, NotNull.
+ * @param os , Not Null.
+ */
+ public Version(String name, String version, String os) {
+ NotNull.exceptIfNull(name, "name");
+ NotNull.exceptIfNull(version, "version");
+ NotNull.exceptIfNull(os, "os");
+ this.name_ = name;
+ this.version_ = version;
+ this.os_ = os;
+ }
+
+ /**
+ * @return name, Not Null.
+ */
+ public String getName() {
+ return name_;
+ }
+
+ /**
+ * @return version, Not Null.
+ */
+ public String getVersion() {
+ return version_;
+ }
+
+ /**
+ * @return os, Not Null.
+ */
+ public String getOS() {
+ return os_;
+ }
+} \ No newline at end of file
diff --git a/src/com/isode/stroke/parser/payloadparsers/FormParser.java b/src/com/isode/stroke/parser/payloadparsers/FormParser.java
index 2aa398c..ab14a4e 100644
--- a/src/com/isode/stroke/parser/payloadparsers/FormParser.java
+++ b/src/com/isode/stroke/parser/payloadparsers/FormParser.java
@@ -17,6 +17,10 @@ import com.isode.stroke.elements.Form.Type;
import com.isode.stroke.elements.FormField;
import com.isode.stroke.elements.FormField.Option;
import com.isode.stroke.elements.FormItem;
+import com.isode.stroke.elements.FormText;
+import com.isode.stroke.elements.FormReportedRef;
+import com.isode.stroke.elements.FormPage;
+import com.isode.stroke.elements.FormSection;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.parser.GenericPayloadParser;
@@ -32,7 +36,17 @@ public class FormParser extends GenericPayloadParser<Form> {
private boolean parsingOption_ = false;
private String currentOptionValue_ = "";
private String currentText_ = "";
-
+ private String currentFieldRef_ = "";
+ private boolean parsingItem_ = false;
+ private boolean parseStarted_ = false;
+ private boolean hasReportedRef_ = false;
+ private FormText currentTextElement_;
+ private FormReportedRef currentReportedRef_;
+ private FormPage currentPage_;
+ private FormSection currentSection_;
+ private List<FormPage> currentPages_ = new ArrayList<FormPage>();
+ private List<FormSection> sectionStack_ = new ArrayList<FormSection>();
+ private List<FormSection> currentSections_ = new ArrayList<FormSection>();
private static final int TopLevel = 0;
private static final int PayloadLevel = 1;
private static final int FieldLevel = 2;
@@ -81,12 +95,16 @@ public class FormParser extends GenericPayloadParser<Form> {
}
else if (element.equals(Form.FORM_ELEMENT_ITEM)) {
+ parsingItem_ = true;
currentItem_ = new FormItem();
}
-
+ else if (element == "page") {
+ currentPage_ = new FormPage();
+ currentPage_.setLabel(attributes.getAttribute("label"));
+ }
}
- if (level_ == FieldLevel && currentField_ != null) {
+ else if (level_ == FieldLevel && currentField_ != null) {
currentText_ = "";
if (element.equals(FormField.FORM_FIELD_ELEMENT_OPTION)) {
currentOptionLabel_ = attributes
@@ -115,7 +133,30 @@ public class FormParser extends GenericPayloadParser<Form> {
currentText_ = "";
}
}
-
+ if (level_ > PayloadLevel) {
+ if (element.equals("section")) {
+ currentSection_ = new FormSection();
+ currentSection_.setLabel(attributes.getAttribute("label"));
+ sectionStack_.add(currentSection_);
+ currentSections_.add(currentSection_);
+ }
+ if (element.equals("reportedref")) {
+ currentReportedRef_ = new FormReportedRef();
+ }
+ if (element.equals("fieldref")) {
+ currentText_ = "";
+ currentFieldRef_ = attributes.getAttribute("var");
+ if (sectionStack_.size() > 0) {
+ sectionStack_.get(sectionStack_.size()-1).addFieldRef(currentFieldRef_);
+ } else if (currentPage_ != null) {
+ currentPage_.addFieldRef(currentFieldRef_);
+ }
+ }
+ if (element.equals("text")) {
+ currentText_ = "";
+ currentTextElement_ = new FormText();
+ }
+ }
++level_;
}
@@ -156,44 +197,104 @@ public class FormParser extends GenericPayloadParser<Form> {
}
else if (element.equals(Form.FORM_ELEMENT_ITEM)) {
+ parsingItem_ = false;
currentItem_.addItemFields(currentFields_);
form.addItem(currentItem_);
currentFields_.clear();
currentItem_ = null;
}
+ else if (element.equals("page")) {
+ getPayloadInternal().addPage(currentPage_);
+ currentPages_.add(currentPage_);
+ }
}
-
- if (element.equals(FormField.FORM_FIELD_ELEMENT_REQUIRED)) {
- currentField_.setRequired(true);
- }
- else if (element.equals(FormField.FORM_FIELD_ELEMENT_DESC)) {
- currentField_.setDescription(currentText_);
- }
- else if (element.equals(FormField.FORM_FIELD_ELEMENT_OPTION)) {
- currentField_.addOption(
- new Option(currentOptionLabel_, currentOptionValue_));
- parsingOption_ = false;
- }
- else if (element.equals(FormField.FORM_FIELD_ELEMENT_VALUE)) {
- if (parsingOption_) {
- currentOptionValue_ = currentText_;
- }
- else {
- currentField_.addValue(currentText_);
- }
+
+ else if (currentField_ != null) {
+ if (element.equals(FormField.FORM_FIELD_ELEMENT_REQUIRED)) {
+ currentField_.setRequired(true);
+ }
+ else if (element.equals(FormField.FORM_FIELD_ELEMENT_DESC)) {
+ currentField_.setDescription(currentText_);
+ }
+ else if (element.equals(FormField.FORM_FIELD_ELEMENT_OPTION)) {
+ currentField_.addOption(
+ new Option(currentOptionLabel_, currentOptionValue_));
+ parsingOption_ = false;
+ }
+ else if (element.equals(FormField.FORM_FIELD_ELEMENT_VALUE)) {
+ if (parsingOption_) {
+ currentOptionValue_ = currentText_;
+ }
+ else {
+ currentField_.addValue(currentText_);
+ }
+ }
}
if (level_ >= PayloadLevel && currentField_ != null) {
- if (element.equals("field")) {
- if (parsingReported_) {
- form.addReportedField(currentField_);
- } else if (currentItem_ != null) {
- currentFields_.add(currentField_);
- } else {
- form.addField(currentField_);
+ if (element.equals("field")) {
+ if (parsingReported_) {
+ getPayloadInternal().addReportedField(currentField_);
+ }
+ else if (parsingItem_) {
+ currentFields_.add(currentField_);
+ }
+ else {
+ if (currentPages_.size() > 0) {
+ for (FormPage page : currentPages_) {
+ for (String pRef : page.getFieldRefs()) {
+ if (pRef.equals(currentField_.getName())) {
+ page.addField(currentField_);
+ }
+ }
+ }
+ for (FormSection section : currentSections_) {
+ for (String sRef : section.getFieldRefs()) {
+ if (sRef.equals(currentField_.getName())) {
+ section.addField(currentField_);
+ }
+ }
+ }
+ } else {
+ form.addField(currentField_);
+ }
}
currentField_ = null;
- }
+ }
+ }
+ if (level_ > PayloadLevel) {
+ if (element.equals("section")) {
+ if (sectionStack_.size() > 1) {
+ // Add the section at the top of the stack to the level below
+ sectionStack_.get(sectionStack_.size()-2).addChildSection(sectionStack_.get(sectionStack_.size()-1));
+ sectionStack_.remove(sectionStack_.size()-1);
+ }
+ else if (sectionStack_.size() == 1) {
+ // Add the remaining section on the stack to it's parent page
+ currentPage_.addChildSection(sectionStack_.get(sectionStack_.size()-1));
+ sectionStack_.remove(sectionStack_.size()-1);
+ }
+ }
+ if (currentReportedRef_ != null && !hasReportedRef_) {
+ if (sectionStack_.size() > 0) {
+ sectionStack_.get(sectionStack_.size()-1).addReportedRef(currentReportedRef_);
+ } else if (currentPage_ != null) {
+ currentPage_.addReportedRef(currentReportedRef_);
+ }
+ hasReportedRef_ = true;
+ currentReportedRef_ = null;
+ }
+ if (currentTextElement_ != null) {
+ if (element.equals("text")) {
+ currentTextElement_.setTextString(currentText_);
+ }
+ if (sectionStack_.size() > 0) {
+ sectionStack_.get(sectionStack_.size()-1).addTextElement(currentTextElement_);
+ } else if (currentPage_ != null) {
+ currentPage_.addTextElement(currentTextElement_);
+ }
+ currentTextElement_ = null;
+ }
}
}
diff --git a/src/com/isode/stroke/parser/payloadparsers/SearchPayloadParser.java b/src/com/isode/stroke/parser/payloadparsers/SearchPayloadParser.java
index c7a53cf..a40551e 100644
--- a/src/com/isode/stroke/parser/payloadparsers/SearchPayloadParser.java
+++ b/src/com/isode/stroke/parser/payloadparsers/SearchPayloadParser.java
@@ -13,6 +13,8 @@ import com.isode.stroke.elements.SearchPayload;
import com.isode.stroke.jid.JID;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.parser.GenericPayloadParser;
+import com.isode.stroke.parser.payloadparsers.FormParserFactory;
+import com.isode.stroke.parser.payloadparsers.FormParser;
public class SearchPayloadParser extends GenericPayloadParser<SearchPayload> {
@@ -22,60 +24,60 @@ public class SearchPayloadParser extends GenericPayloadParser<SearchPayload> {
private int level = 0;
private String currentText = "";
- //private FormParserFactory formParserFactory = new FormParserFactory(); /* Not ported yet*/
- //private FormParser formParser;
- SearchPayload.Item currentItem;
+ private FormParserFactory formParserFactory = new FormParserFactory();
+ private FormParser formParser;
+ private SearchPayload.Item currentItem;
public SearchPayloadParser() {
super(new SearchPayload());
+ formParserFactory = new FormParserFactory();
}
public void handleStartElement(String element, String ns, AttributeMap attributes) {
if (level == TopLevel) {
- }
- else if (level == PayloadLevel) {
- //if (element.equals("x") && ns.equals("jabber:x:data")) {
- // assert formParser == null;
- // formParser = dynamic_cast<FormParser*>(formParserFactory->createPayloadParser());
- //} /* Not ported yet */
- //else
- if (element.equals("item")) {
- assert currentItem == null;
- currentItem = new SearchPayload.Item();
- currentItem.jid = JID.fromString(attributes.getAttribute("jid"));
+
}
- else {
+ else if (level == PayloadLevel) {
+ if (element.equals("x") && ns.equals("jabber:x:data")) {
+ assert (formParser == null);
+ formParser = (FormParser)(formParserFactory.createPayloadParser());
+ }
+ else
+ if (element.equals("item")) {
+ assert currentItem == null;
+ currentItem = new SearchPayload.Item();
+ currentItem.jid = JID.fromString(attributes.getAttribute("jid"));
+ }
+ else {
+ currentText = "";
+ }
+ }
+ else if (level == ItemLevel && currentItem != null) {
currentText = "";
}
- }
- else if (level == ItemLevel && currentItem != null) {
- currentText = "";
- }
- //if (formParser) {
- // formParser->handleStartElement(element, ns, attributes);
- //} /* Not ported yet */
+ if (formParser != null) {
+ formParser.handleStartElement(element, ns, attributes);
+ }
- ++level;
+ ++level;
}
public void handleEndElement(String element, String ns) {
--level;
- //if (formParser) {
- // formParser->handleEndElement(element, ns);
- //} /*Not Ported yet*/
+ if (formParser != null) {
+ formParser.handleEndElement(element, ns);
+ }
if (level == TopLevel) {
}
else if (level == PayloadLevel) {
- //if (formParser) {
- // getPayloadInternal()->setForm(formParser->getPayloadInternal());
- // delete formParser;
- // formParser = NULL;
- //}
- //else /*Not ported yet*/
- if (element.equals("item")) {
+ if (formParser != null) {
+ getPayloadInternal().setForm(formParser.getPayloadInternal());
+ formParser = null;
+ }
+ else if (element.equals("item")) {
assert currentItem != null;
getPayloadInternal().addItem(currentItem);
currentItem = null;
@@ -113,12 +115,12 @@ public class SearchPayloadParser extends GenericPayloadParser<SearchPayload> {
}
public void handleCharacterData(String data) {
- //if (formParser) {
- // formParser->handleCharacterData(data);
- //}
- //else { /*Not ported yet*/
+ if (formParser != null) {
+ formParser.handleCharacterData(data);
+ }
+ else {
currentText += data;
- //}
+ }
}
}
diff --git a/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java
index 79153af..262ba3d 100644
--- a/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java
+++ b/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java
@@ -12,15 +12,22 @@ package com.isode.stroke.serializer.payloadserializers;
import com.isode.stroke.elements.Form;
import com.isode.stroke.elements.FormField;
import com.isode.stroke.elements.FormItem;
+import com.isode.stroke.elements.FormSection;
+import com.isode.stroke.elements.FormText;
+import com.isode.stroke.elements.FormPage;
+import com.isode.stroke.elements.FormReportedRef;
import com.isode.stroke.serializer.GenericPayloadSerializer;
import com.isode.stroke.serializer.xml.XMLElement;
import com.isode.stroke.serializer.xml.XMLTextNode;
+import java.util.Vector;
/**
* Serializer for {@link Form} element.
*/
public class FormSerializer extends GenericPayloadSerializer<Form> {
-
+
+ private Vector<FormField> fields_ = new Vector<FormField>();
+
public FormSerializer() {
super(Form.class);
}
@@ -42,6 +49,14 @@ public class FormSerializer extends GenericPayloadSerializer<Form> {
}
// Add reported element
+
+ for(FormPage page : form.getPages()) {
+ formElement.addNode(pageToXML(page));
+ }
+
+ for(FormField field : form.getFields()) {
+ formElement.addNode(fieldToXML(field, true));
+ }
if (!form.getReportedFields().isEmpty()) {
XMLElement reportedElement = new XMLElement("reported");
@@ -59,12 +74,15 @@ public class FormSerializer extends GenericPayloadSerializer<Form> {
}
formElement.addNode(itemElement);
}
-
- // Add fields
- for (FormField field : form.getFields()) {
- formElement.addNode(fieldToXML(field, true));
+
+ for(FormText text : form.getTextElements()) {
+ formElement.addNode(textToXML(text));
}
+ for(FormField field : fields_) {
+ formElement.addNode(fieldToXML(field,true));
+ }
+
return formElement.serialize();
}
@@ -117,6 +135,65 @@ public class FormSerializer extends GenericPayloadSerializer<Form> {
return fieldElement;
}
+ private XMLElement textToXML(FormText text) {
+ XMLElement textElement = new XMLElement("text");
+ textElement.addNode(new XMLTextNode(text.getTextString()));
+ return textElement;
+ }
+
+ private XMLElement fieldRefToXML(String ref) {
+ XMLElement fieldRefElement = new XMLElement("fieldref");
+ fieldRefElement.setAttribute("var", ref);
+ return fieldRefElement;
+ }
+
+ /*private XMLElement reportedRefToXML(FormReportedRef reportedRef) {
+
+ }*/
+
+ private XMLElement pageToXML(FormPage page) {
+ XMLElement pageElement = new XMLElement("page");
+ pageElement.setAttribute("xmlns", page.getXMLNS());
+ if (!page.getLabel().isEmpty()) {
+ pageElement.setAttribute("label", page.getLabel());
+ }
+ for(FormText text : page.getTextElements()) {
+ pageElement.addNode(textToXML(text));
+ }
+ for (FormField field : page.getFields()) {
+ pageElement.addNode(fieldRefToXML(field.getName()));
+ fields_.add(field);
+ }
+ for(FormReportedRef reportedRef : page.getReportedRefs()) {
+ pageElement.addNode(new XMLElement("reportedref"));
+ }
+ for(FormSection section : page.getChildSections()) {
+ pageElement.addNode(sectionToXML(section));
+ }
+ return pageElement;
+ }
+
+ private XMLElement sectionToXML(FormSection section) {
+ XMLElement sectionElement = new XMLElement("section");
+ if (!section.getLabel().isEmpty()) {
+ sectionElement.setAttribute("label", section.getLabel());
+ }
+ for(FormText text : section.getTextElements()) {
+ sectionElement.addNode(textToXML(text));
+ }
+ for(FormField field : section.getFields()) {
+ sectionElement.addNode(fieldRefToXML(field.getName()));
+ fields_.add(field);
+ }
+ for(FormReportedRef reportedRef : section.getReportedRefs()) {
+ sectionElement.addNode(new XMLElement("reportedref"));
+ }
+ for(FormSection childSection : section.getChildSections()) {
+ sectionElement.addNode(sectionToXML(childSection));
+ }
+ return sectionElement;
+ }
+
private void multiLineify(String text, String elementName,
XMLElement element) {
if (text == null) {
diff --git a/src/com/isode/stroke/serializer/payloadserializers/SearchPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/SearchPayloadSerializer.java
index 3edfe23..da87b5c 100644
--- a/src/com/isode/stroke/serializer/payloadserializers/SearchPayloadSerializer.java
+++ b/src/com/isode/stroke/serializer/payloadserializers/SearchPayloadSerializer.java
@@ -7,6 +7,8 @@ package com.isode.stroke.serializer.payloadserializers;
import com.isode.stroke.elements.SearchPayload;
import com.isode.stroke.serializer.GenericPayloadSerializer;
import com.isode.stroke.serializer.xml.XMLElement;
+import com.isode.stroke.serializer.xml.XMLRawTextNode;
+import com.isode.stroke.elements.Form;
/**
* SearchPayload to String.
@@ -52,9 +54,10 @@ public class SearchPayloadSerializer extends GenericPayloadSerializer<SearchPayl
searchElement.addNode(itemElement);
}
- //if (Form::ref form = searchPayload->getForm()) {
- // searchElement.addNode(boost::shared_ptr<XMLRawTextNode>(new XMLRawTextNode(FormSerializer().serialize(form))));
- //} /* Not ported yet. When the time comes, look at Swiften to check if it's changed. It will have. */
+ if (searchPayload.getForm() != null) {
+ Form form = searchPayload.getForm();
+ searchElement.addNode(new XMLRawTextNode(new FormSerializer().serialize(form)));
+ }
return searchElement.serialize();
}