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