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
022/**
023 * Represents a request to publish an item(s) to a specific node.
024 *
025 * @author Robin Collier
026 */
027public class PublishItem<T extends Item> extends NodeExtension {
028    protected Collection<T> items;
029
030    /**
031     * Construct a request to publish an item to a node.
032     *
033     * @param nodeId The node to publish to
034     * @param toPublish The {@link Item} to publish
035     */
036    public PublishItem(String nodeId, T toPublish) {
037        super(PubSubElementType.PUBLISH, nodeId);
038        items = new ArrayList<>(1);
039        items.add(toPublish);
040    }
041
042    /**
043     * Construct a request to publish multiple items to a node.
044     *
045     * @param nodeId The node to publish to
046     * @param toPublish The list of {@link Item} to publish
047     */
048    public PublishItem(String nodeId, Collection<T> toPublish) {
049        super(PubSubElementType.PUBLISH, nodeId);
050        items = toPublish;
051    }
052
053    @Override
054    public String toXML(String enclosingNamespace) {
055        StringBuilder builder = new StringBuilder("<");
056        builder.append(getElementName());
057        builder.append(" node='");
058        builder.append(getNode());
059        builder.append("'>");
060
061        for (Item item : items) {
062            builder.append(item.toXML(null));
063        }
064        builder.append("</publish>");
065
066        return builder.toString();
067    }
068}