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{
029        protected Collection<T> items;
030        
031        /**
032         * Construct a request to publish an item to a node.
033         * 
034         * @param nodeId The node to publish to
035         * @param toPublish The {@link Item} to publish
036         */
037        public PublishItem(String nodeId, T toPublish)
038        {
039                super(PubSubElementType.PUBLISH, nodeId);
040                items = new ArrayList<T>(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        {
052                super(PubSubElementType.PUBLISH, nodeId);
053                items = toPublish;
054        }
055
056        @Override
057        public String toXML()
058        {
059                StringBuilder builder = new StringBuilder("<");
060                builder.append(getElementName());
061                builder.append(" node='");
062                builder.append(getNode());
063                builder.append("'>");
064                
065                for (Item item : items)
066                {
067                        builder.append(item.toXML());
068                }
069                builder.append("</publish>");
070                
071                return builder.toString();
072        }
073}