001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.xhtmlim.packet;
019
020import org.jivesoftware.smack.packet.PacketExtension;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026/**
027 * An XHTML sub-packet, which is used by XMPP clients to exchange formatted text. The XHTML 
028 * extension is only a subset of XHTML 1.0.<p>
029 * 
030 * The following link summarizes the requirements of XHTML IM:
031 * <a href="http://www.xmpp.org/extensions/jep-0071.html#sect-id2598018">Valid tags</a>.<p>
032 * 
033 * Warning: this is an non-standard protocol documented by
034 * <a href="http://www.xmpp.org/extensions/jep-0071.html">XEP-71</a>. Because this is a
035 * non-standard protocol, it is subject to change.
036 *
037 * @author Gaston Dombiak
038 */
039public class XHTMLExtension implements PacketExtension {
040
041    private List<String> bodies = new ArrayList<String>();
042
043    /**
044    * Returns the XML element name of the extension sub-packet root element.
045    * Always returns "html"
046    *
047    * @return the XML element name of the packet extension.
048    */
049    public String getElementName() {
050        return "html";
051    }
052
053    /** 
054     * Returns the XML namespace of the extension sub-packet root element.
055     * According the specification the namespace is always "http://jabber.org/protocol/xhtml-im"
056     *
057     * @return the XML namespace of the packet extension.
058     */
059    public String getNamespace() {
060        return "http://jabber.org/protocol/xhtml-im";
061    }
062
063    /**
064     * Returns the XML representation of a XHTML extension according the specification.
065     * 
066     * Usually the XML representation will be inside of a Message XML representation like
067     * in the following example:
068     * <pre>
069     * &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
070     *     &lt;subject&gt;Any subject you want&lt;/subject&gt;
071     *     &lt;body&gt;This message contains something interesting.&lt;/body&gt;
072     *     &lt;html xmlns="http://jabber.org/protocol/xhtml-im"&gt;
073     *         &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;
074     *     &lt;/html&gt;
075     * &lt;/message&gt;
076     * </pre>
077     * 
078     */
079    public String toXML() {
080        StringBuilder buf = new StringBuilder();
081        buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
082            "\">");
083        // Loop through all the bodies and append them to the string buffer
084        for (String body : getBodies()) {
085            buf.append(body);
086        }
087        buf.append("</").append(getElementName()).append(">");
088        return buf.toString();
089    }
090
091    /**
092     * Returns a List of the bodies in the packet.
093     *
094     * @return a List of the bodies in the packet.
095     */
096    public List<String> getBodies() {
097        synchronized (bodies) {
098            return Collections.unmodifiableList(new ArrayList<String>(bodies));
099        }
100    }
101
102    /**
103     * Adds a body to the packet.
104     *
105     * @param body the body to add.
106     */
107    public void addBody(String body) {
108        synchronized (bodies) {
109            bodies.add(body);
110        }
111    }
112
113    /**
114     * Returns a count of the bodies in the XHTML packet.
115     *
116     * @return the number of bodies in the XHTML packet.
117     */
118    public int getBodiesCount() {
119        return bodies.size();
120    }
121
122}