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 */
017
018package org.jivesoftware.smackx.jingleold.provider;
019
020import java.io.IOException;
021
022import org.jivesoftware.smack.packet.IqData;
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.parsing.SmackParsingException;
025import org.jivesoftware.smack.provider.ExtensionElementProvider;
026import org.jivesoftware.smack.provider.IqProvider;
027import org.jivesoftware.smack.util.ParserUtils;
028import org.jivesoftware.smack.xml.XmlPullParser;
029import org.jivesoftware.smack.xml.XmlPullParserException;
030
031import org.jivesoftware.smackx.jingleold.JingleActionEnum;
032import org.jivesoftware.smackx.jingleold.packet.Jingle;
033import org.jivesoftware.smackx.jingleold.packet.JingleContent;
034import org.jivesoftware.smackx.jingleold.packet.JingleContentInfo;
035import org.jivesoftware.smackx.jingleold.packet.JingleDescription;
036import org.jivesoftware.smackx.jingleold.packet.JingleTransport;
037
038import org.jxmpp.JxmppContext;
039import org.jxmpp.jid.Jid;
040
041/**
042 * The JingleProvider parses Jingle packets.
043 *
044 * @author Alvaro Saurin
045 */
046public class JingleProvider extends IqProvider<Jingle> {
047
048    /**
049     * Parse a iq/jingle element.
050     * @throws XmlPullParserException if an error in the XML parser occurred.
051     * @throws IOException if an I/O error occurred.
052     * @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
053     */
054    @Override
055    public Jingle parse(XmlPullParser parser, int intialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws IOException, XmlPullParserException, SmackParsingException {
056
057        Jingle jingle = new Jingle();
058        String sid;
059        JingleActionEnum action;
060        Jid initiator, responder;
061        boolean done = false;
062        JingleContent currentContent = null;
063
064        // Sub-elements providers
065        JingleContentProvider jcp = new JingleContentProvider();
066        JingleDescriptionProvider jdpAudio = new JingleDescriptionProvider.Audio();
067        JingleTransportProvider jtpRawUdp = new JingleTransportProvider.RawUdp();
068        JingleTransportProvider jtpIce = new JingleTransportProvider.Ice();
069        ExtensionElementProvider<?> jmipAudio = new JingleContentInfoProvider.Audio();
070
071        XmlPullParser.Event eventType;
072        String elementName;
073        String namespace;
074
075        // Get some attributes for the <jingle> element
076        sid = parser.getAttributeValue("", "sid");
077        action = JingleActionEnum.getAction(parser.getAttributeValue("", "action"));
078        initiator = ParserUtils.getJidAttribute(parser, "initiator", jxmppContext);
079        responder = ParserUtils.getJidAttribute(parser, "responder", jxmppContext);
080
081        jingle.setSid(sid);
082        jingle.setAction(action);
083        jingle.setInitiator(initiator);
084        jingle.setResponder(responder);
085
086        // Start processing sub-elements
087        while (!done) {
088            eventType = parser.next();
089
090            if (eventType == XmlPullParser.Event.START_ELEMENT) {
091                elementName = parser.getName();
092                namespace = parser.getNamespace();
093
094                // Parse some well know subelements, depending on the namespaces
095                // and element names...
096
097                if (elementName.equals(JingleContent.NODENAME)) {
098                    // Add a new <content> element to the jingle
099                    currentContent = jcp.parse(parser);
100                    jingle.addContent(currentContent);
101                } else if (elementName.equals(JingleDescription.NODENAME) && namespace.equals(JingleDescription.Audio.NAMESPACE)) {
102                    // Set the <description> element of the <content>
103                    currentContent.setDescription(jdpAudio.parse(parser));
104                } else if (elementName.equals(JingleTransport.NODENAME)) {
105                    // Add all of the <transport> elements to the <content> of the jingle
106
107                    // Parse the possible transport namespaces
108                    if (namespace.equals(JingleTransport.RawUdp.NAMESPACE)) {
109                        currentContent.addJingleTransport(jtpRawUdp.parse(parser));
110                    } else if (namespace.equals(JingleTransport.Ice.NAMESPACE)) {
111                        currentContent.addJingleTransport(jtpIce.parse(parser));
112                    } else {
113                        // TODO: Should be SmackParseException.
114                        throw new IOException("Unknown transport namespace \"" + namespace + "\" in Jingle packet.");
115                    }
116                } else if (namespace.equals(JingleContentInfo.Audio.NAMESPACE)) {
117                    jingle.setContentInfo((JingleContentInfo) jmipAudio.parse(parser));
118                } else {
119                    // TODO: Should be SmackParseException.
120                    throw new IOException("Unknown combination of namespace \"" + namespace + "\" and element name \""
121                            + elementName + "\" in Jingle packet.");
122                }
123
124            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
125                if (parser.getName().equals(Jingle.NODENAME)) {
126                    done = true;
127                }
128            }
129        }
130
131        return jingle;
132    }
133}