ItemsExtension.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.List;

  19. import org.jivesoftware.smack.packet.ExtensionElement;

  20. /**
  21.  * This class is used to for multiple purposes.  
  22.  * <li>It can represent an event containing a list of items that have been published
  23.  * <li>It can represent an event containing a list of retracted (deleted) items.
  24.  * <li>It can represent a request to delete a list of items.
  25.  * <li>It can represent a request to get existing items.
  26.  *
  27.  * <p><b>Please note, this class is used for internal purposes, and is not required for usage of
  28.  * pubsub functionality.</b>
  29.  *
  30.  * @author Robin Collier
  31.  */
  32. public class ItemsExtension extends NodeExtension implements EmbeddedPacketExtension
  33. {
  34.     protected ItemsElementType type;
  35.     protected Boolean notify;
  36.     protected List<? extends ExtensionElement> items;

  37.     public enum ItemsElementType
  38.     {
  39.         /** An items element, which has an optional <b>max_items</b> attribute when requesting items */
  40.         items(PubSubElementType.ITEMS, "max_items"),
  41.        
  42.         /** A retract element, which has an optional <b>notify</b> attribute when publishing deletions */
  43.         retract(PubSubElementType.RETRACT, "notify");
  44.        
  45.         private PubSubElementType elem;
  46.         private String att;
  47.        
  48.         private ItemsElementType(PubSubElementType nodeElement, String attribute)
  49.         {
  50.             elem = nodeElement;
  51.             att = attribute;
  52.         }
  53.        
  54.         public PubSubElementType getNodeElement()
  55.         {
  56.             return elem;
  57.         }

  58.         public String getElementAttribute()
  59.         {
  60.             return att;
  61.         }
  62.     }

  63.     /**
  64.      * Construct an instance with a list representing items that have been published or deleted.
  65.      *
  66.      * <p>Valid scenarios are:
  67.      * <li>Request items from node - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and an
  68.      * optional value for the <b>max_items</b> attribute.
  69.      * <li>Request to delete items - itemsType = {@link ItemsElementType#retract}, items = list of {@link Item} containing
  70.      * only id's and an optional value for the <b>notify</b> attribute.
  71.      * <li>Items published event - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and
  72.      * attributeValue = <code>null</code>
  73.      * <li>Items deleted event -  itemsType = {@link ItemsElementType#items}, items = list of {@link RetractItem} and
  74.      * attributeValue = <code>null</code>
  75.      *
  76.      * @param itemsType Type of representation
  77.      * @param nodeId The node to which the items are being sent or deleted
  78.      * @param items The list of {@link Item} or {@link RetractItem}
  79.      */
  80.     public ItemsExtension(ItemsElementType itemsType, String nodeId, List<? extends ExtensionElement> items)
  81.     {
  82.         super(itemsType.getNodeElement(), nodeId);
  83.         type = itemsType;
  84.         this.items = items;
  85.     }
  86.    
  87.     /**
  88.      * Construct an instance with a list representing items that have been published or deleted.
  89.      *
  90.      * <p>Valid scenarios are:
  91.      * <li>Request items from node - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and an
  92.      * optional value for the <b>max_items</b> attribute.
  93.      * <li>Request to delete items - itemsType = {@link ItemsElementType#retract}, items = list of {@link Item} containing
  94.      * only id's and an optional value for the <b>notify</b> attribute.
  95.      * <li>Items published event - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and
  96.      * attributeValue = <code>null</code>
  97.      * <li>Items deleted event -  itemsType = {@link ItemsElementType#items}, items = list of {@link RetractItem} and
  98.      * attributeValue = <code>null</code>
  99.      *
  100.      * @param nodeId The node to which the items are being sent or deleted
  101.      * @param items The list of {@link Item} or {@link RetractItem}
  102.      */
  103.     public ItemsExtension(String nodeId, List<? extends ExtensionElement> items, boolean notify)
  104.     {
  105.         super(ItemsElementType.retract.getNodeElement(), nodeId);
  106.         type = ItemsElementType.retract;
  107.         this.items = items;
  108.         this.notify = notify;
  109.     }
  110.    
  111.     /**
  112.      * Get the type of element
  113.      *
  114.      * @return The element type
  115.      */
  116.     public ItemsElementType getItemsElementType()
  117.     {
  118.         return type;
  119.     }
  120.    
  121.     @SuppressWarnings("unchecked")
  122.     public List<ExtensionElement> getExtensions()
  123.     {
  124.         return (List<ExtensionElement>)getItems();
  125.     }
  126.    
  127.     /**
  128.      * Gets the items related to the type of request or event.
  129.      *
  130.      * return List of {@link Item}, {@link RetractItem}, or null
  131.      */
  132.     public List<? extends ExtensionElement> getItems()
  133.     {
  134.         return items;
  135.     }

  136.     /**
  137.      * Gets the value of the optional attribute related to the {@link ItemsElementType}.
  138.      *
  139.      * @return The attribute value
  140.      */
  141.     public boolean getNotify()
  142.     {
  143.         return notify;
  144.     }
  145.    
  146.     @Override
  147.     public CharSequence toXML()
  148.     {
  149.         if ((items == null) || (items.size() == 0))
  150.         {
  151.             return super.toXML();
  152.         }
  153.         else
  154.         {
  155.             StringBuilder builder = new StringBuilder("<");
  156.             builder.append(getElementName());
  157.             builder.append(" node='");
  158.             builder.append(getNode());
  159.            
  160.             if (notify != null)
  161.             {
  162.                 builder.append("' ");
  163.                 builder.append(type.getElementAttribute());
  164.                 builder.append("='");
  165.                 builder.append(notify.equals(Boolean.TRUE) ? 1 : 0);
  166.                 builder.append("'>");
  167.             }
  168.             else
  169.             {
  170.                 builder.append("'>");
  171.                 for (ExtensionElement item : items)
  172.                 {
  173.                     builder.append(item.toXML());
  174.                 }
  175.             }
  176.            
  177.             builder.append("</");
  178.             builder.append(getElementName());
  179.             builder.append(">");
  180.             return builder.toString();
  181.         }
  182.     }

  183.     @Override
  184.     public String toString()
  185.     {
  186.         return getClass().getName() + "Content [" + toXML() + "]";
  187.     }

  188. }