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.Stanza;
020import org.jivesoftware.smack.packet.ExtensionElement;
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    public String getElementName() {
039        return ELEMENT;
040    }
041
042    public String getNamespace() {
043        return NAMESPACE;
044    }
045
046    public String getNode() {
047        return node;
048    }
049
050    public String getVer() {
051        return ver;
052    }
053
054    public String getHash() {
055        return hash;
056    }
057
058    /**
059     * <pre>
060     *  <c xmlns='http://jabber.org/protocol/caps'
061     *     hash='sha-1'
062     *     node='http://code.google.com/p/exodus'
063     *     ver='QgayPKawpkPSDYmwT/WM94uAlu0='/>
064     * </pre>
065     *
066     */
067    @Override
068    public XmlStringBuilder toXML() {
069        XmlStringBuilder xml = new XmlStringBuilder(this);
070        xml.attribute("hash", hash).attribute("node", node).attribute("ver", ver);
071        xml.closeEmptyElement();
072        return xml;
073    }
074
075    public static CapsExtension from(Stanza stanza) {
076        return stanza.getExtension(ELEMENT, NAMESPACE);
077    }
078}