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 java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import javax.xml.namespace.QName;

  22. import org.jivesoftware.smack.packet.ExtensionElement;
  23. import org.jivesoftware.smack.packet.MessageView;
  24. import org.jivesoftware.smack.util.XmlStringBuilder;

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

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

  38.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

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

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

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

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

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

  97.     /**
  98.      * Adds a body to the packet.
  99.      *
  100.      * @param body the body to add.
  101.      */
  102.     public void addBody(CharSequence body) {
  103.         synchronized (bodies) {
  104.             bodies.add(body);
  105.         }
  106.     }

  107.     /**
  108.      * Returns a count of the bodies in the XHTML packet.
  109.      *
  110.      * @return the number of bodies in the XHTML packet.
  111.      */
  112.     public int getBodiesCount() {
  113.         synchronized (bodies) {
  114.             return bodies.size();
  115.         }
  116.     }

  117.     public static XHTMLExtension from(MessageView message) {
  118.         return message.getExtension(XHTMLExtension.class);
  119.     }
  120. }