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