KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > packet > XHTMLExtension


1 /**
2  * $RCSfile$
3  * $Revision: 2407 $
4  * $Date: 2004-11-02 20:37:00 -0300 (Tue, 02 Nov 2004) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smackx.packet;
22
23 import java.util.*;
24
25 import org.jivesoftware.smack.packet.PacketExtension;
26
27 /**
28  * An XHTML sub-packet, which is used by XMPP clients to exchange formatted text. The XHTML
29  * extension is only a subset of XHTML 1.0.<p>
30  *
31  * The following link summarizes the requirements of XHTML IM:
32  * <a HREF="http://www.jabber.org/jeps/jep-0071.html#sect-id2598018">Valid tags</a>.<p>
33  *
34  * Warning: this is an non-standard protocol documented by
35  * <a HREF="http://www.jabber.org/jeps/jep-0071.html">JEP-71</a>. Because this is a
36  * non-standard protocol, it is subject to change.
37  *
38  * @author Gaston Dombiak
39  */

40 public class XHTMLExtension implements PacketExtension {
41
42     private List bodies = new ArrayList();
43
44     /**
45     * Returns the XML element name of the extension sub-packet root element.
46     * Always returns "html"
47     *
48     * @return the XML element name of the packet extension.
49     */

50     public String JavaDoc getElementName() {
51         return "html";
52     }
53
54     /**
55      * Returns the XML namespace of the extension sub-packet root element.
56      * According the specification the namespace is always "http://jabber.org/protocol/xhtml-im"
57      *
58      * @return the XML namespace of the packet extension.
59      */

60     public String JavaDoc getNamespace() {
61         return "http://jabber.org/protocol/xhtml-im";
62     }
63
64     /**
65      * Returns the XML representation of a XHTML extension according the specification.
66      *
67      * Usually the XML representation will be inside of a Message XML representation like
68      * in the following example:
69      * <pre>
70      * &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
71      * &lt;subject&gt;Any subject you want&lt;/subject&gt;
72      * &lt;body&gt;This message contains something interesting.&lt;/body&gt;
73      * &lt;html xmlns="http://jabber.org/protocol/xhtml-im"&gt;
74      * &lt;body&gt;&lt;p style='font-size:large'&gt;This message contains something &lt;em&gt;interesting&lt;/em&gt;.&lt;/p&gt;&lt;/body&gt;
75      * &lt;/html&gt;
76      * &lt;/message&gt;
77      * </pre>
78      *
79      */

80     public String JavaDoc toXML() {
81         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
82         buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
83             "\">");
84         // Loop through all the bodies and append them to the string buffer
85
for (Iterator i = getBodies(); i.hasNext();) {
86             buf.append((String JavaDoc) i.next());
87         }
88         buf.append("</").append(getElementName()).append(">");
89         return buf.toString();
90     }
91
92     /**
93      * Returns an Iterator for the bodies in the packet.
94      *
95      * @return an Iterator for the bodies in the packet.
96      */

97     public Iterator getBodies() {
98         synchronized (bodies) {
99             return Collections.unmodifiableList(new ArrayList(bodies)).iterator();
100         }
101     }
102
103     /**
104      * Adds a body to the packet.
105      *
106      * @param body the body to add.
107      */

108     public void addBody(String JavaDoc body) {
109         synchronized (bodies) {
110             bodies.add(body);
111         }
112     }
113
114     /**
115      * Returns a count of the bodies in the XHTML packet.
116      *
117      * @return the number of bodies in the XHTML packet.
118      */

119     public int getBodiesCount() {
120         return bodies.size();
121     }
122
123 }
124
Popular Tags