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, responder; 054 boolean done = false; 055 JingleContent currentContent = null; 056 057 // Sub-elements providers 058 JingleContentProvider jcp = new JingleContentProvider(); 059 JingleDescriptionProvider jdpAudio = new JingleDescriptionProvider.Audio(); 060 JingleTransportProvider jtpRawUdp = new JingleTransportProvider.RawUdp(); 061 JingleTransportProvider jtpIce = new JingleTransportProvider.Ice(); 062 ExtensionElementProvider<?> jmipAudio = new JingleContentInfoProvider.Audio(); 063 064 int eventType; 065 String elementName; 066 String namespace; 067 068 // Get some attributes for the <jingle> element 069 sid = parser.getAttributeValue("", "sid"); 070 action = JingleActionEnum.getAction(parser.getAttributeValue("", "action")); 071 initiator = ParserUtils.getJidAttribute(parser, "initiator"); 072 responder = ParserUtils.getJidAttribute(parser, "responder"); 073 074 jingle.setSid(sid); 075 jingle.setAction(action); 076 jingle.setInitiator(initiator); 077 jingle.setResponder(responder); 078 079 // Start processing sub-elements 080 while (!done) { 081 eventType = parser.next(); 082 elementName = parser.getName(); 083 namespace = parser.getNamespace(); 084 085 if (eventType == XmlPullParser.START_TAG) { 086 087 // Parse some well know subelements, depending on the namespaces 088 // and element names... 089 090 if (elementName.equals(JingleContent.NODENAME)) { 091 // Add a new <content> element to the jingle 092 currentContent = jcp.parse(parser); 093 jingle.addContent(currentContent); 094 } else if (elementName.equals(JingleDescription.NODENAME) && namespace.equals(JingleDescription.Audio.NAMESPACE)) { 095 // Set the <description> element of the <content> 096 currentContent.setDescription(jdpAudio.parse(parser)); 097 } else if (elementName.equals(JingleTransport.NODENAME)) { 098 // Add all of the <transport> elements to the <content> of the jingle 099 100 // Parse the possible transport namespaces 101 if (namespace.equals(JingleTransport.RawUdp.NAMESPACE)) { 102 currentContent.addJingleTransport(jtpRawUdp.parse(parser)); 103 } else if (namespace.equals(JingleTransport.Ice.NAMESPACE)) { 104 currentContent.addJingleTransport(jtpIce.parse(parser)); 105 } else { 106 throw new SmackException("Unknown transport namespace \"" + namespace + "\" in Jingle packet."); 107 } 108 } else if (namespace.equals(JingleContentInfo.Audio.NAMESPACE)) { 109 jingle.setContentInfo((JingleContentInfo) jmipAudio.parse(parser)); 110 } else { 111 throw new SmackException("Unknown combination of namespace \"" + namespace + "\" and element name \"" 112 + elementName + "\" in Jingle packet."); 113 } 114 115 } else if (eventType == XmlPullParser.END_TAG) { 116 if (parser.getName().equals(Jingle.getElementName())) { 117 done = true; 118 } 119 } 120 } 121 122 return jingle; 123 } 124}