Shuyi Chen | d7955ce | 2013-05-22 14:51:55 -0700 | [diff] [blame] | 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 | */
|
| 14 | package org.jivesoftware.smackx.pubsub;
|
| 15 |
|
| 16 | import java.util.List;
|
| 17 |
|
| 18 | import org.jivesoftware.smack.packet.PacketExtension;
|
| 19 |
|
| 20 | /**
|
| 21 | * This class is used to for multiple purposes.
|
| 22 | * <li>It can represent an event containing a list of items that have been published
|
| 23 | * <li>It can represent an event containing a list of retracted (deleted) items.
|
| 24 | * <li>It can represent a request to delete a list of items.
|
| 25 | * <li>It can represent a request to get existing items.
|
| 26 | *
|
| 27 | * <p><b>Please note, this class is used for internal purposes, and is not required for usage of
|
| 28 | * pubsub functionality.</b>
|
| 29 | *
|
| 30 | * @author Robin Collier
|
| 31 | */
|
| 32 | public class ItemsExtension extends NodeExtension implements EmbeddedPacketExtension
|
| 33 | {
|
| 34 | protected ItemsElementType type;
|
| 35 | protected Boolean notify;
|
| 36 | protected List<? extends PacketExtension> items;
|
| 37 |
|
| 38 | public enum ItemsElementType
|
| 39 | {
|
| 40 | /** An items element, which has an optional <b>max_items</b> attribute when requesting items */
|
| 41 | items(PubSubElementType.ITEMS, "max_items"),
|
| 42 |
|
| 43 | /** A retract element, which has an optional <b>notify</b> attribute when publishing deletions */
|
| 44 | retract(PubSubElementType.RETRACT, "notify");
|
| 45 |
|
| 46 | private PubSubElementType elem;
|
| 47 | private String att;
|
| 48 |
|
| 49 | private ItemsElementType(PubSubElementType nodeElement, String attribute)
|
| 50 | {
|
| 51 | elem = nodeElement;
|
| 52 | att = attribute;
|
| 53 | }
|
| 54 |
|
| 55 | public PubSubElementType getNodeElement()
|
| 56 | {
|
| 57 | return elem;
|
| 58 | }
|
| 59 |
|
| 60 | public String getElementAttribute()
|
| 61 | {
|
| 62 | return att;
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | /**
|
| 67 | * Construct an instance with a list representing items that have been published or deleted.
|
| 68 | *
|
| 69 | * <p>Valid scenarios are:
|
| 70 | * <li>Request items from node - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and an
|
| 71 | * optional value for the <b>max_items</b> attribute.
|
| 72 | * <li>Request to delete items - itemsType = {@link ItemsElementType#retract}, items = list of {@link Item} containing
|
| 73 | * only id's and an optional value for the <b>notify</b> attribute.
|
| 74 | * <li>Items published event - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and
|
| 75 | * attributeValue = <code>null</code>
|
| 76 | * <li>Items deleted event - itemsType = {@link ItemsElementType#items}, items = list of {@link RetractItem} and
|
| 77 | * attributeValue = <code>null</code>
|
| 78 | *
|
| 79 | * @param itemsType Type of representation
|
| 80 | * @param nodeId The node to which the items are being sent or deleted
|
| 81 | * @param items The list of {@link Item} or {@link RetractItem}
|
| 82 | * @param attributeValue The value of the <b>max_items</b>
|
| 83 | */
|
| 84 | public ItemsExtension(ItemsElementType itemsType, String nodeId, List<? extends PacketExtension> items)
|
| 85 | {
|
| 86 | super(itemsType.getNodeElement(), nodeId);
|
| 87 | type = itemsType;
|
| 88 | this.items = items;
|
| 89 | }
|
| 90 |
|
| 91 | /**
|
| 92 | * Construct an instance with a list representing items that have been published or deleted.
|
| 93 | *
|
| 94 | * <p>Valid scenarios are:
|
| 95 | * <li>Request items from node - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and an
|
| 96 | * optional value for the <b>max_items</b> attribute.
|
| 97 | * <li>Request to delete items - itemsType = {@link ItemsElementType#retract}, items = list of {@link Item} containing
|
| 98 | * only id's and an optional value for the <b>notify</b> attribute.
|
| 99 | * <li>Items published event - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and
|
| 100 | * attributeValue = <code>null</code>
|
| 101 | * <li>Items deleted event - itemsType = {@link ItemsElementType#items}, items = list of {@link RetractItem} and
|
| 102 | * attributeValue = <code>null</code>
|
| 103 | *
|
| 104 | * @param itemsType Type of representation
|
| 105 | * @param nodeId The node to which the items are being sent or deleted
|
| 106 | * @param items The list of {@link Item} or {@link RetractItem}
|
| 107 | * @param attributeValue The value of the <b>max_items</b>
|
| 108 | */
|
| 109 | public ItemsExtension(String nodeId, List<? extends PacketExtension> items, boolean notify)
|
| 110 | {
|
| 111 | super(ItemsElementType.retract.getNodeElement(), nodeId);
|
| 112 | type = ItemsElementType.retract;
|
| 113 | this.items = items;
|
| 114 | this.notify = notify;
|
| 115 | }
|
| 116 |
|
| 117 | /**
|
| 118 | * Get the type of element
|
| 119 | *
|
| 120 | * @return The element type
|
| 121 | */
|
| 122 | public ItemsElementType getItemsElementType()
|
| 123 | {
|
| 124 | return type;
|
| 125 | }
|
| 126 |
|
| 127 | public List<PacketExtension> getExtensions()
|
| 128 | {
|
| 129 | return (List<PacketExtension>)getItems();
|
| 130 | }
|
| 131 |
|
| 132 | /**
|
| 133 | * Gets the items related to the type of request or event.
|
| 134 | *
|
| 135 | * return List of {@link Item}, {@link RetractItem}, or null
|
| 136 | */
|
| 137 | public List<? extends PacketExtension> getItems()
|
| 138 | {
|
| 139 | return items;
|
| 140 | }
|
| 141 |
|
| 142 | /**
|
| 143 | * Gets the value of the optional attribute related to the {@link ItemsElementType}.
|
| 144 | *
|
| 145 | * @return The attribute value
|
| 146 | */
|
| 147 | public boolean getNotify()
|
| 148 | {
|
| 149 | return notify;
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public String toXML()
|
| 154 | {
|
| 155 | if ((items == null) || (items.size() == 0))
|
| 156 | {
|
| 157 | return super.toXML();
|
| 158 | }
|
| 159 | else
|
| 160 | {
|
| 161 | StringBuilder builder = new StringBuilder("<");
|
| 162 | builder.append(getElementName());
|
| 163 | builder.append(" node='");
|
| 164 | builder.append(getNode());
|
| 165 |
|
| 166 | if (notify != null)
|
| 167 | {
|
| 168 | builder.append("' ");
|
| 169 | builder.append(type.getElementAttribute());
|
| 170 | builder.append("='");
|
| 171 | builder.append(notify.equals(Boolean.TRUE) ? 1 : 0);
|
| 172 | builder.append("'>");
|
| 173 | }
|
| 174 | else
|
| 175 | {
|
| 176 | builder.append("'>");
|
| 177 | for (PacketExtension item : items)
|
| 178 | {
|
| 179 | builder.append(item.toXML());
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | builder.append("</");
|
| 184 | builder.append(getElementName());
|
| 185 | builder.append(">");
|
| 186 | return builder.toString();
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public String toString()
|
| 192 | {
|
| 193 | return getClass().getName() + "Content [" + toXML() + "]";
|
| 194 | }
|
| 195 |
|
| 196 | }
|