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    public JingleContent(String creator, String name) {
047        super();
048        this.creator = creator;
049        this.name = name;
050    }
051
052    public String getCreator() {
053        return creator;
054    }
055
056    public String getName() {
057        return name;
058    }
059
060    /**
061     * Returns the XML element name of the element.
062     *
063     * @return the XML element name of the element.
064     */
065    @Override
066    public String getElementName() {
067        return NODENAME;
068    }
069
070    /**
071     * Return the namespace.
072     *
073     * @return The namespace
074     */
075    @Override
076    public String getNamespace() {
077        // There is no namespace for <content>
078        return "";
079    }
080
081    /**
082     * Sets the description for this Jingle content.
083     *
084     * @param description
085     *            The description
086     */
087    public void setDescription(JingleDescription description) {
088        this.description = description;
089    }
090
091    /**
092     * Gets the description for this Jingle content.
093     *
094     * @return The description.
095     */
096    public JingleDescription getDescription() {
097        return description;
098    }
099
100    /**
101     * Adds a JingleTransport type to the packet.
102     *
103     * @param transport
104     *            the JingleTransport to add.
105     */
106    public void addJingleTransport(final JingleTransport transport) {
107        synchronized (transports) {
108            transports.add(transport);
109        }
110    }
111
112    /**
113     * Adds a list of transports to add to the packet.
114     *
115     * @param transports
116     *            the transports to add.
117     */
118    public void addTransports(final List<JingleTransport> transports) {
119        synchronized (transports) {
120            for (JingleTransport transport : transports) {
121                addJingleTransport(transport);
122            }
123        }
124    }
125
126    /**
127     * Returns an Iterator for the JingleTransports in the packet.
128     *
129     * @return an Iterator for the JingleTransports in the packet.
130     */
131    public Iterator<JingleTransport> getJingleTransports() {
132        return Collections.unmodifiableList(getJingleTransportsList()).iterator();
133    }
134
135    /**
136     * Returns a list for the JingleTransports in the packet.
137     *
138     * @return a list for the JingleTransports in the packet.
139     */
140    public List<JingleTransport> getJingleTransportsList() {
141        synchronized (transports) {
142            return new ArrayList<>(transports);
143        }
144    }
145
146    /**
147     * Returns a count of the JingleTransports in the Jingle packet.
148     *
149     * @return the number of the JingleTransports in the Jingle packet.
150     */
151    public int getJingleTransportsCount() {
152        synchronized (transports) {
153            return transports.size();
154        }
155    }
156
157    /**
158     * Convert a Jingle description to XML.
159     *
160     * @return a string with the XML representation
161     */
162    @Override
163    public String toXML(String enclosingNamespace) {
164        StringBuilder buf = new StringBuilder();
165
166        synchronized (transports) {
167
168            buf.append('<').append(getElementName());
169
170            buf.append(" creator='" + creator + "' name='" + name + "'>");
171
172            // Add the description.
173            if (description != null) {
174                buf.append(description.toXML(null));
175            }
176
177            // Add all of the transports.
178            for (JingleTransport transport : transports) {
179                buf.append(transport.toXML(null));
180            }
181            buf.append("</").append(getElementName()).append('>');
182        }
183        return buf.toString();
184    }
185
186}