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
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024/**
025 * Represents the element holding the list of subscription elements.
026 *
027 * @author Robin Collier
028 */
029public class SubscriptionsExtension extends NodeExtension {
030    public enum SubscriptionsNamespace {
031        basic(PubSubElementType.SUBSCRIPTIONS),
032        owner(PubSubElementType.SUBSCRIPTIONS_OWNER),
033        ;
034        public final PubSubElementType type;
035
036        SubscriptionsNamespace(PubSubElementType type) {
037            this.type = type;
038        }
039
040        public static SubscriptionsNamespace fromXmlns(String xmlns) {
041            for (SubscriptionsNamespace subscriptionsNamespace : SubscriptionsNamespace.values()) {
042                if (subscriptionsNamespace.type.getNamespace().getXmlns().equals(xmlns)) {
043                    return subscriptionsNamespace;
044                }
045            }
046            throw new IllegalArgumentException("Invalid Subscription namespace: " + xmlns);
047        }
048    }
049
050    protected List<Subscription> items = Collections.emptyList();
051
052    /**
053     * Subscriptions to the root node.
054     *
055     * @param subList The list of subscriptions
056     */
057    public SubscriptionsExtension(List<Subscription> subList) {
058        this(SubscriptionsNamespace.basic, null, subList);
059    }
060
061    /**
062     * Subscriptions to the specified node.
063     *
064     * @param nodeId The node subscribed to
065     * @param subList The list of subscriptions
066     */
067    public SubscriptionsExtension(String nodeId, List<Subscription> subList) {
068        this(SubscriptionsNamespace.basic, nodeId, subList);
069    }
070
071    /**
072     * Subscriptions to the specified node.
073     *
074     * @param subscriptionsNamespace the namespace used by this element
075     * @param nodeId The node subscribed to
076     * @param subList The list of subscriptions
077     * @since 4.3
078     */
079    public SubscriptionsExtension(SubscriptionsNamespace subscriptionsNamespace, String nodeId, List<Subscription> subList) {
080        super(subscriptionsNamespace.type, nodeId);
081
082        if (subList != null)
083            items = subList;
084    }
085
086    /**
087     * Gets the list of subscriptions.
088     *
089     * @return List of subscriptions
090     */
091    public List<Subscription> getSubscriptions() {
092        return items;
093    }
094
095    @Override
096    protected void addXml(XmlStringBuilder xml) {
097        if ((items == null) || (items.size() == 0)) {
098            xml.closeEmptyElement();
099            return;
100        }
101        xml.rightAngleBracket();
102        xml.append(items);
103        xml.closeElement(this);
104    }
105}