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