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