001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.pubsub;
018
019import java.util.ArrayList;
020import java.util.Collection;
021
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024/**
025 * Represents a request to publish an item(s) to a specific node.
026 *
027 * @author Robin Collier
028 */
029public class PublishItem<T extends Item> extends NodeExtension {
030    protected Collection<T> items;
031
032    /**
033     * Construct a request to publish an item to a node.
034     *
035     * @param nodeId The node to publish to
036     * @param toPublish The {@link Item} to publish
037     */
038    public PublishItem(String nodeId, T toPublish) {
039        super(PubSubElementType.PUBLISH, nodeId);
040        items = new ArrayList<>(1);
041        items.add(toPublish);
042    }
043
044    /**
045     * Construct a request to publish multiple items to a node.
046     *
047     * @param nodeId The node to publish to
048     * @param toPublish The list of {@link Item} to publish
049     */
050    public PublishItem(String nodeId, Collection<T> toPublish) {
051        super(PubSubElementType.PUBLISH, nodeId);
052        items = toPublish;
053    }
054
055    @Override
056    protected void addXml(XmlStringBuilder xml) {
057        xml.rightAngleBracket();
058        xml.append(items);
059        xml.closeElement(this);
060    }
061}