1 /** 2 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 3 * you may not use this file except in compliance with the License. 4 * You may obtain a copy of the License at 5 * 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 package org.jivesoftware.smackx.pubsub.packet; 15 16 import org.jivesoftware.smack.packet.IQ; 17 import org.jivesoftware.smack.packet.PacketExtension; 18 import org.jivesoftware.smackx.pubsub.PubSubElementType; 19 20 /** 21 * The standard PubSub extension of an {@link IQ} packet. This is the topmost 22 * element of all pubsub requests and replies as defined in the <a href="http://xmpp.org/extensions/xep-0060">Publish-Subscribe</a> 23 * specification. 24 * 25 * @author Robin Collier 26 */ 27 public class PubSub extends IQ 28 { 29 private PubSubNamespace ns = PubSubNamespace.BASIC; 30 31 /** 32 * Returns the XML element name of the extension sub-packet root element. 33 * 34 * @return the XML element name of the packet extension. 35 */ 36 public String getElementName() { 37 return "pubsub"; 38 } 39 40 /** 41 * Returns the XML namespace of the extension sub-packet root element. 42 * According the specification the namespace is 43 * http://jabber.org/protocol/pubsub with a specific fragment depending 44 * on the request. The namespace is defined at <a href="http://xmpp.org/registrar/namespaces.html">XMPP Registrar</a> at 45 * 46 * The default value has no fragment. 47 * 48 * @return the XML namespace of the packet extension. 49 */ 50 public String getNamespace() 51 { 52 return ns.getXmlns(); 53 } 54 55 /** 56 * Set the namespace for the packet if it something other than the default 57 * case of {@link PubSubNamespace#BASIC}. The {@link #getNamespace()} method will return 58 * the result of calling {@link PubSubNamespace#getXmlns()} on the specified enum. 59 * 60 * @param ns - The new value for the namespace. 61 */ 62 public void setPubSubNamespace(PubSubNamespace ns) 63 { 64 this.ns = ns; 65 } 66 67 public PacketExtension getExtension(PubSubElementType elem) 68 { 69 return getExtension(elem.getElementName(), elem.getNamespace().getXmlns()); 70 } 71 72 /** 73 * Returns the current value of the namespace. The {@link #getNamespace()} method will return 74 * the result of calling {@link PubSubNamespace#getXmlns()} this value. 75 * 76 * @return The current value of the namespace. 77 */ 78 public PubSubNamespace getPubSubNamespace() 79 { 80 return ns; 81 } 82 /** 83 * Returns the XML representation of a pubsub element according the specification. 84 * 85 * The XML representation will be inside of an iq packet like 86 * in the following example: 87 * <pre> 88 * <iq type='set' id="MlIpV-4" to="pubsub.gato.home" from="gato3 (at) gato.home/Smack"> 89 * <pubsub xmlns="http://jabber.org/protocol/pubsub"> 90 * : 91 * Specific request extension 92 * : 93 * </pubsub> 94 * </iq> 95 * </pre> 96 * 97 */ 98 public String getChildElementXML() { 99 StringBuilder buf = new StringBuilder(); 100 buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">"); 101 buf.append(getExtensionsXML()); 102 buf.append("</").append(getElementName()).append(">"); 103 return buf.toString(); 104 } 105 106 } 107