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. import org.jivesoftware.smack.util.XmlStringBuilder;

  21. /**
  22.  * Represents a request to publish an item(s) to a specific node.
  23.  *
  24.  * @author Robin Collier
  25.  */
  26. public class PublishItem<T extends Item> extends NodeExtension {
  27.     protected Collection<T> items;

  28.     /**
  29.      * Construct a request to publish an item to a node.
  30.      *
  31.      * @param nodeId The node to publish to
  32.      * @param toPublish The {@link Item} to publish
  33.      */
  34.     public PublishItem(String nodeId, T toPublish) {
  35.         super(PubSubElementType.PUBLISH, nodeId);
  36.         items = new ArrayList<>(1);
  37.         items.add(toPublish);
  38.     }

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

  49.     @Override
  50.     protected void addXml(XmlStringBuilder xml) {
  51.         xml.rightAngleBracket();
  52.         xml.append(items);
  53.         xml.closeElement(this);
  54.     }
  55. }