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    public enum SubscriptionsNamespace {
029        basic(PubSubElementType.SUBSCRIPTIONS),
030        owner(PubSubElementType.SUBSCRIPTIONS_OWNER),
031        ;
032        public final PubSubElementType type;
033
034        SubscriptionsNamespace(PubSubElementType type) {
035            this.type = type;
036        }
037
038        public static SubscriptionsNamespace fromXmlns(String xmlns) {
039            for (SubscriptionsNamespace subscriptionsNamespace : SubscriptionsNamespace.values()) {
040                if (subscriptionsNamespace.type.getNamespace().getXmlns().equals(xmlns)) {
041                    return subscriptionsNamespace;
042                }
043            }
044            throw new IllegalArgumentException("Invalid Subscription namespace: " + xmlns);
045        }
046    }
047
048    protected List<Subscription> items = Collections.emptyList();
049
050    /**
051     * Subscriptions to the root node.
052     *
053     * @param subList The list of subscriptions
054     */
055    public SubscriptionsExtension(List<Subscription> subList) {
056        this(SubscriptionsNamespace.basic, null, subList);
057    }
058
059    /**
060     * Subscriptions to the specified node.
061     *
062     * @param nodeId The node subscribed to
063     * @param subList The list of subscriptions
064     */
065    public SubscriptionsExtension(String nodeId, List<Subscription> subList) {
066        this(SubscriptionsNamespace.basic, nodeId, subList);
067    }
068
069    /**
070     * Subscriptions to the specified node.
071     *
072     * @param subscriptionsNamespace the namespace used by this element
073     * @param nodeId The node subscribed to
074     * @param subList The list of subscriptions
075     * @since 4.3
076     */
077    public SubscriptionsExtension(SubscriptionsNamespace subscriptionsNamespace, String nodeId, List<Subscription> subList) {
078        super(subscriptionsNamespace.type, nodeId);
079
080        if (subList != null)
081            items = subList;
082    }
083
084    /**
085     * Gets the list of subscriptions.
086     *
087     * @return List of subscriptions
088     */
089    public List<Subscription> getSubscriptions() {
090        return items;
091    }
092
093    @Override
094    public CharSequence toXML(String enclosingNamespace) {
095        if ((items == null) || (items.size() == 0)) {
096            return super.toXML(enclosingNamespace);
097        }
098        else {
099            StringBuilder builder = new StringBuilder("<");
100            builder.append(getElementName());
101
102            if (getNode() != null) {
103                builder.append(" node='");
104                builder.append(getNode());
105                builder.append('\'');
106            }
107            builder.append('>');
108
109            for (Subscription item : items) {
110                builder.append(item.toXML(null));
111            }
112
113            builder.append("</");
114            builder.append(getElementName());
115            builder.append('>');
116            return builder.toString();
117        }
118    }
119}