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