001/**
002 *
003 * Copyright © 2009 Jonas Ådahl, 2011-2020 Florian Schmaus
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.caps.packet;
018
019import javax.xml.namespace.QName;
020
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smack.packet.Stanza;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025/**
026 * A XEP-0115 Entity Capabilities extension.
027 */
028public class CapsExtension implements ExtensionElement {
029    public static final String NAMESPACE = "http://jabber.org/protocol/caps";
030    public static final String ELEMENT = "c";
031    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
032
033    private final String node, ver, hash;
034
035    public CapsExtension(String node, String version, String hash) {
036        this.node = node;
037        this.ver = version;
038        this.hash = hash;
039    }
040
041    @Override
042    public String getElementName() {
043        return ELEMENT;
044    }
045
046    @Override
047    public String getNamespace() {
048        return NAMESPACE;
049    }
050
051    public String getNode() {
052        return node;
053    }
054
055    public String getVer() {
056        return ver;
057    }
058
059    public String getHash() {
060        return hash;
061    }
062
063    /**
064     * {@inheritDoc}.
065     *
066     * <pre>
067     *  &lt;c xmlns='http://jabber.org/protocol/caps'
068     *     hash='sha-1'
069     *     node='http://code.google.com/p/exodus'
070     *     ver='QgayPKawpkPSDYmwT/WM94uAlu0='/&gt;
071     * </pre>
072     *
073     */
074    @Override
075    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
076        XmlStringBuilder xml = new XmlStringBuilder(this);
077        xml.attribute("hash", hash).attribute("node", node).attribute("ver", ver);
078        xml.closeEmptyElement();
079        return xml;
080    }
081
082    public static CapsExtension from(Stanza stanza) {
083        return stanza.getExtension(CapsExtension.class);
084    }
085}