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.XmlElement;
  19. import org.jivesoftware.smack.packet.XmlEnvironment;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;

  21. import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;

  22. /**
  23.  * A class which represents a common element within the pubsub defined
  24.  * schemas.  One which has a <b>node</b> as an attribute.  This class is
  25.  * used on its own as well as a base class for many others, since the
  26.  * node is a central concept to most pubsub functionality.
  27.  *
  28.  * @author Robin Collier
  29.  */
  30. public class NodeExtension implements XmlElement {
  31.     private final PubSubElementType element;
  32.     private final String node;

  33.     /**
  34.      * Constructs a <code>NodeExtension</code> with an element name specified
  35.      * by {@link PubSubElementType} and the specified node id.
  36.      *
  37.      * @param elem Defines the element name and namespace
  38.      * @param nodeId Specifies the id of the node
  39.      */
  40.     public NodeExtension(PubSubElementType elem, String nodeId) {
  41.         element = elem;
  42.         this.node = nodeId;
  43.     }

  44.     /**
  45.      * Constructs a <code>NodeExtension</code> 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.         this(elem, null);
  52.     }

  53.     /**
  54.      * Gets the node id.
  55.      *
  56.      * @return The node id
  57.      */
  58.     public String getNode() {
  59.         return node;
  60.     }

  61.     @Override
  62.     public String getElementName() {
  63.         return element.getElementName();
  64.     }

  65.     public PubSubNamespace getPubSubNamespace() {
  66.         return element.getNamespace();
  67.     }

  68.     @Override
  69.     public final String getNamespace() {
  70.         return getPubSubNamespace().getXmlns();
  71.     }

  72.     @Override
  73.     public final XmlStringBuilder toXML(XmlEnvironment enclosingNamespace) {
  74.         XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
  75.         xml.optAttribute("node", node);

  76.         addXml(xml);

  77.         return xml;
  78.     }

  79.     protected void addXml(XmlStringBuilder xml) {
  80.         xml.closeEmptyElement();
  81.     }

  82.     @Override
  83.     public String toString() {
  84.         return getClass().getName() + " - content [" + toXML() + "]";
  85.     }
  86. }