001/**
002 *
003 * Copyright 2017 Florian Schmaus
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.jingle.element;
018
019import java.util.Collections;
020import java.util.List;
021
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025/**
026 * A jingle transport extension.
027 *
028 */
029public abstract class JingleContentTransport implements ExtensionElement {
030
031    public static final String ELEMENT = "transport";
032
033    protected final List<JingleContentTransportCandidate> candidates;
034    protected final JingleContentTransportInfo info;
035
036    protected JingleContentTransport(List<JingleContentTransportCandidate> candidates) {
037        this(candidates, null);
038    }
039
040    protected JingleContentTransport(List<JingleContentTransportCandidate> candidates, JingleContentTransportInfo info) {
041        if (candidates != null) {
042            this.candidates = Collections.unmodifiableList(candidates);
043        }
044        else {
045            this.candidates = Collections.emptyList();
046        }
047
048        this.info = info;
049    }
050
051    public List<JingleContentTransportCandidate> getCandidates() {
052        return candidates;
053    }
054
055    public JingleContentTransportInfo getInfo() {
056        return info;
057    }
058
059    @Override
060    public String getElementName() {
061        return ELEMENT;
062    }
063
064    protected void addExtraAttributes(XmlStringBuilder xml) {
065
066    }
067
068    @Override
069    public XmlStringBuilder toXML(String enclosingNamespace) {
070        XmlStringBuilder xml = new XmlStringBuilder(this);
071        addExtraAttributes(xml);
072
073        if (candidates.isEmpty() && info == null) {
074            xml.closeEmptyElement();
075
076        } else {
077
078            xml.rightAngleBracket();
079            xml.append(candidates);
080            xml.optElement(info);
081            xml.closeElement(this);
082        }
083
084        return xml;
085    }
086}