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