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