1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-10 18:59:41 +02:00

Use XmlStringBuilder in most toXML() bodies

Also change StringUtils.escapeForXML() and Packet.toXML() to return
CharSequence instead of String. XmlStringBuilder now has 'optX' methods.

Remove XmlUtils in favor of XmlStringBuilder
This commit is contained in:
Florian Schmaus 2014-03-21 09:51:52 +01:00
parent 1cf4681581
commit 978f692eb0
47 changed files with 511 additions and 412 deletions

View file

@ -336,7 +336,7 @@ public class MultipleRecipientManager {
*/
private static class PacketCopy extends Packet {
private String text;
private CharSequence text;
/**
* Create a copy of a packet with the text to send. The passed text must be a valid text to
@ -344,11 +344,12 @@ public class MultipleRecipientManager {
*
* @param text the whole text of the packet to send
*/
public PacketCopy(String text) {
public PacketCopy(CharSequence text) {
this.text = text;
}
public String toXML() {
@Override
public CharSequence toXML() {
return text;
}

View file

@ -128,7 +128,7 @@ public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache
private static void writeInfoToFile(File file, DiscoverInfo info) throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
try {
dos.writeUTF(info.toXML());
dos.writeUTF(info.toXML().toString());
} finally {
dos.close();
}

View file

@ -17,6 +17,7 @@
package org.jivesoftware.smackx.caps.packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.caps.EntityCapsManager;
public class CapsExtension implements PacketExtension {
@ -56,10 +57,10 @@ public class CapsExtension implements PacketExtension {
* ver='QgayPKawpkPSDYmwT/WM94uAlu0='/>
*
*/
public String toXML() {
return "<" + EntityCapsManager.ELEMENT + " xmlns=\"" + EntityCapsManager.NAMESPACE + "\" " +
"hash=\"" + hash + "\" " +
"node=\"" + node + "\" " +
"ver=\"" + ver + "\"/>";
public CharSequence toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);
xml.attribute("hash", hash).attribute("node", node).attribute("ver", ver);
xml.closeEmptyElement();
return xml;
}
}

View file

@ -18,7 +18,7 @@
package org.jivesoftware.smackx.disco.packet;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import java.util.Collection;
import java.util.Collections;
@ -186,29 +186,27 @@ public class DiscoverInfo extends IQ {
return false;
}
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<query xmlns=\"" + NAMESPACE + "\"");
if (getNode() != null) {
buf.append(" node=\"");
buf.append(StringUtils.escapeForXML(getNode()));
buf.append("\"");
}
buf.append(">");
@Override
public CharSequence getChildElementXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement("query");
xml.xmlnsAttribute(NAMESPACE);
xml.optAttribute("node", getNode());
xml.rightAngelBracket();
synchronized (identities) {
for (Identity identity : identities) {
buf.append(identity.toXML());
xml.append(identity.toXML());
}
}
synchronized (features) {
for (Feature feature : features) {
buf.append(feature.toXML());
xml.append(feature.toXML());
}
}
// Add packet extensions, if any are defined.
buf.append(getExtensionsXML());
buf.append("</query>");
return buf.toString();
xml.append(getExtensionsXML());
xml.closeElement("query");
return xml;
}
/**
@ -358,22 +356,15 @@ public class DiscoverInfo extends IQ {
return lang;
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<identity");
// Check if this packet has 'lang' set and maybe append it to the resulting string
if (lang != null)
buf.append(" xml:lang=\"").append(StringUtils.escapeForXML(lang)).append("\"");
// Category must always be set
buf.append(" category=\"").append(StringUtils.escapeForXML(category)).append("\"");
// Name must always be set
buf.append(" name=\"").append(StringUtils.escapeForXML(name)).append("\"");
// Check if this packet has 'type' set and maybe append it to the resulting string
if (type != null) {
buf.append(" type=\"").append(StringUtils.escapeForXML(type)).append("\"");
}
buf.append("/>");
return buf.toString();
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement("identity");
xml.xmllangAttribute(lang);
xml.attribute("category", category);
xml.attribute("name", name);
xml.optAttribute("type", type);
xml.closeEmptyElement();
return xml;
}
/**
@ -488,10 +479,12 @@ public class DiscoverInfo extends IQ {
return variable;
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<feature var=\"").append(StringUtils.escapeForXML(variable)).append("\"/>");
return buf.toString();
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement("feature");
xml.attribute("var", variable);
xml.closeEmptyElement();
return xml;
}
public boolean equals(Object obj) {

View file

@ -18,7 +18,7 @@
package org.jivesoftware.smackx.disco.packet;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import java.util.Collection;
import java.util.Collections;
@ -102,22 +102,20 @@ public class DiscoverItems extends IQ {
this.node = node;
}
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<query xmlns=\"" + NAMESPACE + "\"");
if (getNode() != null) {
buf.append(" node=\"");
buf.append(StringUtils.escapeForXML(getNode()));
buf.append("\"");
}
buf.append(">");
public XmlStringBuilder getChildElementXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement("query");
xml.xmlnsAttribute(NAMESPACE);
xml.optAttribute("node", getNode());
xml.rightAngelBracket();
synchronized (items) {
for (Item item : items) {
buf.append(item.toXML());
xml.append(item.toXML());
}
}
buf.append("</query>");
return buf.toString();
xml.closeElement("query");
return xml;
}
/**
@ -231,20 +229,15 @@ public class DiscoverItems extends IQ {
this.action = action;
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<item jid=\"").append(entityID).append("\"");
if (name != null) {
buf.append(" name=\"").append(StringUtils.escapeForXML(name)).append("\"");
}
if (node != null) {
buf.append(" node=\"").append(StringUtils.escapeForXML(node)).append("\"");
}
if (action != null) {
buf.append(" action=\"").append(StringUtils.escapeForXML(action)).append("\"");
}
buf.append("/>");
return buf.toString();
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement("item");
xml.attribute("jid", entityID);
xml.optAttribute("name", name);
xml.optAttribute("node", node);
xml.optAttribute("action", action);
xml.closeEmptyElement();
return xml;
}
}
}

View file

@ -46,7 +46,7 @@ public class AffiliationsExtension extends NodeExtension
}
@Override
public String toXML()
public CharSequence toXML()
{
if ((items == null) || (items.size() == 0))
{

View file

@ -73,7 +73,7 @@ public class FormNode extends NodeExtension
}
@Override
public String toXML()
public CharSequence toXML()
{
if (configForm == null)
{

View file

@ -151,7 +151,7 @@ public class ItemsExtension extends NodeExtension implements EmbeddedPacketExten
}
@Override
public String toXML()
public CharSequence toXML()
{
if ((items == null) || (items.size() == 0))
{

View file

@ -75,7 +75,7 @@ public class NodeExtension implements PacketExtension
return element.getNamespace().getXmlns();
}
public String toXML()
public CharSequence toXML()
{
return '<' + getElementName() + (node == null ? "" : " node='" + node + '\'') + "/>";
}

View file

@ -16,7 +16,7 @@
*/
package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smack.util.XmlUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* A packet extension representing the <b>options</b> element.
@ -54,22 +54,15 @@ public class OptionsExtension extends NodeExtension
{
return id;
}
@Override
public String toXML()
{
StringBuilder builder = new StringBuilder("<");
builder.append(getElementName());
XmlUtils.appendAttribute(builder, "jid", jid);
if (getNode() != null)
XmlUtils.appendAttribute(builder, "node", getNode());
if (id != null)
XmlUtils.appendAttribute(builder, "subid", id);
builder.append("/>");
return builder.toString();
}
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(getElementName());
xml.attribute("jid", jid);
xml.optAttribute("node", getNode());
xml.optAttribute("subid", id);
xml.closeEmptyElement();
return xml;
}
}

View file

@ -66,7 +66,7 @@ public class SubscriptionsExtension extends NodeExtension
}
@Override
public String toXML()
public CharSequence toXML()
{
if ((items == null) || (items.size() == 0))
{

View file

@ -16,7 +16,7 @@
*/
package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smack.util.XmlUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
@ -55,22 +55,15 @@ public class UnsubscribeExtension extends NodeExtension
{
return id;
}
@Override
public String toXML()
{
StringBuilder builder = new StringBuilder("<");
builder.append(getElementName());
XmlUtils.appendAttribute(builder, "jid", jid);
if (getNode() != null)
XmlUtils.appendAttribute(builder, "node", getNode());
if (id != null)
XmlUtils.appendAttribute(builder, "subid", id);
builder.append("/>");
return builder.toString();
}
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(getElementName());
xml.attribute("jid", jid);
xml.optAttribute("node", getNode());
xml.optAttribute("subid", id);
xml.closeEmptyElement();
return xml;
}
}

View file

@ -844,11 +844,11 @@ public class VCard extends IQ {
appendTag(tag, null, null, hasContent, builder);
}
private void appendTag(String tag, final String tagText) {
private void appendTag(String tag, final CharSequence tagText) {
if (tagText == null) return;
final ContentBuilder contentBuilder = new ContentBuilder() {
public void addTagContent() {
sb.append(tagText.trim());
sb.append(tagText.toString().trim());
}
};
appendTag(tag, true, contentBuilder);

View file

@ -18,6 +18,7 @@
package org.jivesoftware.smackx.xdata;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import java.util.ArrayList;
import java.util.Collections;
@ -264,15 +265,13 @@ public class FormField {
}
public String toXML() {
StringBuilder buf = new StringBuilder();
XmlStringBuilder buf = new XmlStringBuilder();
buf.append("<field");
// Add attributes
if (getLabel() != null) {
buf.append(" label=\"").append(getLabel()).append("\"");
}
if (getVariable() != null) {
buf.append(" var=\"").append(getVariable()).append("\"");
}
buf.attribute("var", getVariable());
if (getType() != null) {
buf.append(" type=\"").append(getType()).append("\"");
}
@ -286,7 +285,7 @@ public class FormField {
}
// Loop through all the values and append them to the string buffer
for (Iterator<String> i = getValues(); i.hasNext();) {
buf.append("<value>").append(i.next()).append("</value>");
buf.element("value", i.next());
}
// Loop through all the values and append them to the string buffer
for (Iterator<Option> i = getOptions(); i.hasNext();) {

View file

@ -78,7 +78,7 @@ public class CloseTest {
close.setTo("juliet@capulet.lit/balcony");
close.setPacketID("us71g45j");
assertXMLEqual(control, close.toXML());
assertXMLEqual(control, close.toXML().toString());
}
}

View file

@ -83,7 +83,7 @@ public class DataTest {
data.setTo("juliet@capulet.lit/balcony");
data.setPacketID("kr91n475");
assertXMLEqual(control, data.toXML());
assertXMLEqual(control, data.toXML().toString());
}
}

View file

@ -100,7 +100,7 @@ public class OpenTest {
open.setTo("juliet@capulet.lit/balcony");
open.setPacketID("jn3h8g65");
assertXMLEqual(control, open.toXML());
assertXMLEqual(control, open.toXML().toString());
}

View file

@ -101,7 +101,7 @@ public class EntityCapsManagerTest {
DiscoverInfo restored_di = EntityCapsManager.getDiscoveryInfoByNodeVer(nodeVer);
assertNotNull(restored_di);
assertEquals(di.toXML(), restored_di.toXML());
assertEquals(di.toXML().toString(), restored_di.toXML().toString());
}
private static DiscoverInfo createComplexSamplePacket() {

View file

@ -50,6 +50,7 @@ public class FileTransferNegotiatorTest {
FileTransferNegotiator fileNeg = FileTransferNegotiator.getInstanceFor(connection);
fileNeg.negotiateOutgoingTransfer("me", "streamid", "file", 1024, null, 10);
Packet packet = connection.getSentPacket();
assertTrue(packet.toXML().indexOf("\"stream-method\" type=\"list-single\"") != -1);
String xml = packet.toXML().toString();
assertTrue(xml.indexOf("var='stream-method' type=\"list-single\"") != -1);
}
}

View file

@ -135,10 +135,10 @@ public class Protocol {
if (printProtocol) {
System.out.println("------------------- Request -------------\n");
System.out.println(prettyFormat(request.toXML()));
System.out.println(prettyFormat(request.toXML().toString()));
System.out.println("------------------- Response ------------\n");
if (response != null) {
System.out.println(prettyFormat(response.toXML()));
System.out.println(prettyFormat(response.toXML().toString()));
}
else {
System.out.println("No response");