1 package org.opensrf.net.xmpp;
7 * uncomment to use the DOM serialization code...
10 import org.apache.xerces.dom.DocumentImpl;
11 import org.apache.xerces.dom.DOMImplementationImpl;
12 import org.apache.xml.serialize.OutputFormat;
13 import org.apache.xml.serialize.Serializer;
14 import org.apache.xml.serialize.SerializerFactory;
15 import org.apache.xml.serialize.XMLSerializer;
20 * Models a single XMPP message.
22 public class XMPPMessage {
26 /** Message recipient */
31 private String thread;
35 public XMPPMessage() {
38 public String getBody() {
41 public String getTo() {
44 public String getFrom() {
47 public String getThread() {
50 public String getXid() {
53 public void setBody(String body) {
56 public void setTo(String to) {
59 public void setFrom(String from) {
62 public void setThread(String thread) {
65 public void setXid(String xid) {
71 * Generates the XML representation of this message.
73 public String toXML() {
74 StringBuffer sb = new StringBuffer("<message to='");
76 sb.append("' osrf_xid='");
78 sb.append("'><thread>");
79 escapeXML(thread, sb);
80 sb.append("</thread><body>");
82 sb.append("</body></message>");
88 * Escapes non-valid XML characters.
89 * @param s The string to escape.
90 * @param sb The StringBuffer to append new data to.
92 private void escapeXML(String s, StringBuffer sb) {
93 if( s == null ) return;
96 for( int i = 0; i < l; i++ ) {
117 * This is a DOM implementataion of message serialization.
118 * I'm inclined to think the stringbuffer version is faster, but
122 public String __toXML() {
124 Document doc = new DocumentImpl();
125 Element message = doc.createElement("message");
126 Element body = doc.createElement("body");
127 Element thread = doc.createElement("thread");
129 doc.appendChild(message);
130 message.setAttribute("to", getTo());
131 message.setAttribute("from", getFrom());
132 message.appendChild(body);
133 message.appendChild(thread);
135 body.appendChild(doc.createTextNode(getBody()));
136 thread.appendChild(doc.createTextNode(getThread()));
138 XMLSerializer serializer = new XMLSerializer();
139 StringWriter strWriter = new StringWriter();
140 OutputFormat outFormat = new OutputFormat();
142 outFormat.setEncoding("UTF-8");
143 outFormat.setVersion("1.0");
144 outFormat.setIndenting(false);
145 outFormat.setOmitXMLDeclaration(true);
147 serializer.setOutputCharStream(strWriter);
148 serializer.setOutputFormat(outFormat);
151 serializer.serialize(doc);
152 } catch(IOException ioe) {
154 return strWriter.toString();