001/**
002 *
003 * Copyright 2003-2005 Jive Software.
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.jingleold.packet;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.Iterator;
022import java.util.List;
023
024import org.jivesoftware.smack.packet.ExtensionElement;
025
026/**
027 * Jingle content.
028 *
029 * @author Jeff Williams
030 */
031public class JingleContent implements ExtensionElement {
032
033    public static final String NODENAME = "content";
034    public static final String CREATOR = "creator";
035    public static final String NAME = "name";
036
037    private String creator;
038    private String name;
039
040    private JingleDescription description;
041    private final List<JingleTransport> transports = new ArrayList<>();
042
043    /**
044     * Creates a content description.
045     *
046     * @param creator the creator.
047     * @param name the name.
048     */
049    public JingleContent(String creator, String name) {
050        super();
051        this.creator = creator;
052        this.name = name;
053    }
054
055    public String getCreator() {
056        return creator;
057    }
058
059    public String getName() {
060        return name;
061    }
062
063    /**
064     * Returns the XML element name of the element.
065     *
066     * @return the XML element name of the element.
067     */
068    @Override
069    public String getElementName() {
070        return NODENAME;
071    }
072
073    /**
074     * Return the namespace.
075     *
076     * @return The namespace
077     */
078    @Override
079    public String getNamespace() {
080        // There is no namespace for <content>
081        return "";
082    }
083
084    /**
085     * Sets the description for this Jingle content.
086     *
087     * @param description TODO javadoc me please
088     *            The description
089     */
090    public void setDescription(JingleDescription description) {
091        this.description = description;
092    }
093
094    /**
095     * Gets the description for this Jingle content.
096     *
097     * @return The description.
098     */
099    public JingleDescription getDescription() {
100        return description;
101    }
102
103    /**
104     * Adds a JingleTransport type to the packet.
105     *
106     * @param transport TODO javadoc me please
107     *            the JingleTransport to add.
108     */
109    public void addJingleTransport(final JingleTransport transport) {
110        synchronized (transports) {
111            transports.add(transport);
112        }
113    }
114
115    /**
116     * Adds a list of transports to add to the packet.
117     *
118     * @param transports TODO javadoc me please
119     *            the transports to add.
120     */
121    public void addTransports(final List<JingleTransport> transports) {
122        synchronized (transports) {
123            for (JingleTransport transport : transports) {
124                addJingleTransport(transport);
125            }
126        }
127    }
128
129    /**
130     * Returns an Iterator for the JingleTransports in the packet.
131     *
132     * @return an Iterator for the JingleTransports in the packet.
133     */
134    public Iterator<JingleTransport> getJingleTransports() {
135        return Collections.unmodifiableList(getJingleTransportsList()).iterator();
136    }
137
138    /**
139     * Returns a list for the JingleTransports in the packet.
140     *
141     * @return a list for the JingleTransports in the packet.
142     */
143    public List<JingleTransport> getJingleTransportsList() {
144        synchronized (transports) {
145            return new ArrayList<>(transports);
146        }
147    }
148
149    /**
150     * Returns a count of the JingleTransports in the Jingle packet.
151     *
152     * @return the number of the JingleTransports in the Jingle packet.
153     */
154    public int getJingleTransportsCount() {
155        synchronized (transports) {
156            return transports.size();
157        }
158    }
159
160    /**
161     * Convert a Jingle description to XML.
162     *
163     * @return a string with the XML representation
164     */
165    @Override
166    public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
167        StringBuilder buf = new StringBuilder();
168
169        synchronized (transports) {
170
171            buf.append('<').append(getElementName());
172
173            buf.append(" creator='" + creator + "' name='" + name + "'>");
174
175            // Add the description.
176            if (description != null) {
177                buf.append(description.toXML());
178            }
179
180            // Add all of the transports.
181            for (JingleTransport transport : transports) {
182                buf.append(transport.toXML());
183            }
184            buf.append("</").append(getElementName()).append('>');
185        }
186        return buf.toString();
187    }
188
189}