aboutsummaryrefslogtreecommitdiff
path: root/src/org/jivesoftware/smackx/pubsub/packet/PubSub.java
blob: 5aa4865a4154f19aa93fca3f2c3c5a586ebce3c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smackx.pubsub.packet;

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.pubsub.PubSubElementType;

/**
 * The standard PubSub extension of an {@link IQ} packet.  This is the topmost
 * element of all pubsub requests and replies as defined in the <a href="http://xmpp.org/extensions/xep-0060">Publish-Subscribe</a> 
 * specification.
 * 
 * @author Robin Collier
 */
public class PubSub extends IQ
{
	private PubSubNamespace ns = PubSubNamespace.BASIC;
	
	/**
    * Returns the XML element name of the extension sub-packet root element.
    *
    * @return the XML element name of the packet extension.
    */
    public String getElementName() {
        return "pubsub";
    }

    /** 
     * Returns the XML namespace of the extension sub-packet root element.
     * According the specification the namespace is 
     * http://jabber.org/protocol/pubsub with a specific fragment depending
     * on the request.  The namespace is defined at <a href="http://xmpp.org/registrar/namespaces.html">XMPP Registrar</a> at
     * 
     * The default value has no fragment.
     * 
     * @return the XML namespace of the packet extension.
     */
    public String getNamespace() 
    {
        return ns.getXmlns();
    }

    /**
     * Set the namespace for the packet if it something other than the default
     * case of {@link PubSubNamespace#BASIC}.  The {@link #getNamespace()} method will return 
     * the result of calling {@link PubSubNamespace#getXmlns()} on the specified enum.
     * 
     * @param ns - The new value for the namespace.
     */
	public void setPubSubNamespace(PubSubNamespace ns)
	{
		this.ns = ns;
	}

	public PacketExtension getExtension(PubSubElementType elem)
	{
		return getExtension(elem.getElementName(), elem.getNamespace().getXmlns());
	}

	/**
	 * Returns the current value of the namespace.  The {@link #getNamespace()} method will return 
     * the result of calling {@link PubSubNamespace#getXmlns()} this value.
	 * 
	 * @return The current value of the namespace.
	 */
	public PubSubNamespace getPubSubNamespace()
	{
		return ns;
	}
    /**
     * Returns the XML representation of a pubsub element according the specification.
     * 
     * The XML representation will be inside of an iq packet like
     * in the following example:
     * <pre>
     * &lt;iq type='set' id="MlIpV-4" to="pubsub.gato.home" from="gato3@gato.home/Smack"&gt;
     *     &lt;pubsub xmlns="http://jabber.org/protocol/pubsub"&gt;
     *                      :
     *         Specific request extension
     *                      :
     *     &lt;/pubsub&gt;
     * &lt;/iq&gt;
     * </pre>
     * 
     */
    public String getChildElementXML() {
        StringBuilder buf = new StringBuilder();
        buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">");
        buf.append(getExtensionsXML());
        buf.append("</").append(getElementName()).append(">");
        return buf.toString();
    }

}