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