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 java.util.Collections;
020import java.util.List;
021
022/**
023 * Represents the element holding the list of subscription elements.
024 * 
025 * @author Robin Collier
026 */
027public class SubscriptionsExtension extends NodeExtension
028{
029    protected List<Subscription> items = Collections.emptyList();
030
031    /**
032     * Subscriptions to the root node.
033     * 
034     * @param subList The list of subscriptions
035     */
036    public SubscriptionsExtension(List<Subscription> subList)
037    {
038        super(PubSubElementType.SUBSCRIPTIONS);
039
040        if (subList != null)
041            items = subList;
042    }
043
044    /**
045     * Subscriptions to the specified node.
046     * 
047     * @param nodeId The node subscribed to
048     * @param subList The list of subscriptions
049     */
050    public SubscriptionsExtension(String nodeId, List<Subscription> subList)
051    {
052        super(PubSubElementType.SUBSCRIPTIONS, nodeId);
053
054        if (subList != null)
055            items = subList;
056    }
057
058    /**
059     * Gets the list of subscriptions.
060     * 
061     * @return List of subscriptions
062     */
063    public List<Subscription> getSubscriptions()
064    {
065        return items;
066    }
067
068    @Override
069    public CharSequence toXML()
070    {
071        if ((items == null) || (items.size() == 0))
072        {
073            return super.toXML();
074        }
075        else
076        {
077            StringBuilder builder = new StringBuilder("<");
078            builder.append(getElementName());
079
080            if (getNode() != null)
081            {
082                builder.append(" node='");
083                builder.append(getNode());
084                builder.append('\'');
085            }
086            builder.append('>');
087
088            for (Subscription item : items)
089            {
090                builder.append(item.toXML());
091            }
092
093            builder.append("</");
094            builder.append(getElementName());
095            builder.append('>');
096            return builder.toString();
097        }
098    }
099}