NodeExtension.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.pubsub;

  18. import org.jivesoftware.smack.packet.ExtensionElement;

  19. /**
  20.  * A class which represents a common element within the pubsub defined
  21.  * schemas.  One which has a <b>node</b> as an attribute.  This class is
  22.  * used on its own as well as a base class for many others, since the
  23.  * node is a central concept to most pubsub functionality.
  24.  *
  25.  * @author Robin Collier
  26.  */
  27. public class NodeExtension implements ExtensionElement
  28. {
  29.     private final PubSubElementType element;
  30.     private final String node;
  31.    
  32.     /**
  33.      * Constructs a <tt>NodeExtension</tt> with an element name specified
  34.      * by {@link PubSubElementType} and the specified node id.
  35.      *
  36.      * @param elem Defines the element name and namespace
  37.      * @param nodeId Specifies the id of the node
  38.      */
  39.     public NodeExtension(PubSubElementType elem, String nodeId)
  40.     {
  41.         element = elem;
  42.         this.node = nodeId;
  43.     }

  44.     /**
  45.      * Constructs a <tt>NodeExtension</tt> with an element name specified
  46.      * by {@link PubSubElementType}.
  47.      *
  48.      * @param elem Defines the element name and namespace
  49.      */
  50.     public NodeExtension(PubSubElementType elem)
  51.     {
  52.         this(elem, null);
  53.     }

  54.     /**
  55.      * Gets the node id
  56.      *
  57.      * @return The node id
  58.      */
  59.     public String getNode()
  60.     {
  61.         return node;
  62.     }
  63.    
  64.     public String getElementName()
  65.     {
  66.         return element.getElementName();
  67.     }

  68.     public String getNamespace()
  69.     {
  70.         return element.getNamespace().getXmlns();
  71.     }

  72.     public CharSequence toXML()
  73.     {
  74.         return '<' + getElementName() + (node == null ? "" : " node='" + node + '\'') + "/>";
  75.     }

  76.     @Override
  77.     public String toString()
  78.     {
  79.         return getClass().getName() + " - content [" + toXML() + "]";
  80.     }
  81. }