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

@ -338,7 +338,7 @@ public class BOSHConnection extends XMPPConnection {
void sendPacketInternal(Packet packet) {
if (!done) {
try {
send(ComposableBody.builder().setPayloadXML(packet.toXML())
send(ComposableBody.builder().setPayloadXML(packet.toXML().toString())
.build());
} catch (BOSHException e) {
LOGGER.log(Level.SEVERE, "BOSHException in sendPacketInternal", e);
@ -391,7 +391,7 @@ public class BOSHConnection extends XMPPConnection {
try {
client.disconnect(ComposableBody.builder()
.setNamespaceDefinition("xmpp", XMPP_BOSH_NS)
.setPayloadXML(unavailablePresence.toXML())
.setPayloadXML(unavailablePresence.toXML().toString())
.build());
// Wait 150 ms for processes to clean-up, then shutdown.
Thread.sleep(150);

View File

@ -291,9 +291,9 @@ public class DirectoryRosterStore implements RosterStore {
XmlStringBuilder xml = new XmlStringBuilder();
xml.openElement("item");
xml.element("user", item.getUser());
xml.element("name", item.getName());
xml.element("type", item.getItemType());
xml.element("status", item.getItemStatus());
xml.optElement("name", item.getName());
xml.optElement("type", item.getItemType());
xml.optElement("status", item.getItemStatus());
for (String groupName : item.getGroupNames()) {
xml.openElement("group");
xml.element("groupName", groupName);

View File

@ -241,8 +241,8 @@ public class LiteDebugger implements SmackDebugger {
// data as Smack sees it and not as it's coming in as raw XML.
listener = new PacketListener() {
public void processPacket(Packet packet) {
interpretedText1.append(packet.toXML());
interpretedText2.append(packet.toXML());
interpretedText1.append(packet.toXML().toString());
interpretedText2.append(packet.toXML().toString());
interpretedText1.append(NEWLINE);
interpretedText2.append(NEWLINE);
}

View File

@ -17,6 +17,8 @@
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* IQ packet used by Smack to bind a resource and to obtain the jid assigned by the server.
* There are two ways to bind a resource. One is simply sending an empty Bind packet where the
@ -53,16 +55,16 @@ public class Bind extends IQ {
this.jid = jid;
}
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
if (resource != null) {
buf.append("<resource>").append(resource).append("</resource>");
}
if (jid != null) {
buf.append("<jid>").append(jid).append("</jid>");
}
buf.append("</bind>");
return buf.toString();
@Override
public CharSequence getChildElementXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement("bind");
xml.xmlnsAttribute("urn:ietf:params:xml:ns:xmpp-bind");
xml.rightAngelBracket();
xml.optElement("resource", resource);
xml.optElement("jid", jid);
xml.closeElement("bind");
return xml;
}
}

View File

@ -19,6 +19,8 @@ package org.jivesoftware.smack.packet;
import java.util.*;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* Default implementation of the PacketExtension interface. Unless a PacketExtensionProvider
* is registered with {@link org.jivesoftware.smack.provider.ProviderManager ProviderManager},
@ -76,17 +78,16 @@ public class DefaultPacketExtension implements PacketExtension {
return namespace;
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append(elementName).append(" xmlns=\"").append(namespace).append("\">");
@Override
public CharSequence toXML() {
XmlStringBuilder buf = new XmlStringBuilder();
buf.halfOpenElement(elementName).xmlnsAttribute(namespace).rightAngelBracket();
for (String name : getNames()) {
String value = getValue(name);
buf.append("<").append(name).append(">");
buf.append(value);
buf.append("</").append(name).append(">");
buf.element(name, value);
}
buf.append("</").append(elementName).append(">");
return buf.toString();
buf.closeElement(elementName);
return buf;
}
/**

View File

@ -17,7 +17,7 @@
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* The base IQ (Info/Query) packet. IQ packets are used to get and set information
@ -71,36 +71,27 @@ public abstract class IQ extends Packet {
}
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<iq ");
if (getPacketID() != null) {
buf.append("id=\"" + getPacketID() + "\" ");
}
if (getTo() != null) {
buf.append("to=\"").append(StringUtils.escapeForXML(getTo())).append("\" ");
}
if (getFrom() != null) {
buf.append("from=\"").append(StringUtils.escapeForXML(getFrom())).append("\" ");
}
@Override
public CharSequence toXML() {
XmlStringBuilder buf = new XmlStringBuilder();
buf.halfOpenElement("iq");
addCommonAttributes(buf);
if (type == null) {
buf.append("type=\"get\">");
buf.attribute("type", "get");
}
else {
buf.append("type=\"").append(getType()).append("\">");
buf.attribute("type", type.toString());
}
buf.rightAngelBracket();
// Add the query section if there is one.
String queryXML = getChildElementXML();
if (queryXML != null) {
buf.append(queryXML);
}
buf.optAppend(getChildElementXML());
// Add the error sub-packet, if there is one.
XMPPError error = getError();
if (error != null) {
buf.append(error.toXML());
}
buf.append("</iq>");
return buf.toString();
buf.closeElement("iq");
return buf;
}
/**
@ -111,7 +102,7 @@ public abstract class IQ extends Packet {
*
* @return the child element section of the IQ XML.
*/
public abstract String getChildElementXML();
public abstract CharSequence getChildElementXML();
/**
* Convenience method to create a new empty {@link Type#RESULT IQ.Type.RESULT}
@ -170,7 +161,8 @@ public abstract class IQ extends Packet {
"IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML());
}
final IQ result = new IQ() {
public String getChildElementXML() {
@Override
public CharSequence getChildElementXML() {
return request.getChildElementXML();
}
};

View File

@ -17,7 +17,7 @@
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import java.util.*;
@ -404,59 +404,47 @@ public class Message extends Packet {
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<message");
if (getXmlns() != null) {
buf.append(" xmlns=\"").append(getXmlns()).append("\"");
}
if (language != null) {
buf.append(" xml:lang=\"").append(getLanguage()).append("\"");
}
if (getPacketID() != null) {
buf.append(" id=\"").append(getPacketID()).append("\"");
}
if (getTo() != null) {
buf.append(" to=\"").append(StringUtils.escapeForXML(getTo())).append("\"");
}
if (getFrom() != null) {
buf.append(" from=\"").append(StringUtils.escapeForXML(getFrom())).append("\"");
}
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder buf = new XmlStringBuilder();
buf.halfOpenElement("message");
buf.xmlnsAttribute(getXmlns());
buf.xmllangAttribute(getLanguage());
addCommonAttributes(buf);
if (type != Type.normal) {
buf.append(" type=\"").append(type).append("\"");
buf.attribute("type", type);
}
buf.append(">");
buf.rightAngelBracket();
// Add the subject in the default language
Subject defaultSubject = getMessageSubject(null);
if (defaultSubject != null) {
buf.append("<subject>").append(StringUtils.escapeForXML(defaultSubject.subject)).append("</subject>");
buf.element("subject", defaultSubject.subject);
}
// Add the subject in other languages
for (Subject subject : getSubjects()) {
// Skip the default language
if(subject.equals(defaultSubject))
continue;
buf.append("<subject xml:lang=\"").append(subject.language).append("\">");
buf.append(StringUtils.escapeForXML(subject.subject));
buf.append("</subject>");
buf.halfOpenElement("subject").xmllangAttribute(subject.language).rightAngelBracket();
buf.escape(subject.subject);
buf.closeElement("subject");
}
// Add the body in the default language
Body defaultBody = getMessageBody(null);
if (defaultBody != null) {
buf.append("<body>").append(StringUtils.escapeForXML(defaultBody.message)).append("</body>");
buf.element("body", defaultBody.message);
}
// Add the bodies in other languages
for (Body body : getBodies()) {
// Skip the default language
if(body.equals(defaultBody))
continue;
buf.append("<body xml:lang=\"").append(body.getLanguage()).append("\">");
buf.append(StringUtils.escapeForXML(body.getMessage()));
buf.append("</body>");
}
if (thread != null) {
buf.append("<thread>").append(thread).append("</thread>");
buf.halfOpenElement("body").xmllangAttribute(body.getLanguage()).rightAngelBracket();
buf.escape(body.getMessage());
buf.closeElement("body");
}
buf.optElement("thread", thread);
// Append the error subpacket if the message type is an error.
if (type == Type.error) {
XMPPError error = getError();
@ -466,8 +454,8 @@ public class Message extends Packet {
}
// Add packet extensions, if any are defined.
buf.append(getExtensionsXML());
buf.append("</message>");
return buf.toString();
buf.closeElement("message");
return buf;
}

View File

@ -18,6 +18,7 @@
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
@ -352,7 +353,7 @@ public abstract class Packet {
*
* @return the XML format of the packet as a String.
*/
public abstract String toXML();
public abstract CharSequence toXML();
/**
* Returns the extension sub-packets (including properties data) as an XML
@ -361,40 +362,47 @@ public abstract class Packet {
* @return the extension sub-packets as XML or the Empty String if there
* are no packet extensions.
*/
protected synchronized String getExtensionsXML() {
StringBuilder buf = new StringBuilder();
protected synchronized CharSequence getExtensionsXML() {
XmlStringBuilder xml = new XmlStringBuilder();
// Add in all standard extension sub-packets.
for (PacketExtension extension : getExtensions()) {
buf.append(extension.toXML());
xml.append(extension.toXML());
}
// Add in packet properties.
if (properties != null && !properties.isEmpty()) {
buf.append("<properties xmlns=\"http://www.jivesoftware.com/xmlns/xmpp/properties\">");
xml.halfOpenElement("properties").xmlnsAttribute("http://www.jivesoftware.com/xmlns/xmpp/properties");
// Loop through all properties and write them out.
for (String name : getPropertyNames()) {
Object value = getProperty(name);
buf.append("<property>");
buf.append("<name>").append(StringUtils.escapeForXML(name)).append("</name>");
buf.append("<value type=\"");
xml.openElement("property");
xml.element("name", name);
xml.halfOpenElement("value");
String type;
String valueStr;
if (value instanceof Integer) {
buf.append("integer\">").append(value).append("</value>");
type = "integer";
valueStr = Integer.toString((Integer)value);
}
else if (value instanceof Long) {
buf.append("long\">").append(value).append("</value>");
type = "long";
valueStr = Long.toString((Long) value);
}
else if (value instanceof Float) {
buf.append("float\">").append(value).append("</value>");
type = "float";
valueStr = Float.toString((Float) value);
}
else if (value instanceof Double) {
buf.append("double\">").append(value).append("</value>");
type = "double";
valueStr = Double.toString((Double) value);
}
else if (value instanceof Boolean) {
buf.append("boolean\">").append(value).append("</value>");
type = "boolean";
valueStr = Boolean.toString((Boolean) value);
}
else if (value instanceof String) {
buf.append("string\">");
buf.append(StringUtils.escapeForXML((String)value));
buf.append("</value>");
type = "string";
valueStr = (String) value;
}
// Otherwise, it's a generic Serializable object. Serialized objects are in
// a binary format, which won't work well inside of XML. Therefore, we base-64
@ -406,12 +414,13 @@ public abstract class Packet {
byteStream = new ByteArrayOutputStream();
out = new ObjectOutputStream(byteStream);
out.writeObject(value);
buf.append("java-object\">");
String encodedVal = StringUtils.encodeBase64(byteStream.toByteArray());
buf.append(encodedVal).append("</value>");
type ="java-object";
valueStr = StringUtils.encodeBase64(byteStream.toByteArray());
}
catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error encoding java object", e);
type ="java-object";
valueStr = "Serializing error: " + e.getMessage();
}
finally {
if (out != null) {
@ -432,11 +441,15 @@ public abstract class Packet {
}
}
}
buf.append("</property>");
xml.attribute("type", type);
xml.rightAngelBracket();
xml.escape(valueStr);
xml.closeElement("value");
xml.closeElement("property");
}
buf.append("</properties>");
xml.closeElement("properties");
}
return buf.toString();
return xml;
}
public String getXmlns() {
@ -485,9 +498,20 @@ public abstract class Packet {
result = 31 * result + (error != null ? error.hashCode() : 0);
return result;
}
@Override
public String toString() {
return toXML();
return toXML().toString();
}
/**
* Add to, from and id attributes
*
* @param xml
*/
protected void addCommonAttributes(XmlStringBuilder xml) {
xml.optAttribute("id", getPacketID());
xml.optAttribute("to", getTo());
xml.optAttribute("from", getFrom());
}
}

View File

@ -49,5 +49,5 @@ public interface PacketExtension {
*
* @return the packet extension as XML.
*/
public String toXML();
public CharSequence toXML();
}

View File

@ -17,7 +17,7 @@
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* Represents XMPP presence packets. Every presence packet has a type, which is one of
@ -220,49 +220,35 @@ public class Presence extends Packet {
this.language = language;
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<presence");
if(getXmlns() != null) {
buf.append(" xmlns=\"").append(getXmlns()).append("\"");
}
if (language != null) {
buf.append(" xml:lang=\"").append(getLanguage()).append("\"");
}
if (getPacketID() != null) {
buf.append(" id=\"").append(getPacketID()).append("\"");
}
if (getTo() != null) {
buf.append(" to=\"").append(StringUtils.escapeForXML(getTo())).append("\"");
}
if (getFrom() != null) {
buf.append(" from=\"").append(StringUtils.escapeForXML(getFrom())).append("\"");
}
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder buf = new XmlStringBuilder();
buf.halfOpenElement("presence");
buf.xmlnsAttribute(getXmlns());
buf.xmllangAttribute(getLanguage());
addCommonAttributes(buf);
if (type != Type.available) {
buf.append(" type=\"").append(type).append("\"");
}
buf.append(">");
if (status != null) {
buf.append("<status>").append(StringUtils.escapeForXML(status)).append("</status>");
buf.attribute("type", type);
}
buf.rightAngelBracket();
buf.optElement("status", status);
if (priority != Integer.MIN_VALUE) {
buf.append("<priority>").append(priority).append("</priority>");
buf.element("priority", Integer.toString(priority));
}
if (mode != null && mode != Mode.available) {
buf.append("<show>").append(mode).append("</show>");
buf.element("show", mode);
}
buf.append(this.getExtensionsXML());
buf.append(getExtensionsXML());
// Add the error sub-packet, if there is one.
XMPPError error = getError();
if (error != null) {
buf.append(error.toXML());
}
buf.closeElement("presence");
buf.append("</presence>");
return buf.toString();
return buf;
}
public String toString() {

View File

@ -19,6 +19,8 @@ package org.jivesoftware.smack.packet;
import java.util.Map;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* Represents registration packets. An empty GET query will cause the server to return information
* about it's registration support. SET queries can be used to create accounts or update
@ -85,23 +87,22 @@ public class Registration extends IQ {
this.attributes = attributes;
}
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<query xmlns=\"jabber:iq:register\">");
if (instructions != null) {
buf.append("<instructions>").append(instructions).append("</instructions>");
}
@Override
public XmlStringBuilder getChildElementXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement("query");
xml.xmlnsAttribute("jabber:iq:register");
xml.rightAngelBracket();
xml.optElement("instructions", instructions);
if (attributes != null && attributes.size() > 0) {
for (String name : attributes.keySet()) {
String value = attributes.get(name);
buf.append("<").append(name).append(">");
buf.append(value);
buf.append("</").append(name).append(">");
xml.element(name, value);
}
}
// Add packet extensions, if any are defined.
buf.append(getExtensionsXML());
buf.append("</query>");
return buf.toString();
xml.append(getExtensionsXML());
xml.closeElement("query");
return xml;
}
}

View File

@ -18,6 +18,7 @@
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
@ -65,22 +66,20 @@ public class RosterPacket extends IQ {
}
}
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<query xmlns=\"jabber:iq:roster\"");
if (rosterVersion != null) {
buf.append(" ver=\"");
buf.append(rosterVersion);
buf.append('"');
}
buf.append(">");
public CharSequence getChildElementXML() {
XmlStringBuilder buf = new XmlStringBuilder();
buf.halfOpenElement("query");
buf.xmlnsAttribute("jabber:iq:roster");
buf.optAttribute("ver", rosterVersion);
buf.rightAngelBracket();
synchronized (rosterItems) {
for (Item entry : rosterItems) {
buf.append(entry.toXML());
}
}
buf.append("</query>");
return buf.toString();
buf.closeElement("query");
return buf;
}
public String getVersion() {

View File

@ -36,7 +36,8 @@ public class Session extends IQ {
setType(IQ.Type.SET);
}
public String getChildElementXML() {
@Override
public CharSequence getChildElementXML() {
return "<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>";
}
}

View File

@ -152,7 +152,7 @@ public class XMPPError {
*
* @return the error as XML.
*/
public String toXML() {
public CharSequence toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<error");
if (type != null) {

View File

@ -0,0 +1,89 @@
/**
*
* Copyright 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
import java.util.ArrayList;
import java.util.List;
public class LazyStringBuilder implements Appendable, CharSequence {
private final List<CharSequence> list;
public LazyStringBuilder() {
list = new ArrayList<CharSequence>(20);
}
public LazyStringBuilder append(LazyStringBuilder lsb) {
list.addAll(lsb.list);
return this;
}
@Override
public LazyStringBuilder append(CharSequence csq) {
assert csq != null;
list.add(csq);
return this;
}
@Override
public LazyStringBuilder append(CharSequence csq, int start, int end) {
CharSequence subsequence = csq.subSequence(start, end);
list.add(subsequence);
return this;
}
@Override
public LazyStringBuilder append(char c) {
list.add(Character.toString(c));
return this;
}
@Override
public int length() {
int length = 0;
for (CharSequence csq : list) {
length += csq.length();
}
return length;
}
@Override
public char charAt(int index) {
for (CharSequence csq : list) {
if (index < csq.length()) {
return csq.charAt(index);
} else {
index -= csq.length();
}
}
throw new IndexOutOfBoundsException();
}
@Override
public CharSequence subSequence(int start, int end) {
return toString().subSequence(start, end);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(length());
for (CharSequence csq : list) {
sb.append(csq);
}
return sb.toString();
}
}

View File

@ -290,7 +290,7 @@ public class StringUtils {
* @param string the string to escape.
* @return the string with appropriate characters escaped.
*/
public static String escapeForXML(final String string) {
public static CharSequence escapeForXML(final String string) {
if (string == null) {
return null;
}
@ -339,7 +339,7 @@ public class StringUtils {
if (i > last) {
out.append(input, last, i - last);
}
return out.toString();
return out;
}
/**

View File

@ -16,12 +16,20 @@
*/
package org.jivesoftware.smack.util;
public class XmlStringBuilder implements Appendable, CharSequence {
import org.jivesoftware.smack.packet.PacketExtension;
private final StringBuilder sb;
public class XmlStringBuilder implements Appendable, CharSequence {
public static final String RIGHT_ANGEL_BRACKET = Character.toString('>');
private final LazyStringBuilder sb;
public XmlStringBuilder() {
sb = new StringBuilder();
sb = new LazyStringBuilder();
}
public XmlStringBuilder(PacketExtension pe) {
this();
prelude(pe);
}
/**
@ -29,11 +37,10 @@ public class XmlStringBuilder implements Appendable, CharSequence {
*
* @param name
* @param content
* @return
* @return the XmlStringBuilder
*/
public XmlStringBuilder element(String name, String content) {
if (content == null)
return this;
assert content != null;
openElement(name);
escape(content);
closeElement(name);
@ -41,8 +48,21 @@ public class XmlStringBuilder implements Appendable, CharSequence {
}
public XmlStringBuilder element(String name, Enum<?> content) {
assert content != null;
element(name, content.name());
return this;
}
public XmlStringBuilder optElement(String name, String content) {
if (content != null) {
element(name, content.name());
element(name, content);
}
return this;
}
public XmlStringBuilder optElement(String name, Enum<?> content) {
if (content != null) {
element(name, content);
}
return this;
}
@ -53,54 +73,111 @@ public class XmlStringBuilder implements Appendable, CharSequence {
}
public XmlStringBuilder openElement(String name) {
halfOpenElement(name).append('>');
halfOpenElement(name).rightAngelBracket();
return this;
}
public XmlStringBuilder closeElement(String name) {
sb.append("</").append(name).append('>');
sb.append("</").append(name);
rightAngelBracket();
return this;
}
public XmlStringBuilder emptyElementClose() {
public XmlStringBuilder closeElement(PacketExtension pe) {
closeElement(pe.getElementName());
return this;
}
public XmlStringBuilder closeEmptyElement() {
sb.append("/>");
return this;
}
public XmlStringBuilder rightAngelBracket() {
sb.append(RIGHT_ANGEL_BRACKET);
return this;
}
/**
* Does nothing if value is null.
*
* @param name
* @param value
* @return
* @return the XmlStringBuilder
*/
public XmlStringBuilder attribute(String name, String value) {
if (value == null)
return this;
assert value != null;
sb.append(' ').append(name).append("='");
escape(value);
sb.append('\'');
return this;
}
public XmlStringBuilder xmlnsAttribute(String value) {
attribute("xmlns", value);
public XmlStringBuilder attribute(String name, Enum<?> value) {
assert value != null;
attribute(name, value.name());
return this;
}
public XmlStringBuilder optAttribute(String name, String value) {
if (value != null) {
attribute(name, value);
}
return this;
}
public XmlStringBuilder optAttribute(String name, Enum<?> value) {
if (value != null) {
attribute(name, value.name());
}
return this;
}
public XmlStringBuilder xmlnsAttribute(String value) {
optAttribute("xmlns", value);
return this;
}
public XmlStringBuilder xmllangAttribute(String value) {
optAttribute("xml:lang", value);
return this;
}
public XmlStringBuilder escape(String text) {
assert text != null;
sb.append(StringUtils.escapeForXML(text));
return this;
}
public XmlStringBuilder prelude(PacketExtension pe) {
halfOpenElement(pe.getElementName());
xmlnsAttribute(pe.getNamespace());
return this;
}
public XmlStringBuilder optAppend(CharSequence csq) {
if (csq != null) {
append(csq);
}
return this;
}
public XmlStringBuilder append(XmlStringBuilder xsb) {
assert xsb != null;
sb.append(xsb.sb);
return this;
}
@Override
public XmlStringBuilder append(CharSequence csq) {
assert csq != null;
sb.append(csq);
return this;
}
@Override
public XmlStringBuilder append(CharSequence csq, int start, int end) {
assert csq != null;
sb.append(csq, start, end);
return this;
}

View File

@ -1,34 +0,0 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
/**
* Simple utility for XML.
*
* @author Robin Collier
*/
public class XmlUtils {
static public void appendAttribute(StringBuilder builder, String att, String value) {
builder.append(" ");
builder.append(att);
builder.append("='");
builder.append(value);
builder.append("'");
}
}

View File

@ -54,7 +54,7 @@ public class MessageTest {
Message messageTypeInConstructor = new Message(null, Message.Type.chat);
messageTypeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE);
assertEquals(type, messageTypeInConstructor.getType());
assertXMLEqual(control, messageTypeInConstructor.toXML());
assertXMLEqual(control, messageTypeInConstructor.toXML().toString());
controlBuilder = new StringBuilder();
controlBuilder.append("<message")
@ -67,7 +67,7 @@ public class MessageTest {
Message messageTypeSet = getNewMessage();
messageTypeSet.setType(type2);
assertEquals(type2, messageTypeSet.getType());
assertXMLEqual(control, messageTypeSet.toXML());
assertXMLEqual(control, messageTypeSet.toXML().toString());
}
@Test(expected=IllegalArgumentException.class)
@ -98,7 +98,7 @@ public class MessageTest {
message.setSubject(messageSubject);
assertEquals(messageSubject, message.getSubject());
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
}
@Test
@ -117,7 +117,7 @@ public class MessageTest {
message.setBody(messageBody);
assertEquals(messageBody, message.getBody());
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
}
@Test
@ -150,7 +150,7 @@ public class MessageTest {
message.addBody(null, messageBody1);
message.addBody(lang2, messageBody2);
message.addBody(lang3, messageBody3);
Diff xmlDiff = new Diff(control, message.toXML());
Diff xmlDiff = new Diff(control, message.toXML().toString());
xmlDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
assertTrue(xmlDiff.similar());
@ -196,7 +196,7 @@ public class MessageTest {
message.setThread(messageThread);
assertEquals(messageThread, message.getThread());
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
}
@Test
@ -214,7 +214,7 @@ public class MessageTest {
Message message = getNewMessage();
message.setLanguage(lang);
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
}
@Test

View File

@ -49,7 +49,7 @@ public class PresenceTest {
Presence presenceTypeInConstructor = new Presence(type);
presenceTypeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE);
assertEquals(type, presenceTypeInConstructor.getType());
assertXMLEqual(control, presenceTypeInConstructor.toXML());
assertXMLEqual(control, presenceTypeInConstructor.toXML().toString());
controlBuilder = new StringBuilder();
controlBuilder.append("<presence")
@ -62,7 +62,7 @@ public class PresenceTest {
Presence presenceTypeSet = getNewPresence();
presenceTypeSet.setType(type2);
assertEquals(type2, presenceTypeSet.getType());
assertXMLEqual(control, presenceTypeSet.toXML());
assertXMLEqual(control, presenceTypeSet.toXML().toString());
}
@Test(expected=NullPointerException.class)
@ -96,7 +96,7 @@ public class PresenceTest {
presence.setStatus(status);
assertEquals(status, presence.getStatus());
assertXMLEqual(control, presence.toXML());
assertXMLEqual(control, presence.toXML().toString());
}
@Test
@ -115,7 +115,7 @@ public class PresenceTest {
presence.setPriority(priority);
assertEquals(priority, presence.getPriority());
assertXMLEqual(control, presence.toXML());
assertXMLEqual(control, presence.toXML().toString());
}
@Test(expected=IllegalArgumentException.class)
@ -148,7 +148,7 @@ public class PresenceTest {
mode1);
presenceModeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE);
assertEquals(mode1, presenceModeInConstructor.getMode());
assertXMLEqual(control, presenceModeInConstructor.toXML());
assertXMLEqual(control, presenceModeInConstructor.toXML().toString());
controlBuilder = new StringBuilder();
controlBuilder.append("<presence>")
@ -161,7 +161,7 @@ public class PresenceTest {
Presence presenceModeSet = getNewPresence();
presenceModeSet.setMode(mode2);
assertEquals(mode2, presenceModeSet.getMode());
assertXMLEqual(control, presenceModeSet.toXML());
assertXMLEqual(control, presenceModeSet.toXML().toString());
}
@Test
@ -189,7 +189,7 @@ public class PresenceTest {
Presence presence = getNewPresence();
presence.setLanguage(lang);
assertXMLEqual(control, presence.toXML());
assertXMLEqual(control, presence.toXML().toString());
}
private static Presence getNewPresence() {

View File

@ -75,7 +75,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getBodyLanguages().isEmpty());
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
assertNull(message.getBody(otherLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has non-default language, body has no language
control = XMLBuilder.create("message")
@ -94,7 +94,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getBodyLanguages().isEmpty());
assertEquals(otherLanguage, message.getBody(otherLanguage));
assertNull(message.getBody(defaultLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has no language, body has no language
control = XMLBuilder.create("message")
@ -112,7 +112,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getBodyLanguages().isEmpty());
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
assertNull(message.getBody(otherLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has no language, body has default language
control = XMLBuilder.create("message")
@ -133,7 +133,7 @@ public class PacketParserUtilsTest {
assertNull(message.getBody(otherLanguage));
// body attribute xml:lang is unnecessary
assertXMLNotEqual(control, message.toXML());
assertXMLNotEqual(control, message.toXML().toString());
// message has no language, body has non-default language
control = XMLBuilder.create("message")
@ -153,7 +153,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getBodyLanguages().contains(otherLanguage));
assertEquals(otherLanguage, message.getBody(otherLanguage));
assertNull(message.getBody(defaultLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has default language, body has non-default language
control = XMLBuilder.create("message")
@ -174,7 +174,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getBodyLanguages().contains(otherLanguage));
assertEquals(otherLanguage, message.getBody(otherLanguage));
assertNull(message.getBody(defaultLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has non-default language, body has default language
control = XMLBuilder.create("message")
@ -195,7 +195,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getBodyLanguages().contains(defaultLanguage));
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
assertNull(message.getBody(otherLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
}
@ -225,7 +225,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getSubjectLanguages().isEmpty());
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
assertNull(message.getSubject(otherLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has non-default language, subject has no language
control = XMLBuilder.create("message")
@ -244,7 +244,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getSubjectLanguages().isEmpty());
assertEquals(otherLanguage, message.getSubject(otherLanguage));
assertNull(message.getSubject(defaultLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has no language, subject has no language
control = XMLBuilder.create("message")
@ -262,7 +262,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getSubjectLanguages().isEmpty());
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
assertNull(message.getSubject(otherLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has no language, subject has default language
control = XMLBuilder.create("message")
@ -283,7 +283,7 @@ public class PacketParserUtilsTest {
assertNull(message.getSubject(otherLanguage));
// subject attribute xml:lang is unnecessary
assertXMLNotEqual(control, message.toXML());
assertXMLNotEqual(control, message.toXML().toString());
// message has no language, subject has non-default language
control = XMLBuilder.create("message")
@ -303,7 +303,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getSubjectLanguages().contains(otherLanguage));
assertEquals(otherLanguage, message.getSubject(otherLanguage));
assertNull(message.getSubject(defaultLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has default language, subject has non-default language
control = XMLBuilder.create("message")
@ -324,7 +324,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getSubjectLanguages().contains(otherLanguage));
assertEquals(otherLanguage, message.getSubject(otherLanguage));
assertNull(message.getSubject(defaultLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has non-default language, subject has default language
control = XMLBuilder.create("message")
@ -345,7 +345,7 @@ public class PacketParserUtilsTest {
assertTrue(message.getSubjectLanguages().contains(defaultLanguage));
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
assertNull(message.getSubject(otherLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
}
@ -381,7 +381,7 @@ public class PacketParserUtilsTest {
assertEquals(2, message.getBodies().size());
assertEquals(1, message.getBodyLanguages().size());
assertTrue(message.getBodyLanguages().contains(otherLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has default language, first body no language, second body default language
control = XMLBuilder.create("message")
@ -405,7 +405,7 @@ public class PacketParserUtilsTest {
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
assertEquals(1, message.getBodies().size());
assertEquals(0, message.getBodyLanguages().size());
assertXMLNotEqual(control, message.toXML());
assertXMLNotEqual(control, message.toXML().toString());
// message has non-default language, first body no language, second body default language
control = XMLBuilder.create("message")
@ -430,7 +430,7 @@ public class PacketParserUtilsTest {
assertEquals(2, message.getBodies().size());
assertEquals(1, message.getBodyLanguages().size());
assertTrue(message.getBodyLanguages().contains(defaultLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has no language, first body no language, second body default language
control = XMLBuilder.create("message")
@ -453,7 +453,7 @@ public class PacketParserUtilsTest {
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
assertEquals(1, message.getBodies().size());
assertEquals(0, message.getBodyLanguages().size());
assertXMLNotEqual(control, message.toXML());
assertXMLNotEqual(control, message.toXML().toString());
// message has no language, first body no language, second body other language
control = XMLBuilder.create("message")
@ -477,7 +477,7 @@ public class PacketParserUtilsTest {
assertEquals(otherLanguage, message.getBody(otherLanguage));
assertEquals(2, message.getBodies().size());
assertEquals(1, message.getBodyLanguages().size());
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has no language, first body no language, second body no language
control = XMLBuilder.create("message")
@ -499,7 +499,7 @@ public class PacketParserUtilsTest {
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
assertEquals(1, message.getBodies().size());
assertEquals(0, message.getBodyLanguages().size());
assertXMLNotEqual(control, message.toXML());
assertXMLNotEqual(control, message.toXML().toString());
}
@ -535,7 +535,7 @@ public class PacketParserUtilsTest {
assertEquals(2, message.getSubjects().size());
assertEquals(1, message.getSubjectLanguages().size());
assertTrue(message.getSubjectLanguages().contains(otherLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has default language, first subject no language, second subject default language
control = XMLBuilder.create("message")
@ -559,7 +559,7 @@ public class PacketParserUtilsTest {
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
assertEquals(1, message.getSubjects().size());
assertEquals(0, message.getSubjectLanguages().size());
assertXMLNotEqual(control, message.toXML());
assertXMLNotEqual(control, message.toXML().toString());
// message has non-default language, first subject no language, second subject default language
control = XMLBuilder.create("message")
@ -584,7 +584,7 @@ public class PacketParserUtilsTest {
assertEquals(2, message.getSubjects().size());
assertEquals(1, message.getSubjectLanguages().size());
assertTrue(message.getSubjectLanguages().contains(defaultLanguage));
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has no language, first subject no language, second subject default language
control = XMLBuilder.create("message")
@ -607,7 +607,7 @@ public class PacketParserUtilsTest {
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
assertEquals(1, message.getSubjects().size());
assertEquals(0, message.getSubjectLanguages().size());
assertXMLNotEqual(control, message.toXML());
assertXMLNotEqual(control, message.toXML().toString());
// message has no language, first subject no language, second subject other language
control = XMLBuilder.create("message")
@ -631,7 +631,7 @@ public class PacketParserUtilsTest {
assertEquals(otherLanguage, message.getSubject(otherLanguage));
assertEquals(2, message.getSubjects().size());
assertEquals(1, message.getSubjectLanguages().size());
assertXMLEqual(control, message.toXML());
assertXMLEqual(control, message.toXML().toString());
// message has no language, first subject no language, second subject no language
control = XMLBuilder.create("message")
@ -653,7 +653,7 @@ public class PacketParserUtilsTest {
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
assertEquals(1, message.getSubjects().size());
assertEquals(0, message.getSubjectLanguages().size());
assertXMLNotEqual(control, message.toXML());
assertXMLNotEqual(control, message.toXML().toString());
}
@ -679,9 +679,9 @@ public class PacketParserUtilsTest {
+ "Bad Message Body</span>";
assertEquals(body, message.getBody());
assertXMLNotEqual(control, message.toXML());
assertXMLNotEqual(control, message.toXML().toString());
DetailedDiff diffs = new DetailedDiff(new Diff(control, message.toXML()));
DetailedDiff diffs = new DetailedDiff(new Diff(control, message.toXML().toString()));
// body has no namespace URI, span is escaped
assertEquals(6, diffs.getAllDifferences().size());
@ -754,7 +754,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
Packet message = PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
Diff xmlDiff = new Diff(control, message.toXML());
Diff xmlDiff = new Diff(control, message.toXML().toString());
xmlDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
assertTrue(xmlDiff.similar());
}
@ -765,7 +765,7 @@ public class PacketParserUtilsTest {
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
assertXMLEqual(stanza, presence.toXML());
assertXMLEqual(stanza, presence.toXML().toString());
}
@SuppressWarnings("deprecation")
@ -775,7 +775,7 @@ public class PacketParserUtilsTest {
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
assertXMLEqual(stanza, presence.toXML());
assertXMLEqual(stanza, presence.toXML().toString());
assertEquals(Presence.Type.unsubscribed, presence.getType());
}
@ -789,7 +789,7 @@ public class PacketParserUtilsTest {
+ "</presence>";
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
assertXMLEqual(stanza, presence.toXML());
assertXMLEqual(stanza, presence.toXML().toString());
assertEquals(Presence.Type.unsubscribed, presence.getType());
assertEquals("dnd", presence.getMode().name());
assertEquals("en", presence.getLanguage());

View File

@ -35,39 +35,43 @@ public class StringUtilsTest {
assertNull(StringUtils.escapeForXML(null));
input = "<b>";
assertEquals("&lt;b&gt;", StringUtils.escapeForXML(input));
assertCharSequenceEquals("&lt;b&gt;", StringUtils.escapeForXML(input));
input = "\"";
assertEquals("&quot;", StringUtils.escapeForXML(input));
assertCharSequenceEquals("&quot;", StringUtils.escapeForXML(input));
input = "&";
assertEquals("&amp;", StringUtils.escapeForXML(input));
assertCharSequenceEquals("&amp;", StringUtils.escapeForXML(input));
input = "<b>\n\t\r</b>";
assertEquals("&lt;b&gt;\n\t\r&lt;/b&gt;", StringUtils.escapeForXML(input));
assertCharSequenceEquals("&lt;b&gt;\n\t\r&lt;/b&gt;", StringUtils.escapeForXML(input));
input = " & ";
assertEquals(" &amp; ", StringUtils.escapeForXML(input));
assertCharSequenceEquals(" &amp; ", StringUtils.escapeForXML(input));
input = " \" ";
assertEquals(" &quot; ", StringUtils.escapeForXML(input));
assertCharSequenceEquals(" &quot; ", StringUtils.escapeForXML(input));
input = "> of me <";
assertEquals("&gt; of me &lt;", StringUtils.escapeForXML(input));
assertCharSequenceEquals("&gt; of me &lt;", StringUtils.escapeForXML(input));
input = "> of me & you<";
assertEquals("&gt; of me &amp; you&lt;", StringUtils.escapeForXML(input));
assertCharSequenceEquals("&gt; of me &amp; you&lt;", StringUtils.escapeForXML(input));
input = "& <";
assertEquals("&amp; &lt;", StringUtils.escapeForXML(input));
assertCharSequenceEquals("&amp; &lt;", StringUtils.escapeForXML(input));
input = "&";
assertEquals("&amp;", StringUtils.escapeForXML(input));
assertCharSequenceEquals("&amp;", StringUtils.escapeForXML(input));
input = "It's a good day today";
assertEquals("It&apos;s a good day today", StringUtils.escapeForXML(input));
assertCharSequenceEquals("It&apos;s a good day today", StringUtils.escapeForXML(input));
}
public static void assertCharSequenceEquals(CharSequence expected, CharSequence actual) {
assertEquals(expected.toString(), actual.toString());
}
@Test
public void testHash() {
// Test null

View File

@ -801,7 +801,7 @@ public class EnhancedDebugger implements SmackDebugger {
messagesTable.addRow(
new Object[]{
formatXML(packet.toXML()),
formatXML(packet.toXML().toString()),
dateFormatter.format(new Date()),
packetReceivedIcon,
packetTypeIcon,
@ -862,7 +862,7 @@ public class EnhancedDebugger implements SmackDebugger {
messagesTable.addRow(
new Object[]{
formatXML(packet.toXML()),
formatXML(packet.toXML().toString()),
dateFormatter.format(new Date()),
packetSentIcon,
packetTypeIcon,

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");

View File

@ -786,9 +786,6 @@ public class AgentSession {
* @throws NotConnectedException
*/
public void setNote(String sessionID, String note) throws NoResponseException, XMPPErrorException, NotConnectedException {
note = ChatNotes.replace(note, "\n", "\\n");
note = StringUtils.escapeForXML(note);
ChatNotes notes = new ChatNotes();
notes.setType(IQ.Type.SET);
notes.setTo(workgroupJID);

View File

@ -19,6 +19,7 @@ package org.jivesoftware.smackx.workgroup.ext.notes;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.xmlpull.v1.XmlPullParser;
/**
@ -57,13 +58,13 @@ public class ChatNotes extends IQ {
}
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
XmlStringBuilder buf = new XmlStringBuilder();
buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">");
buf.append("<sessionID>").append(getSessionID()).append("</sessionID>");
if (getNotes() != null) {
buf.append("<notes>").append(getNotes()).append("</notes>");
buf.element("notes", getNotes());
}
buf.append("</").append(ELEMENT_NAME).append("> ");

View File

@ -374,13 +374,10 @@ public class Workgroup {
String name = iter.next();
String value = metadata.get(name).toString();
String escapedName = StringUtils.escapeForXML(name);
String escapedValue = StringUtils.escapeForXML(value);
FormField field = new FormField(escapedName);
FormField field = new FormField(name);
field.setType(FormField.TYPE_TEXT_SINGLE);
form.addField(field);
form.setAnswer(escapedName, escapedValue);
form.setAnswer(name, value);
}
joinQueue(form, userID);
}

View File

@ -146,7 +146,7 @@ class PacketWriter {
while (!done && (writerThread == thisThread)) {
Packet packet = nextPacket();
if (packet != null) {
writer.write(packet.toXML());
writer.write(packet.toXML().toString());
if (queue.isEmpty()) {
writer.flush();
@ -159,7 +159,7 @@ class PacketWriter {
try {
while (!queue.isEmpty()) {
Packet packet = queue.remove();
writer.write(packet.toXML());
writer.write(packet.toXML().toString());
}
writer.flush();
}