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