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    @Override
073    public JingleDescription parse(XmlPullParser parser, int initialDepth) throws SmackException, XmlPullParserException, IOException {
074        boolean done = false;
075        JingleDescription desc = getInstance();
076
077        while (!done) {
078            int eventType = parser.next();
079            String name = parser.getName();
080
081            if (eventType == XmlPullParser.START_TAG) {
082                if (name.equals(PayloadType.NODENAME)) {
083                    desc.addPayloadType(parsePayload(parser));
084                } else {
085                    throw new SmackException("Unknow element \"" + name + "\" in content.");
086                }
087            } else if (eventType == XmlPullParser.END_TAG) {
088                if (name.equals(JingleDescription.NODENAME)) {
089                    done = true;
090                }
091            }
092        }
093        return desc;
094    }
095
096    /**
097     * Return a new instance of this class. Subclasses must overwrite this
098     * method.
099     */
100    protected abstract JingleDescription getInstance();
101
102    /**
103     * Jingle audio.
104     */
105    public static class Audio extends JingleDescriptionProvider {
106
107        /**
108         * Parse an audio payload type.
109         */
110        @Override
111        public PayloadType parsePayload(final XmlPullParser parser) {
112            PayloadType pte = super.parsePayload(parser);
113            PayloadType.Audio pt = new PayloadType.Audio(pte);
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 pt;
123        }
124
125        /**
126         * Get a new instance of this object.
127         */
128        @Override
129        protected JingleDescription getInstance() {
130            return new JingleDescription.Audio();
131        }
132    }
133}