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.Collections;
020import java.util.List;
021
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024import org.jivesoftware.smackx.pubsub.Affiliation.AffiliationNamespace;
025
026/**
027 * Represents the <b>affiliations</b> element of the reply to a request for affiliations.
028 * It is defined in the specification in section <a href="http://xmpp.org/extensions/xep-0060.html#entity-affiliations">5.7 Retrieve Affiliations</a> and
029 * <a href="http://www.xmpp.org/extensions/xep-0060.html#owner-affiliations">8.9 Manage Affiliations</a>.
030 *
031 * @author Robin Collier
032 */
033public class AffiliationsExtension extends NodeExtension {
034    protected List<Affiliation> items = Collections.emptyList();
035    private final String node;
036
037    public AffiliationsExtension() {
038        this(null);
039    }
040
041    public AffiliationsExtension(List<Affiliation> subList) {
042        this(subList, null);
043    }
044
045    public AffiliationsExtension(AffiliationNamespace affiliationsNamespace, List<Affiliation> subList) {
046        this(affiliationsNamespace, subList, null);
047    }
048
049    public AffiliationsExtension(List<Affiliation> subList, String node) {
050        this(AffiliationNamespace.basic, subList, node);
051    }
052
053    public AffiliationsExtension(AffiliationNamespace affiliationsNamespace, List<Affiliation> subList, String node) {
054        super(affiliationsNamespace.type);
055        items = subList;
056        this.node = node;
057    }
058
059    public List<Affiliation> getAffiliations() {
060        return items;
061    }
062
063    @Override
064    public CharSequence toXML(String enclosingNamespace) {
065        if ((items == null) || (items.size() == 0)) {
066            return super.toXML(enclosingNamespace);
067        }
068        else {
069            // Can't use XmlStringBuilder(this), because we don't want the namespace to be included
070            XmlStringBuilder xml = new XmlStringBuilder();
071            xml.halfOpenElement(getElementName());
072            xml.optAttribute("node", node);
073            xml.rightAngleBracket();
074            xml.append(items);
075            xml.closeElement(this);
076            return xml;
077        }
078    }
079}