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; 018 019import org.jivesoftware.smack.packet.ExtensionElement; 020import org.jivesoftware.smack.packet.XmlEnvironment; 021import org.jivesoftware.smack.util.XmlStringBuilder; 022 023import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace; 024 025/** 026 * A class which represents a common element within the pubsub defined 027 * schemas. One which has a <b>node</b> as an attribute. This class is 028 * used on its own as well as a base class for many others, since the 029 * node is a central concept to most pubsub functionality. 030 * 031 * @author Robin Collier 032 */ 033public class NodeExtension implements ExtensionElement { 034 private final PubSubElementType element; 035 private final String node; 036 037 /** 038 * Constructs a <code>NodeExtension</code> with an element name specified 039 * by {@link PubSubElementType} and the specified node id. 040 * 041 * @param elem Defines the element name and namespace 042 * @param nodeId Specifies the id of the node 043 */ 044 public NodeExtension(PubSubElementType elem, String nodeId) { 045 element = elem; 046 this.node = nodeId; 047 } 048 049 /** 050 * Constructs a <code>NodeExtension</code> with an element name specified 051 * by {@link PubSubElementType}. 052 * 053 * @param elem Defines the element name and namespace 054 */ 055 public NodeExtension(PubSubElementType elem) { 056 this(elem, null); 057 } 058 059 /** 060 * Gets the node id. 061 * 062 * @return The node id 063 */ 064 public String getNode() { 065 return node; 066 } 067 068 @Override 069 public String getElementName() { 070 return element.getElementName(); 071 } 072 073 public PubSubNamespace getPubSubNamespace() { 074 return element.getNamespace(); 075 } 076 077 @Override 078 public final String getNamespace() { 079 return getPubSubNamespace().getXmlns(); 080 } 081 082 @Override 083 public final XmlStringBuilder toXML(XmlEnvironment enclosingNamespace) { 084 XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace); 085 xml.optAttribute("node", node); 086 087 addXml(xml); 088 089 return xml; 090 } 091 092 protected void addXml(XmlStringBuilder xml) { 093 xml.closeEmptyElement(); 094 } 095 096 @Override 097 public String toString() { 098 return getClass().getName() + " - content [" + toXML() + "]"; 099 } 100}