Home | History | Annotate | Download | only in pubsub
      1 /**
      2  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      3  * you may not use this file except in compliance with the License.
      4  * You may obtain a copy of the License at
      5  *
      6  *     http://www.apache.org/licenses/LICENSE-2.0
      7  *
      8  * Unless required by applicable law or agreed to in writing, software
      9  * distributed under the License is distributed on an "AS IS" BASIS,
     10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     11  * See the License for the specific language governing permissions and
     12  * limitations under the License.
     13  */
     14 package org.jivesoftware.smackx.pubsub;
     15 
     16 import java.util.Collections;
     17 import java.util.List;
     18 
     19 /**
     20  * Represents the element holding the list of subscription elements.
     21  *
     22  * @author Robin Collier
     23  */
     24 public class SubscriptionsExtension extends NodeExtension
     25 {
     26 	protected List<Subscription> items = Collections.EMPTY_LIST;
     27 
     28 	/**
     29 	 * Subscriptions to the root node
     30 	 *
     31 	 * @param subList The list of subscriptions
     32 	 */
     33 	public SubscriptionsExtension(List<Subscription> subList)
     34 	{
     35 		super(PubSubElementType.SUBSCRIPTIONS);
     36 
     37 		if (subList != null)
     38 			items = subList;
     39 	}
     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 
     51 		if (subList != null)
     52 			items = subList;
     53 	}
     54 
     55 	/**
     56 	 * Gets the list of subscriptions.
     57 	 *
     58 	 * @return List of subscriptions
     59 	 */
     60 	public List<Subscription> getSubscriptions()
     61 	{
     62 		return items;
     63 	}
     64 
     65 	@Override
     66 	public String toXML()
     67 	{
     68 		if ((items == null) || (items.size() == 0))
     69 		{
     70 			return super.toXML();
     71 		}
     72 		else
     73 		{
     74 			StringBuilder builder = new StringBuilder("<");
     75 			builder.append(getElementName());
     76 
     77 			if (getNode() != null)
     78 			{
     79 				builder.append(" node='");
     80 				builder.append(getNode());
     81 				builder.append("'");
     82 			}
     83 			builder.append(">");
     84 
     85 			for (Subscription item : items)
     86 			{
     87 				builder.append(item.toXML());
     88 			}
     89 
     90 			builder.append("</");
     91 			builder.append(getElementName());
     92 			builder.append(">");
     93 			return builder.toString();
     94 		}
     95 	}
     96 }
     97