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