1/**
2 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 *     http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14package org.jivesoftware.smackx.pubsub;
15
16import java.util.ArrayList;
17import java.util.Collection;
18
19/**
20 * Represents a request to publish an item(s) to a specific node.
21 *
22 * @author Robin Collier
23 */
24public class PublishItem <T extends Item> extends NodeExtension
25{
26	protected Collection<T> items;
27
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	{
36		super(PubSubElementType.PUBLISH, nodeId);
37		items = new ArrayList<T>(1);
38		items.add(toPublish);
39	}
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
53	@Override
54	public String toXML()
55	{
56		StringBuilder builder = new StringBuilder("<");
57		builder.append(getElementName());
58		builder.append(" node='");
59		builder.append(getNode());
60		builder.append("'>");
61
62		for (Item item : items)
63		{
64			builder.append(item.toXML());
65		}
66		builder.append("</publish>");
67
68		return builder.toString();
69	}
70}
71