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