001/**
002 *
003 * Copyright 2016 Fernando Ramirez
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 */
017package org.jivesoftware.smackx.bob.element;
018
019import org.jivesoftware.smack.packet.Message;
020import org.jivesoftware.smack.util.XmlStringBuilder;
021
022import org.jivesoftware.smackx.bob.BoBHash;
023import org.jivesoftware.smackx.xhtmlim.XHTMLText;
024import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension;
025
026/**
027 * Bits of Binary extension element.
028 *
029 * @author Fernando Ramirez
030 * @see <a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of
031 *      Binary</a>
032 */
033public class BoBExtension extends XHTMLExtension {
034
035    private final BoBHash bobHash;
036    private final String alt;
037    private final String paragraph;
038
039    /**
040     * Bits of Binary extension constructor.
041     *
042     * @param bobHash
043     * @param alt
044     * @param paragraph
045     */
046    public BoBExtension(BoBHash bobHash, String alt, String paragraph) {
047        this.bobHash = bobHash;
048        this.alt = alt;
049        this.paragraph = paragraph;
050    }
051
052    /**
053     * Get the BoB hash.
054     *
055     * @return the BoB hash
056     */
057    public BoBHash getBoBHash() {
058        return bobHash;
059    }
060
061    /**
062     * Get the alt field.
063     *
064     * @return the alt field
065     */
066    public String getAlt() {
067        return alt;
068    }
069
070    @Override
071    public XmlStringBuilder toXML(String enclosingNamespace) {
072        XmlStringBuilder xml = new XmlStringBuilder(this);
073        xml.rightAngleBracket();
074
075        xml.halfOpenElement(Message.BODY);
076        xml.xmlnsAttribute(XHTMLText.NAMESPACE);
077        xml.rightAngleBracket();
078
079        xml.openElement(XHTMLText.P);
080        xml.optEscape(paragraph);
081
082        xml.halfOpenElement(XHTMLText.IMG);
083        xml.optAttribute("alt", alt);
084        xml.attribute("src", bobHash.toSrc());
085        xml.closeEmptyElement();
086
087        xml.closeElement(XHTMLText.P);
088        xml.closeElement(Message.BODY);
089        xml.closeElement(this);
090        return xml;
091    }
092
093    public static BoBExtension from(Message message) {
094        return message.getExtension(ELEMENT, NAMESPACE);
095    }
096
097}