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.ExtensionElement;
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        public PubSub() {
036        super(ELEMENT, NAMESPACE);
037        }
038
039        public PubSub(PubSubNamespace ns) {
040        super(ELEMENT, ns.getXmlns());
041    }
042
043    public PubSub(String to, Type type, PubSubNamespace ns) {
044        super(ELEMENT, (ns == null ? PubSubNamespace.BASIC : ns).getXmlns());
045        setTo(to);
046        setType(type);
047    }
048
049        /**
050    * Returns the XML element name of the extension sub-packet root element.
051    *
052    * @return the XML element name of the stanza(/packet) extension.
053    */
054    public String getElementName() {
055        return ELEMENT;
056    }
057
058    @SuppressWarnings("unchecked")
059    public <PE extends ExtensionElement> PE getExtension(PubSubElementType elem)
060        {
061                return (PE) getExtension(elem.getElementName(), elem.getNamespace().getXmlns());
062        }
063
064    /**
065     * Returns the XML representation of a pubsub element according the specification.
066     * 
067     * The XML representation will be inside of an iq stanza(/packet) like
068     * in the following example:
069     * <pre>
070     * &lt;iq type='set' id="MlIpV-4" to="pubsub.gato.home" from="gato3@gato.home/Smack"&gt;
071     *     &lt;pubsub xmlns="http://jabber.org/protocol/pubsub"&gt;
072     *                      :
073     *         Specific request extension
074     *                      :
075     *     &lt;/pubsub&gt;
076     * &lt;/iq&gt;
077     * </pre>
078     * 
079     */
080    @Override
081    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
082        // N.B. We could use SimpleIQ here, but PubSub IQs will nearly *always* have packet extensions, which means that
083        // SimpleIQs xml.setEmptyElement() is counter-productive in this case and we use xml.rightAngleBracket()
084        // instead, as there are likely sub-elements to follow.
085        xml.rightAngleBracket();
086        return xml;
087    }
088
089    public static PubSub createPubsubPacket(String to, Type type, ExtensionElement extension, PubSubNamespace ns) {
090        PubSub pubSub = new PubSub(to, type, ns);
091        pubSub.addExtension(extension);
092        return pubSub;
093    }
094}