2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.pubsub;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the element holding the list of subscription elements.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @author Robin Collier
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public class SubscriptionsExtension extends NodeExtension {
|
2018-04-19 11:05:43 +02:00
|
|
|
public enum SubscriptionsNamespace {
|
|
|
|
basic(PubSubElementType.SUBSCRIPTIONS),
|
|
|
|
owner(PubSubElementType.SUBSCRIPTIONS_OWNER),
|
|
|
|
;
|
|
|
|
public final PubSubElementType type;
|
|
|
|
|
|
|
|
SubscriptionsNamespace(PubSubElementType type) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SubscriptionsNamespace fromXmlns(String xmlns) {
|
|
|
|
for (SubscriptionsNamespace subscriptionsNamespace : SubscriptionsNamespace.values()) {
|
|
|
|
if (subscriptionsNamespace.type.getNamespace().getXmlns().equals(xmlns)) {
|
|
|
|
return subscriptionsNamespace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("Invalid Subscription namespace: " + xmlns);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
protected List<Subscription> items = Collections.emptyList();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
/**
|
|
|
|
* Subscriptions to the root node.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @param subList The list of subscriptions
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public SubscriptionsExtension(List<Subscription> subList) {
|
2018-04-19 11:05:43 +02:00
|
|
|
this(SubscriptionsNamespace.basic, null, subList);
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
/**
|
|
|
|
* Subscriptions to the specified node.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @param nodeId The node subscribed to
|
|
|
|
* @param subList The list of subscriptions
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public SubscriptionsExtension(String nodeId, List<Subscription> subList) {
|
2018-04-19 11:05:43 +02:00
|
|
|
this(SubscriptionsNamespace.basic, nodeId, subList);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscriptions to the specified node.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2018-04-19 11:05:43 +02:00
|
|
|
* @param subscriptionsNamespace the namespace used by this element
|
|
|
|
* @param nodeId The node subscribed to
|
|
|
|
* @param subList The list of subscriptions
|
|
|
|
* @since 4.3
|
|
|
|
*/
|
|
|
|
public SubscriptionsExtension(SubscriptionsNamespace subscriptionsNamespace, String nodeId, List<Subscription> subList) {
|
|
|
|
super(subscriptionsNamespace.type, nodeId);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
if (subList != null)
|
|
|
|
items = subList;
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
/**
|
|
|
|
* Gets the list of subscriptions.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @return List of subscriptions
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public List<Subscription> getSubscriptions() {
|
2017-02-07 22:02:40 +01:00
|
|
|
return items;
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
@Override
|
2018-04-25 14:20:18 +02:00
|
|
|
public CharSequence toXML(String enclosingNamespace) {
|
2018-04-06 10:21:46 +02:00
|
|
|
if ((items == null) || (items.size() == 0)) {
|
2018-04-25 14:20:18 +02:00
|
|
|
return super.toXML(enclosingNamespace);
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2018-04-06 10:21:46 +02:00
|
|
|
else {
|
2017-02-07 22:02:40 +01:00
|
|
|
StringBuilder builder = new StringBuilder("<");
|
|
|
|
builder.append(getElementName());
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2018-04-06 10:21:46 +02:00
|
|
|
if (getNode() != null) {
|
2017-02-07 22:02:40 +01:00
|
|
|
builder.append(" node='");
|
|
|
|
builder.append(getNode());
|
|
|
|
builder.append('\'');
|
|
|
|
}
|
|
|
|
builder.append('>');
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2018-04-06 10:21:46 +02:00
|
|
|
for (Subscription item : items) {
|
2018-04-25 14:20:18 +02:00
|
|
|
builder.append(item.toXML(null));
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
builder.append("</");
|
|
|
|
builder.append(getElementName());
|
|
|
|
builder.append('>');
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|