diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-06-20 01:02:12 (GMT) |
---|---|---|
committer | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-06-27 07:12:55 (GMT) |
commit | a673d269487fd86efe7f9c5f9b4cd1c00cab556d (patch) | |
tree | 10145697d3153a8a170361c5355ad9bccee21776 /src/com/isode/stroke/parser | |
parent | 27212e007077418d18014286a46723fa26693864 (diff) | |
download | stroke-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/com/isode/stroke/parser')
-rw-r--r-- | src/com/isode/stroke/parser/payloadparsers/FormParser.java | 163 | ||||
-rw-r--r-- | src/com/isode/stroke/parser/payloadparsers/SearchPayloadParser.java | 78 |
2 files changed, 172 insertions, 69 deletions
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; - //} + } } } |