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 org.jivesoftware.smack.packet.ExtensionElement; 025import org.jivesoftware.smack.packet.Message; 026import org.jivesoftware.smack.util.XmlStringBuilder; 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 final List<CharSequence> bodies = new ArrayList<>(); 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 extension. 050 */ 051 @Override 052 public String getElementName() { 053 return ELEMENT; 054 } 055 056 /** 057 * Returns the XML namespace of the extension sub-packet root element. 058 * According the specification the namespace is always "http://jabber.org/protocol/xhtml-im" 059 * 060 * @return the XML namespace of the stanza extension. 061 */ 062 @Override 063 public String getNamespace() { 064 return NAMESPACE; 065 } 066 067 /** 068 * Returns the XML representation of a XHTML extension according the specification. 069 * 070 * Usually the XML representation will be inside of a Message XML representation like 071 * in the following example: 072 * <pre> 073 * <message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"> 074 * <subject>Any subject you want</subject> 075 * <body>This message contains something interesting.</body> 076 * <html xmlns="http://jabber.org/protocol/xhtml-im"> 077 * <body><p style='font-size:large'>This message contains something <em>interesting</em>.</p></body> 078 * </html> 079 * </message> 080 * </pre> 081 * 082 */ 083 @Override 084 public XmlStringBuilder toXML(String enclosingNamespace) { 085 XmlStringBuilder xml = new XmlStringBuilder(this); 086 xml.rightAngleBracket(); 087 // Loop through all the bodies and append them to the string buffer 088 for (CharSequence body : getBodies()) { 089 xml.append(body); 090 } 091 xml.closeElement(this); 092 return xml; 093 } 094 095 /** 096 * Returns a List of the bodies in the packet. 097 * 098 * @return a List of the bodies in the packet. 099 */ 100 public List<CharSequence> getBodies() { 101 synchronized (bodies) { 102 return Collections.unmodifiableList(new ArrayList<>(bodies)); 103 } 104 } 105 106 /** 107 * Adds a body to the packet. 108 * 109 * @param body the body to add. 110 */ 111 public void addBody(CharSequence body) { 112 synchronized (bodies) { 113 bodies.add(body); 114 } 115 } 116 117 /** 118 * Returns a count of the bodies in the XHTML packet. 119 * 120 * @return the number of bodies in the XHTML packet. 121 */ 122 public int getBodiesCount() { 123 synchronized (bodies) { 124 return bodies.size(); 125 } 126 } 127 128 public static XHTMLExtension from(Message message) { 129 return message.getExtension(ELEMENT, NAMESPACE); 130 } 131}