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 */ 017package org.jivesoftware.smackx.jingleold.provider; 018 019import java.io.IOException; 020 021import org.jivesoftware.smack.packet.XmlEnvironment; 022import org.jivesoftware.smack.provider.ExtensionElementProvider; 023import org.jivesoftware.smack.xml.XmlPullParser; 024import org.jivesoftware.smack.xml.XmlPullParserException; 025 026import org.jivesoftware.smackx.jingleold.media.PayloadType; 027import org.jivesoftware.smackx.jingleold.packet.JingleDescription; 028 029import org.jxmpp.JxmppContext; 030 031/** 032 * Parser for a Jingle description. 033 * 034 * @author Alvaro Saurin 035 */ 036public abstract class JingleDescriptionProvider extends ExtensionElementProvider<JingleDescription> { 037 038 /** 039 * Parse a iq/jingle/description/payload-type element. 040 * 041 * @param parser TODO javadoc me please 042 * the input to parse 043 * @return a payload type element 044 */ 045 protected PayloadType parsePayload(final XmlPullParser parser) { 046 int ptId; 047 String ptName; 048 int ptChannels; 049 050 ptId = Integer.parseInt(parser.getAttributeValue("", "id")); 051 052 ptName = parser.getAttributeValue("", "name"); 053 054 ptChannels = Integer.parseInt(parser.getAttributeValue("", "channels")); 055 056 return new PayloadType(ptId, ptName, ptChannels); 057 } 058 059 /** 060 * Parse a iq/jingle/description element. 061 * 062 * @param parser TODO javadoc me please 063 * the input to parse 064 * @return a description element 065 * @throws IOException if an I/O error occurred. 066 * @throws XmlPullParserException if an error in the XML parser occurred. 067 */ 068 @Override 069 public JingleDescription parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException { 070 JingleDescription desc = getInstance(); 071 072 outerloop: while (true) { 073 XmlPullParser.Event eventType = parser.next(); 074 if (eventType == XmlPullParser.Event.START_ELEMENT) { 075 String name = parser.getName(); 076 if (name.equals(PayloadType.NODENAME)) { 077 desc.addPayloadType(parsePayload(parser)); 078 } else { 079 // TODO: Should be SmackParseException. 080 throw new IOException("Unknow element \"" + name + "\" in content."); 081 } 082 } else if (eventType == XmlPullParser.Event.END_ELEMENT) { 083 if (parser.getDepth() == initialDepth) { 084 break outerloop; 085 } 086 } 087 } 088 return desc; 089 } 090 091 /** 092 * Return a new instance of this class. Subclasses must overwrite this 093 * method. 094 * 095 * @return the jingle description. 096 */ 097 protected abstract JingleDescription getInstance(); 098 099 /** 100 * Jingle audio. 101 */ 102 public static class Audio extends JingleDescriptionProvider { 103 104 /** 105 * Parse an audio payload type. 106 */ 107 @Override 108 public PayloadType parsePayload(final XmlPullParser parser) { 109 PayloadType pte = super.parsePayload(parser); 110 PayloadType.Audio pt = new PayloadType.Audio(pte); 111 int ptClockRate = Integer.parseInt(parser.getAttributeValue("", "clockrate")); 112 pt.setClockRate(ptClockRate); 113 114 return pt; 115 } 116 117 /** 118 * Get a new instance of this object. 119 */ 120 @Override 121 protected JingleDescription getInstance() { 122 return new JingleDescription.Audio(); 123 } 124 } 125}