From 33648cee9778a0ce73fa17f0bba4069896615f95 Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Tue, 28 Aug 2012 17:42:54 +0100 Subject: Improve efficiency of FormParser diff --git a/src/com/isode/stroke/parser/payloadparsers/FormParser.java b/src/com/isode/stroke/parser/payloadparsers/FormParser.java index 0b72e6b..86852e6 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FormParser.java +++ b/src/com/isode/stroke/parser/payloadparsers/FormParser.java @@ -29,6 +29,7 @@ import com.isode.stroke.elements.FormField.TextSingleFormField; import com.isode.stroke.jid.JID; import com.isode.stroke.parser.AttributeMap; import com.isode.stroke.parser.GenericPayloadParser; +import java.util.ArrayList; /** * Parser for {@link Form} element. @@ -211,7 +212,8 @@ public class FormParser extends GenericPayloadParser
{ private static final int FieldLevel = 2; private int level_; - private String currentText_ = ""; + //private String currentText_ = ""; + private ArrayList texts_ = new ArrayList(); private String currentOptionLabel_ = ""; private FieldParseHelper currentFieldParseHelper_ = null; @@ -243,9 +245,9 @@ public class FormParser extends GenericPayloadParser { form.setType(Type.getType(type)); } else if (level_ == PayloadLevel) { if (element.equals(Form.FORM_ELEMENT_TITLE)) { - currentText_ = ""; + texts_ = new ArrayList(); } else if (element.equals(Form.FORM_ELEMENT_INSTRUCTIONS)) { - currentText_ = ""; + texts_ = new ArrayList(); } else if (element.equals(Form.FORM_ELEMENT_FIELD)) { String type = attributes .getAttribute(FormField.FORM_FIELD_ATTRIBUTE_TYPE); @@ -299,7 +301,7 @@ public class FormParser extends GenericPayloadParser { } } } else if ((level_ == FieldLevel) && (currentFieldParseHelper_ != null)) { - currentText_ = ""; + texts_ = new ArrayList(); if (element.equals(FormField.FORM_FIELD_ELEMENT_OPTION)) { currentOptionLabel_ = attributes .getAttribute(FormField.FORM_FIELD_ATTRIBUTE_OPTION_LABEL); @@ -324,17 +326,17 @@ public class FormParser extends GenericPayloadParser { if (element.equals(Form.FORM_ELEMENT_TITLE)) { String currentTitle = form.getTitle(); if (currentTitle.isEmpty()) { - form.setTitle(currentText_); + form.setTitle(getCurrentText()); } else { - form.setTitle(currentTitle + "\n" + currentText_); + form.setTitle(currentTitle + "\n" + getCurrentText()); } } else if (element.equals(Form.FORM_ELEMENT_INSTRUCTIONS)) { String currentInstructions = form.getInstructions(); if (currentInstructions.isEmpty()) { - form.setInstructions(currentText_); + form.setInstructions(getCurrentText()); } else { form.setInstructions(currentInstructions + "\n" - + currentText_); + + getCurrentText()); } } else if (element.equals(Form.FORM_ELEMENT_FIELD)) { if (currentFieldParseHelper_ != null) { @@ -347,28 +349,40 @@ public class FormParser extends GenericPayloadParser { currentFieldParseHelper_.getField().setRequired(true); } else if (element.equals(FormField.FORM_FIELD_ELEMENT_DESC)) { currentFieldParseHelper_.getField() - .setDescription(currentText_); + .setDescription(getCurrentText()); } else if (element.equals(FormField.FORM_FIELD_ELEMENT_OPTION)) { currentFieldParseHelper_.getField().addOption( - new Option(currentOptionLabel_, currentText_)); + new Option(currentOptionLabel_, getCurrentText())); } else if (element.equals(FormField.FORM_FIELD_ELEMENT_VALUE)) { - currentFieldParseHelper_.addValue(currentText_); + currentFieldParseHelper_.addValue(getCurrentText()); } } } + private String getCurrentText() { + int size = 0; + for (String text : texts_) { + size += text.length(); + } + StringBuilder builder = new StringBuilder(size); + for (String text : texts_) { + builder.append(text); + } + return builder.toString(); + } + public void handleCharacterData(String text) { if (text == null) { throw new NullPointerException("'text' must not be null"); } - currentText_ += text; + texts_.add(text); } @Override public String toString() { return FormParser.class.getSimpleName() + "\nlevel: " + level_ - + "\ncurrent text: " + currentText_ + + "\ncurrent text: " + getCurrentText() + "\ncurrent option label: " + currentOptionLabel_; } } -- cgit v0.10.2-6-g49f6