1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-12-12 14:01:08 +01:00

Properly escape Bookmarks and FormField XML

by using XmlStringBuilder. Fixes SMACK-577

Also extend LazyStringBuilder with a cache. And extend XmlStringBuilder
with some more convenience methods.

Move the ELEMENT and NAMESPACE definition from Form to DataForm, where
it belongs.
This commit is contained in:
Florian Schmaus 2014-06-19 11:48:01 +02:00
parent 2ce7656180
commit 26b5bc0212
9 changed files with 140 additions and 95 deletions

View file

@ -49,9 +49,6 @@ public class Form {
public static final String TYPE_CANCEL = "cancel";
public static final String TYPE_RESULT = "result";
public static final String NAMESPACE = "jabber:x:data";
public static final String ELEMENT = "x";
private DataForm dataForm;
/**

View file

@ -17,7 +17,6 @@
package org.jivesoftware.smackx.xdata;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import java.util.ArrayList;
@ -33,6 +32,8 @@ import java.util.List;
*/
public class FormField {
public static final String ELEMENT = "field";
public static final String TYPE_BOOLEAN = "boolean";
public static final String TYPE_FIXED = "fixed";
public static final String TYPE_HIDDEN = "hidden";
@ -263,25 +264,17 @@ public class FormField {
}
}
public String toXML() {
public XmlStringBuilder toXML() {
XmlStringBuilder buf = new XmlStringBuilder();
buf.append("<field");
buf.halfOpenElement(ELEMENT);
// Add attributes
if (getLabel() != null) {
buf.append(" label=\"").append(getLabel()).append("\"");
}
buf.attribute("var", getVariable());
if (getType() != null) {
buf.append(" type=\"").append(getType()).append("\"");
}
buf.append(">");
buf.optAttribute("label", getLabel());
buf.optAttribute("var", getVariable());
buf.optAttribute("type", getType());
buf.rightAngelBracket();
// Add elements
if (getDescription() != null) {
buf.append("<desc>").append(getDescription()).append("</desc>");
}
if (isRequired()) {
buf.append("<required/>");
}
buf.optElement("desc", getDescription());
buf.condEmptyElement(isRequired(), "required");
// Loop through all the values and append them to the string buffer
for (String value : getValues()) {
buf.element("value", value);
@ -290,8 +283,8 @@ public class FormField {
for (Option option : getOptions()) {
buf.append(option.toXML());
}
buf.append("</field>");
return buf.toString();
buf.closeElement(ELEMENT);
return buf;
}
@Override
@ -320,8 +313,10 @@ public class FormField {
*/
public static class Option {
public static final String ELEMNT = "option";
private final String value;
private String label;
private String value;
public Option(String value) {
this.value = value;
@ -355,19 +350,18 @@ public class FormField {
return getLabel();
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<option");
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(ELEMNT);
// Add attribute
if (getLabel() != null) {
buf.append(" label=\"").append(getLabel()).append("\"");
}
buf.append(">");
// Add element
buf.append("<value>").append(StringUtils.escapeForXML(getValue())).append("</value>");
xml.optAttribute("label", getLabel());
xml.rightAngelBracket();
buf.append("</option>");
return buf.toString();
// Add element
xml.element("value", getValue());
xml.closeElement(ELEMENT);
return xml;
}
@Override

View file

@ -18,7 +18,7 @@
package org.jivesoftware.smackx.xdata.packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.xdata.Form;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.xdata.FormField;
import java.util.ArrayList;
@ -33,6 +33,9 @@ import java.util.List;
*/
public class DataForm implements PacketExtension {
public static final String NAMESPACE = "jabber:x:data";
public static final String ELEMENT = "x";
private String type;
private String title;
private List<String> instructions = new ArrayList<String>();
@ -120,11 +123,11 @@ public class DataForm implements PacketExtension {
}
public String getElementName() {
return Form.ELEMENT;
return ELEMENT;
}
public String getNamespace() {
return Form.NAMESPACE;
return NAMESPACE;
}
/**
@ -207,15 +210,15 @@ public class DataForm implements PacketExtension {
return found;
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
"\" type=\"" + getType() +"\">");
if (getTitle() != null) {
buf.append("<title>").append(getTitle()).append("</title>");
}
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder buf = new XmlStringBuilder(this);
buf.attribute("type", getType());
buf.rightAngelBracket();
buf.optElement("title", getTitle());
for (String instruction : getInstructions()) {
buf.append("<instructions>").append(instruction).append("</instructions>");
buf.element("instructions", instruction);
}
// Append the list of fields returned from a search
if (getReportedData() != null) {
@ -229,8 +232,8 @@ public class DataForm implements PacketExtension {
for (FormField field : getFields()) {
buf.append(field.toXML());
}
buf.append("</").append(getElementName()).append(">");
return buf.toString();
buf.closeElement(this);
return buf;
}
/**
@ -241,6 +244,8 @@ public class DataForm implements PacketExtension {
* @author Gaston Dombiak
*/
public static class ReportedData {
public static final String ELEMENT = "reported";
private List<FormField> fields = new ArrayList<FormField>();
public ReportedData(List<FormField> fields) {
@ -256,15 +261,15 @@ public class DataForm implements PacketExtension {
return Collections.unmodifiableList(new ArrayList<FormField>(fields));
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<reported>");
public CharSequence toXML() {
XmlStringBuilder buf = new XmlStringBuilder();
buf.openElement(ELEMENT);
// Loop through all the form items and append them to the string buffer
for (FormField field : getFields()) {
buf.append(field.toXML());
}
buf.append("</reported>");
return buf.toString();
buf.closeElement(ELEMENT);
return buf;
}
}
@ -275,6 +280,8 @@ public class DataForm implements PacketExtension {
* @author Gaston Dombiak
*/
public static class Item {
public static final String ELEMENT = "item";
private List<FormField> fields = new ArrayList<FormField>();
public Item(List<FormField> fields) {
@ -290,15 +297,15 @@ public class DataForm implements PacketExtension {
return Collections.unmodifiableList(new ArrayList<FormField>(fields));
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<item>");
public CharSequence toXML() {
XmlStringBuilder buf = new XmlStringBuilder();
buf.openElement(ELEMENT);
// Loop through all the form items and append them to the string buffer
for (FormField field : getFields()) {
buf.append(field.toXML());
}
buf.append("</item>");
return buf.toString();
buf.closeElement(ELEMENT);
return buf;
}
}
}