PublishItem.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.ArrayList;
  19. import java.util.Collection;

  20. /**
  21.  * Represents a request to publish an item(s) to a specific node.
  22.  *
  23.  * @author Robin Collier
  24.  */
  25. public class PublishItem<T extends Item> extends NodeExtension
  26. {
  27.     protected Collection<T> items;
  28.    
  29.     /**
  30.      * Construct a request to publish an item to a node.
  31.      *
  32.      * @param nodeId The node to publish to
  33.      * @param toPublish The {@link Item} to publish
  34.      */
  35.     public PublishItem(String nodeId, T toPublish)
  36.     {
  37.         super(PubSubElementType.PUBLISH, nodeId);
  38.         items = new ArrayList<T>(1);
  39.         items.add(toPublish);
  40.     }

  41.     /**
  42.      * Construct a request to publish multiple items to a node.
  43.      *
  44.      * @param nodeId The node to publish to
  45.      * @param toPublish The list of {@link Item} to publish
  46.      */
  47.     public PublishItem(String nodeId, Collection<T> toPublish)
  48.     {
  49.         super(PubSubElementType.PUBLISH, nodeId);
  50.         items = toPublish;
  51.     }

  52.     @Override
  53.     public String toXML()
  54.     {
  55.         StringBuilder builder = new StringBuilder("<");
  56.         builder.append(getElementName());
  57.         builder.append(" node='");
  58.         builder.append(getNode());
  59.         builder.append("'>");
  60.        
  61.         for (Item item : items)
  62.         {
  63.             builder.append(item.toXML());
  64.         }
  65.         builder.append("</publish>");
  66.        
  67.         return builder.toString();
  68.     }
  69. }