]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/net/xmpp/XMPPMessage.java
LP1827055 Remove Python libs, install bits, and docs
[OpenSRF.git] / src / java / org / opensrf / net / xmpp / XMPPMessage.java
1 package org.opensrf.net.xmpp;
2
3 import java.io.*;
4
5 /**
6  * Models a single XMPP message.
7  */
8 public class XMPPMessage {
9
10     /** Message body */
11     private String body;
12     /** Message recipient */
13     private String to;
14     /** Message sender */
15     private String from;
16     /** Message thread */
17     private String thread;
18     /** Message xid */
19     private String xid;
20
21     public XMPPMessage() {
22     }
23
24     public String getBody() {
25         return body;
26     }
27     public String getTo() { 
28         return to; 
29     }
30     public String getFrom() { 
31         return from;
32     }
33     public String getThread() { 
34         return thread; 
35     }
36     public String getXid() {
37         return xid;
38     }
39     public void setBody(String body) {
40         this.body = body;
41     }
42     public void setTo(String to) { 
43         this.to = to; 
44     }
45     public void setFrom(String from) { 
46         this.from = from; 
47     }
48     public void setThread(String thread) { 
49         this.thread = thread; 
50     }
51     public void setXid(String xid) {
52         this.xid = xid; 
53     }
54
55
56     /**
57      * Generates the XML representation of this message.
58      */
59     public String toXML() {
60         StringBuffer sb = new StringBuffer("<message to='");
61         escapeXML(to, sb);
62         sb.append("' osrf_xid='");
63         escapeXML(xid, sb);
64         sb.append("'><thread>");
65         escapeXML(thread, sb);
66         sb.append("</thread><body>");
67         escapeXML(body, sb);
68         sb.append("</body></message>");
69         return sb.toString();
70     }
71
72
73     /**
74      * Escapes non-valid XML characters.
75      * @param s The string to escape.
76      * @param sb The StringBuffer to append new data to.
77      */
78     private void escapeXML(String s, StringBuffer sb) {
79         if( s == null ) return;
80         char c;
81         int l = s.length();
82         for( int i = 0; i < l; i++ ) {
83             c = s.charAt(i);
84             switch(c) {
85                 case '<': 
86                     sb.append("&lt;");
87                     break;
88                 case '>': 
89                     sb.append("&gt;");
90                     break;
91                 case '&': 
92                     sb.append("&amp;");
93                     break;
94                 default:
95                     sb.append(c);
96             }
97         }
98     }
99 }
100
101