001/**
002 *
003 * Copyright the original author or authors
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.pubsub.packet;
018
019import org.jivesoftware.smack.packet.IQ;
020import org.jivesoftware.smack.packet.PacketExtension;
021import org.jivesoftware.smackx.pubsub.PubSubElementType;
022
023/**
024 * The standard PubSub extension of an {@link IQ} packet.  This is the topmost
025 * element of all pubsub requests and replies as defined in the <a href="http://xmpp.org/extensions/xep-0060">Publish-Subscribe</a> 
026 * specification.
027 * 
028 * @author Robin Collier
029 */
030public class PubSub extends IQ
031{
032    public static final String ELEMENT = "pubsub";
033    public static final String NAMESPACE = "http://jabber.org/protocol/pubsub";
034
035        private PubSubNamespace ns = PubSubNamespace.BASIC;
036
037        public PubSub() {
038        }
039
040        public PubSub(String to, Type type) {
041        setTo(to);
042        setType(type);
043    }
044
045    public PubSub(String to, Type type, PubSubNamespace ns) {
046        this(to, type);
047        if (ns != null) {
048            setPubSubNamespace(ns);
049        }
050    }
051
052        /**
053    * Returns the XML element name of the extension sub-packet root element.
054    *
055    * @return the XML element name of the packet extension.
056    */
057    public String getElementName() {
058        return ELEMENT;
059    }
060
061    /** 
062     * Returns the XML namespace of the extension sub-packet root element.
063     * According the specification the namespace is 
064     * http://jabber.org/protocol/pubsub with a specific fragment depending
065     * on the request.  The namespace is defined at <a href="http://xmpp.org/registrar/namespaces.html">XMPP Registrar</a> at
066     * 
067     * The default value has no fragment.
068     * 
069     * @return the XML namespace of the packet extension.
070     */
071    public String getNamespace() 
072    {
073        return ns.getXmlns();
074    }
075
076    /**
077     * Set the namespace for the packet if it something other than the default
078     * case of {@link PubSubNamespace#BASIC}.  The {@link #getNamespace()} method will return 
079     * the result of calling {@link PubSubNamespace#getXmlns()} on the specified enum.
080     * 
081     * @param ns - The new value for the namespace.
082     */
083        public void setPubSubNamespace(PubSubNamespace ns)
084        {
085                this.ns = ns;
086        }
087
088        public PacketExtension getExtension(PubSubElementType elem)
089        {
090                return getExtension(elem.getElementName(), elem.getNamespace().getXmlns());
091        }
092
093        /**
094         * Returns the current value of the namespace.  The {@link #getNamespace()} method will return 
095     * the result of calling {@link PubSubNamespace#getXmlns()} this value.
096         * 
097         * @return The current value of the namespace.
098         */
099        public PubSubNamespace getPubSubNamespace()
100        {
101                return ns;
102        }
103    /**
104     * Returns the XML representation of a pubsub element according the specification.
105     * 
106     * The XML representation will be inside of an iq packet like
107     * in the following example:
108     * <pre>
109     * &lt;iq type='set' id="MlIpV-4" to="pubsub.gato.home" from="gato3@gato.home/Smack"&gt;
110     *     &lt;pubsub xmlns="http://jabber.org/protocol/pubsub"&gt;
111     *                      :
112     *         Specific request extension
113     *                      :
114     *     &lt;/pubsub&gt;
115     * &lt;/iq&gt;
116     * </pre>
117     * 
118     */
119    public String getChildElementXML() {
120        StringBuilder buf = new StringBuilder();
121        buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">");
122        buf.append(getExtensionsXML());
123        buf.append("</").append(getElementName()).append(">");
124        return buf.toString();
125    }
126
127    public static PubSub createPubsubPacket(String to, Type type, PacketExtension extension, PubSubNamespace ns) {
128        PubSub pubSub = new PubSub(to, type, ns);
129        pubSub.addExtension(extension);
130        return pubSub;
131    }
132}