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