XHTMLExtension.java

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

  17. package org.jivesoftware.smackx.xhtmlim.packet;

  18. import org.jivesoftware.smack.packet.Message;
  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;

  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;

  24. /**
  25.  * An XHTML sub-packet, which is used by XMPP clients to exchange formatted text. The XHTML
  26.  * extension is only a subset of XHTML 1.0.
  27.  * <p>
  28.  * The following link summarizes the requirements of XHTML IM:
  29.  * <a href="http://www.xmpp.org/extensions/xep-0071.html">XEP-0071: XHTML-IM</a>.
  30.  * </p>
  31.  *
  32.  * @author Gaston Dombiak
  33.  */
  34. public class XHTMLExtension implements ExtensionElement {

  35.     public static final String ELEMENT = "html";
  36.     public static final String NAMESPACE = "http://jabber.org/protocol/xhtml-im";

  37.     private List<CharSequence> bodies = new ArrayList<CharSequence>();

  38.     /**
  39.     * Returns the XML element name of the extension sub-packet root element.
  40.     * Always returns "html"
  41.     *
  42.     * @return the XML element name of the packet extension.
  43.     */
  44.     public String getElementName() {
  45.         return ELEMENT;
  46.     }

  47.     /**
  48.      * Returns the XML namespace of the extension sub-packet root element.
  49.      * According the specification the namespace is always "http://jabber.org/protocol/xhtml-im"
  50.      *
  51.      * @return the XML namespace of the packet extension.
  52.      */
  53.     public String getNamespace() {
  54.         return NAMESPACE;
  55.     }

  56.     /**
  57.      * Returns the XML representation of a XHTML extension according the specification.
  58.      *
  59.      * Usually the XML representation will be inside of a Message XML representation like
  60.      * in the following example:
  61.      * <pre>
  62.      * &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
  63.      *     &lt;subject&gt;Any subject you want&lt;/subject&gt;
  64.      *     &lt;body&gt;This message contains something interesting.&lt;/body&gt;
  65.      *     &lt;html xmlns="http://jabber.org/protocol/xhtml-im"&gt;
  66.      *         &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;
  67.      *     &lt;/html&gt;
  68.      * &lt;/message&gt;
  69.      * </pre>
  70.      *
  71.      */
  72.     @Override
  73.     public XmlStringBuilder toXML() {
  74.         XmlStringBuilder xml = new XmlStringBuilder(this);
  75.         xml.rightAngleBracket();
  76.         // Loop through all the bodies and append them to the string buffer
  77.         for (CharSequence body : getBodies()) {
  78.             xml.append(body);
  79.         }
  80.         xml.closeElement(this);
  81.         return xml;
  82.     }

  83.     /**
  84.      * Returns a List of the bodies in the packet.
  85.      *
  86.      * @return a List of the bodies in the packet.
  87.      */
  88.     public List<CharSequence> getBodies() {
  89.         synchronized (bodies) {
  90.             return Collections.unmodifiableList(new ArrayList<CharSequence>(bodies));
  91.         }
  92.     }

  93.     /**
  94.      * Adds a body to the packet.
  95.      *
  96.      * @param body the body to add.
  97.      */
  98.     public void addBody(CharSequence body) {
  99.         synchronized (bodies) {
  100.             bodies.add(body);
  101.         }
  102.     }

  103.     /**
  104.      * Returns a count of the bodies in the XHTML packet.
  105.      *
  106.      * @return the number of bodies in the XHTML packet.
  107.      */
  108.     public int getBodiesCount() {
  109.         synchronized (bodies) {
  110.             return bodies.size();
  111.         }
  112.     }

  113.     public static XHTMLExtension from(Message message) {
  114.         return message.getExtension(ELEMENT, NAMESPACE);
  115.     }
  116. }