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.SmackException; 022import org.jivesoftware.smack.provider.ExtensionElementProvider; 023import org.jivesoftware.smackx.jingleold.media.PayloadType; 024import org.jivesoftware.smackx.jingleold.packet.JingleDescription; 025import org.xmlpull.v1.XmlPullParser; 026import org.xmlpull.v1.XmlPullParserException; 027 028/** 029 * Parser for a Jingle description 030 * 031 * @author Alvaro Saurin <alvaro.saurin@gmail.com> 032 */ 033public abstract class JingleDescriptionProvider extends ExtensionElementProvider<JingleDescription> { 034 035 /** 036 * Parse a iq/jingle/description/payload-type element. 037 * 038 * @param parser 039 * the input to parse 040 * @return a payload type element 041 */ 042 protected PayloadType parsePayload(final XmlPullParser parser) { 043 int ptId = 0; 044 String ptName; 045 int ptChannels = 0; 046 047 try { 048 ptId = Integer.parseInt(parser.getAttributeValue("", "id")); 049 } catch (Exception e) { 050 } 051 052 ptName = parser.getAttributeValue("", "name"); 053 054 try { 055 ptChannels = Integer.parseInt(parser.getAttributeValue("", "channels")); 056 } catch (Exception e) { 057 } 058 059 return new PayloadType(ptId, ptName, ptChannels); 060 } 061 062 /** 063 * Parse a iq/jingle/description element. 064 * 065 * @param parser 066 * the input to parse 067 * @return a description element 068 * @throws SmackException 069 * @throws IOException 070 * @throws XmlPullParserException 071 */ 072 public JingleDescription parse(XmlPullParser parser, int initialDepth) throws SmackException, XmlPullParserException, IOException { 073 boolean done = false; 074 JingleDescription desc = getInstance(); 075 076 while (!done) { 077 int eventType = parser.next(); 078 String name = parser.getName(); 079 080 if (eventType == XmlPullParser.START_TAG) { 081 if (name.equals(PayloadType.NODENAME)) { 082 desc.addPayloadType(parsePayload(parser)); 083 } else { 084 throw new SmackException("Unknow element \"" + name + "\" in content."); 085 } 086 } else if (eventType == XmlPullParser.END_TAG) { 087 if (name.equals(JingleDescription.NODENAME)) { 088 done = true; 089 } 090 } 091 } 092 return desc; 093 } 094 095 /** 096 * Return a new instance of this class. Subclasses must overwrite this 097 * method. 098 */ 099 protected abstract JingleDescription getInstance(); 100 101 /** 102 * Jingle audio 103 */ 104 public static class Audio extends JingleDescriptionProvider { 105 106 /** 107 * Parse an audio payload type. 108 */ 109 public PayloadType parsePayload(final XmlPullParser parser) { 110 PayloadType pte = super.parsePayload(parser); 111 PayloadType.Audio pt = new PayloadType.Audio(pte); 112 int ptClockRate = 0; 113 114 try { 115 ptClockRate = Integer.parseInt(parser.getAttributeValue("", "clockrate")); 116 } catch (Exception e) { 117 } 118 pt.setClockRate(ptClockRate); 119 120 return pt; 121 } 122 123 /** 124 * Get a new instance of this object. 125 */ 126 protected JingleDescription getInstance() { 127 return new JingleDescription.Audio(); 128 } 129 } 130}