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.Message;
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.List;
027
028/**
029 * An XHTML sub-packet, which is used by XMPP clients to exchange formatted text. The XHTML 
030 * extension is only a subset of XHTML 1.0.
031 * <p>
032 * The following link summarizes the requirements of XHTML IM:
033 * <a href="http://www.xmpp.org/extensions/xep-0071.html">XEP-0071: XHTML-IM</a>.
034 * </p>
035 *
036 * @author Gaston Dombiak
037 */
038public class XHTMLExtension implements ExtensionElement {
039
040    public static final String ELEMENT = "html";
041    public static final String NAMESPACE = "http://jabber.org/protocol/xhtml-im";
042
043    private List<CharSequence> bodies = new ArrayList<CharSequence>();
044
045    /**
046    * Returns the XML element name of the extension sub-packet root element.
047    * Always returns "html"
048    *
049    * @return the XML element name of the stanza(/packet) extension.
050    */
051    public String getElementName() {
052        return ELEMENT;
053    }
054
055    /** 
056     * Returns the XML namespace of the extension sub-packet root element.
057     * According the specification the namespace is always "http://jabber.org/protocol/xhtml-im"
058     *
059     * @return the XML namespace of the stanza(/packet) extension.
060     */
061    public String getNamespace() {
062        return NAMESPACE;
063    }
064
065    /**
066     * Returns the XML representation of a XHTML extension according the specification.
067     * 
068     * Usually the XML representation will be inside of a Message XML representation like
069     * in the following example:
070     * <pre>
071     * &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
072     *     &lt;subject&gt;Any subject you want&lt;/subject&gt;
073     *     &lt;body&gt;This message contains something interesting.&lt;/body&gt;
074     *     &lt;html xmlns="http://jabber.org/protocol/xhtml-im"&gt;
075     *         &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;
076     *     &lt;/html&gt;
077     * &lt;/message&gt;
078     * </pre>
079     * 
080     */
081    @Override
082    public XmlStringBuilder toXML() {
083        XmlStringBuilder xml = new XmlStringBuilder(this);
084        xml.rightAngleBracket();
085        // Loop through all the bodies and append them to the string buffer
086        for (CharSequence body : getBodies()) {
087            xml.append(body);
088        }
089        xml.closeElement(this);
090        return xml;
091    }
092
093    /**
094     * Returns a List of the bodies in the packet.
095     *
096     * @return a List of the bodies in the packet.
097     */
098    public List<CharSequence> getBodies() {
099        synchronized (bodies) {
100            return Collections.unmodifiableList(new ArrayList<CharSequence>(bodies));
101        }
102    }
103
104    /**
105     * Adds a body to the packet.
106     *
107     * @param body the body to add.
108     */
109    public void addBody(CharSequence body) {
110        synchronized (bodies) {
111            bodies.add(body);
112        }
113    }
114
115    /**
116     * Returns a count of the bodies in the XHTML packet.
117     *
118     * @return the number of bodies in the XHTML packet.
119     */
120    public int getBodiesCount() {
121        synchronized (bodies) {
122            return bodies.size();
123        }
124    }
125
126    public static XHTMLExtension from(Message message) {
127        return message.getExtension(ELEMENT, NAMESPACE);
128    }
129}