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