SubscriptionsExtension.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 java.util.Collections;
  19. import java.util.List;

  20. import org.jivesoftware.smack.util.XmlStringBuilder;

  21. /**
  22.  * Represents the element holding the list of subscription elements.
  23.  *
  24.  * @author Robin Collier
  25.  */
  26. public class SubscriptionsExtension extends NodeExtension {
  27.     public enum SubscriptionsNamespace {
  28.         basic(PubSubElementType.SUBSCRIPTIONS),
  29.         owner(PubSubElementType.SUBSCRIPTIONS_OWNER),
  30.         ;
  31.         public final PubSubElementType type;

  32.         SubscriptionsNamespace(PubSubElementType type) {
  33.             this.type = type;
  34.         }

  35.         public static SubscriptionsNamespace fromXmlns(String xmlns) {
  36.             for (SubscriptionsNamespace subscriptionsNamespace : SubscriptionsNamespace.values()) {
  37.                 if (subscriptionsNamespace.type.getNamespace().getXmlns().equals(xmlns)) {
  38.                     return subscriptionsNamespace;
  39.                 }
  40.             }
  41.             throw new IllegalArgumentException("Invalid Subscription namespace: " + xmlns);
  42.         }
  43.     }

  44.     protected List<Subscription> items = Collections.emptyList();

  45.     /**
  46.      * Subscriptions to the root node.
  47.      *
  48.      * @param subList The list of subscriptions
  49.      */
  50.     public SubscriptionsExtension(List<Subscription> subList) {
  51.         this(SubscriptionsNamespace.basic, null, subList);
  52.     }

  53.     /**
  54.      * Subscriptions to the specified node.
  55.      *
  56.      * @param nodeId The node subscribed to
  57.      * @param subList The list of subscriptions
  58.      */
  59.     public SubscriptionsExtension(String nodeId, List<Subscription> subList) {
  60.         this(SubscriptionsNamespace.basic, nodeId, subList);
  61.     }

  62.     /**
  63.      * Subscriptions to the specified node.
  64.      *
  65.      * @param subscriptionsNamespace the namespace used by this element
  66.      * @param nodeId The node subscribed to
  67.      * @param subList The list of subscriptions
  68.      * @since 4.3
  69.      */
  70.     public SubscriptionsExtension(SubscriptionsNamespace subscriptionsNamespace, String nodeId, List<Subscription> subList) {
  71.         super(subscriptionsNamespace.type, nodeId);

  72.         if (subList != null)
  73.             items = subList;
  74.     }

  75.     /**
  76.      * Gets the list of subscriptions.
  77.      *
  78.      * @return List of subscriptions
  79.      */
  80.     public List<Subscription> getSubscriptions() {
  81.         return items;
  82.     }

  83.     @Override
  84.     protected void addXml(XmlStringBuilder xml) {
  85.         if ((items == null) || (items.size() == 0)) {
  86.             xml.closeEmptyElement();
  87.             return;
  88.         }
  89.         xml.rightAngleBracket();
  90.         xml.append(items);
  91.         xml.closeElement(this);
  92.     }
  93. }