]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/net/xmpp/XMPPMessage.java
f1f0d55b45dffd27c34ace36f4ee4560bd404c0e
[OpenSRF.git] / src / java / org / opensrf / net / xmpp / XMPPMessage.java
1 package org.opensrf.net.xmpp;
2
3 import java.io.*;
4
5
6 /*
7  * uncomment to use the DOM serialization code...
8  
9 import org.w3c.dom.*;
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;
16 */
17
18
19 /**
20  * Models a single XMPP message.
21  */
22 public class XMPPMessage {
23
24     /** Message body */
25     private String body;
26     /** Message recipient */
27     private String to;
28     /** Message sender */
29     private String from;
30     /** Message thread */
31     private String thread;
32     /** Message xid */
33     private String xid;
34
35     public XMPPMessage() {
36     }
37
38     public String getBody() {
39         return body;
40     }
41     public String getTo() { 
42         return to; 
43     }
44     public String getFrom() { 
45         return from;
46     }
47     public String getThread() { 
48         return thread; 
49     }
50     public String getXid() {
51         return xid;
52     }
53     public void setBody(String body) {
54         this.body = body;
55     }
56     public void setTo(String to) { 
57         this.to = to; 
58     }
59     public void setFrom(String from) { 
60         this.from = from; 
61     }
62     public void setThread(String thread) { 
63         this.thread = thread; 
64     }
65     public void setXid(String xid) {
66         this.xid = xid; 
67     }
68
69
70     /**
71      * Generates the XML representation of this message.
72      */
73     public String toXML() {
74         StringBuffer sb = new StringBuffer("<message to='");
75         escapeXML(to, sb);
76         sb.append("' osrf_xid='");
77         escapeXML(xid, sb);
78         sb.append("'><thread>");
79         escapeXML(thread, sb);
80         sb.append("</thread><body>");
81         escapeXML(body, sb);
82         sb.append("</body></message>");
83         return sb.toString();
84     }
85
86
87     /**
88      * Escapes non-valid XML characters.
89      * @param s The string to escape.
90      * @param sb The StringBuffer to append new data to.
91      */
92     private void escapeXML(String s, StringBuffer sb) {
93         if( s == null ) return;
94         char c;
95         int l = s.length();
96         for( int i = 0; i < l; i++ ) {
97             c = s.charAt(i);
98             switch(c) {
99                 case '<': 
100                     sb.append("&lt;");
101                     break;
102                 case '>': 
103                     sb.append("&gt;");
104                     break;
105                 case '&': 
106                     sb.append("&amp;");
107                     break;
108                 default:
109                     sb.append(c);
110             }
111         }
112     }
113
114
115
116     /**
117      * This is a DOM implementataion of message serialization. 
118      * I'm inclined to think the stringbuffer version is faster, but 
119      * I have no proof.
120      */
121     /*
122     public String __toXML() {
123
124         Document doc = new DocumentImpl();
125         Element message = doc.createElement("message");
126         Element body = doc.createElement("body");
127         Element thread = doc.createElement("thread");
128
129         doc.appendChild(message);
130         message.setAttribute("to", getTo());
131         message.setAttribute("from", getFrom());
132         message.appendChild(body);
133         message.appendChild(thread);
134
135         body.appendChild(doc.createTextNode(getBody()));
136         thread.appendChild(doc.createTextNode(getThread()));
137
138         XMLSerializer serializer = new XMLSerializer();
139         StringWriter strWriter = new StringWriter();
140         OutputFormat outFormat = new OutputFormat();
141
142         outFormat.setEncoding("UTF-8");
143         outFormat.setVersion("1.0");
144         outFormat.setIndenting(false);
145         outFormat.setOmitXMLDeclaration(true);
146
147         serializer.setOutputCharStream(strWriter);
148         serializer.setOutputFormat(outFormat);
149
150         try {
151             serializer.serialize(doc);
152         } catch(IOException ioe) {
153         }
154         return strWriter.toString();
155     }
156     */
157 }
158
159