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