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.jingle.packet;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.Iterator;
022import java.util.List;
023
024import org.jivesoftware.smack.packet.PacketExtension;
025
026/**
027 * Jingle content.
028 * 
029 * @author Jeff Williams
030 */
031public class JingleContent implements PacketExtension {
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<JingleTransport>();
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    public String getElementName() {
066        return NODENAME;
067    }
068
069    /**
070     * Return the namespace.
071     * 
072     * @return The namespace
073     */
074    public String getNamespace() {
075        // There is no namespace for <content>
076        return "";
077    }
078
079    /**
080     * Sets the description for this Jingle content.
081     * 
082     * @param description
083     *            The description
084     */
085    public void setDescription(JingleDescription description) {
086        this.description = description;
087    }
088
089    /**
090     * Gets the description for this Jingle content.
091     * 
092     * @return The description.
093     */
094    public JingleDescription getDescription() {
095        return description;
096    }
097
098    /**
099     * Adds a JingleTransport type to the packet.
100     * 
101     * @param transport
102     *            the JignleTransport to add.
103     */
104    public void addJingleTransport(final JingleTransport transport) {
105        synchronized (transports) {
106            transports.add(transport);
107        }
108    }
109
110    /**
111     * Adds a list of transports to add to the packet.
112     * 
113     * @param transports
114     *            the transports to add.
115     */
116    public void addTransports(final List<JingleTransport> transports) {
117        synchronized (transports) {
118            for (JingleTransport transport : transports) {
119                addJingleTransport(transport);
120            }
121        }
122    }
123
124    /**
125     * Returns an Iterator for the JingleTransports in the packet.
126     * 
127     * @return an Iterator for the JingleTransports in the packet.
128     */
129    public Iterator<JingleTransport> getJingleTransports() {
130        return Collections.unmodifiableList(getJingleTransportsList()).iterator();
131    }
132
133    /**
134     * Returns a list for the JingleTransports in the packet.
135     * 
136     * @return a list for the JingleTransports in the packet.
137     */
138    public List<JingleTransport> getJingleTransportsList() {
139        synchronized (transports) {
140            return new ArrayList<JingleTransport>(transports);
141        }
142    }
143
144    /**
145     * Returns a count of the JingleTransports in the Jingle packet.
146     * 
147     * @return the number of the JingleTransports in the Jingle packet.
148     */
149    public int getJingleTransportsCount() {
150        synchronized (transports) {
151            return transports.size();
152        }
153    }
154
155    /**
156     * Convert a Jingle description to XML.
157     * 
158     * @return a string with the XML representation
159     */
160    public String toXML() {
161        StringBuilder buf = new StringBuilder();
162
163        synchronized (transports) {
164
165            buf.append("<").append(getElementName());
166
167            buf.append(" creator='" + creator + "' name='" + name + "'>");
168
169            // Add the description.
170            if (description != null) {
171                buf.append(description.toXML());
172            }
173
174            // Add all of the transports.
175            for (JingleTransport transport : transports) {
176                buf.append(transport.toXML());
177            }
178            buf.append("</").append(getElementName()).append(">");
179        }
180        return buf.toString();
181    }
182
183}