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. /**
  21.  * Represents the element holding the list of subscription elements.
  22.  *
  23.  * @author Robin Collier
  24.  */
  25. public class SubscriptionsExtension extends NodeExtension
  26. {
  27.     protected List<Subscription> items = Collections.emptyList();
  28.    
  29.     /**
  30.      * Subscriptions to the root node
  31.      *
  32.      * @param subList The list of subscriptions
  33.      */
  34.     public SubscriptionsExtension(List<Subscription> subList)
  35.     {
  36.         super(PubSubElementType.SUBSCRIPTIONS);
  37.        
  38.         if (subList != null)
  39.             items = subList;
  40.     }

  41.     /**
  42.      * Subscriptions to the specified node.
  43.      *
  44.      * @param nodeId The node subscribed to
  45.      * @param subList The list of subscriptions
  46.      */
  47.     public SubscriptionsExtension(String nodeId, List<Subscription> subList)
  48.     {
  49.         super(PubSubElementType.SUBSCRIPTIONS, nodeId);

  50.         if (subList != null)
  51.             items = subList;
  52.     }

  53.     /**
  54.      * Gets the list of subscriptions.
  55.      *
  56.      * @return List of subscriptions
  57.      */
  58.     public List<Subscription> getSubscriptions()
  59.     {
  60.         return items;
  61.     }

  62.     @Override
  63.     public CharSequence toXML()
  64.     {
  65.         if ((items == null) || (items.size() == 0))
  66.         {
  67.             return super.toXML();
  68.         }
  69.         else
  70.         {
  71.             StringBuilder builder = new StringBuilder("<");
  72.             builder.append(getElementName());
  73.            
  74.             if (getNode() != null)
  75.             {
  76.                 builder.append(" node='");
  77.                 builder.append(getNode());
  78.                 builder.append("'");
  79.             }
  80.             builder.append(">");
  81.            
  82.             for (Subscription item : items)
  83.             {
  84.                 builder.append(item.toXML());
  85.             }
  86.            
  87.             builder.append("</");
  88.             builder.append(getElementName());
  89.             builder.append(">");
  90.             return builder.toString();
  91.         }
  92.     }
  93. }