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.packet.XmlEnvironment;
022import org.jivesoftware.smack.provider.ExtensionElementProvider;
023import org.jivesoftware.smack.xml.XmlPullParser;
024import org.jivesoftware.smack.xml.XmlPullParserException;
025
026import org.jivesoftware.smackx.jingleold.media.PayloadType;
027import org.jivesoftware.smackx.jingleold.packet.JingleDescription;
028
029/**
030 * Parser for a Jingle description.
031 *
032 * @author Alvaro Saurin
033 */
034public abstract class JingleDescriptionProvider extends ExtensionElementProvider<JingleDescription> {
035
036    /**
037     * Parse a iq/jingle/description/payload-type element.
038     *
039     * @param parser TODO javadoc me please
040     *            the input to parse
041     * @return a payload type element
042     */
043    protected PayloadType parsePayload(final XmlPullParser parser) {
044        int ptId = 0;
045        String ptName;
046        int ptChannels = 0;
047
048        try {
049            ptId = Integer.parseInt(parser.getAttributeValue("", "id"));
050        } catch (Exception e) {
051        }
052
053        ptName = parser.getAttributeValue("", "name");
054
055        try {
056            ptChannels = Integer.parseInt(parser.getAttributeValue("", "channels"));
057        } catch (Exception e) {
058        }
059
060        return new PayloadType(ptId, ptName, ptChannels);
061    }
062
063    /**
064     * Parse a iq/jingle/description element.
065     *
066     * @param parser TODO javadoc me please
067     *            the input to parse
068     * @return a description element
069     * @throws IOException if an I/O error occurred.
070     * @throws XmlPullParserException if an error in the XML parser occurred.
071     */
072    @Override
073    public JingleDescription parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
074        boolean done = false;
075        JingleDescription desc = getInstance();
076
077        while (!done) {
078            XmlPullParser.Event eventType = parser.next();
079            String name = parser.getName();
080
081            if (eventType == XmlPullParser.Event.START_ELEMENT) {
082                if (name.equals(PayloadType.NODENAME)) {
083                    desc.addPayloadType(parsePayload(parser));
084                } else {
085                    // TODO: Should be SmackParseException.
086                    throw new IOException("Unknow element \"" + name + "\" in content.");
087                }
088            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
089                if (name.equals(JingleDescription.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     * @return the jingle description.
102     */
103    protected abstract JingleDescription getInstance();
104
105    /**
106     * Jingle audio.
107     */
108    public static class Audio extends JingleDescriptionProvider {
109
110        /**
111         * Parse an audio payload type.
112         */
113        @Override
114        public PayloadType parsePayload(final XmlPullParser parser) {
115            PayloadType pte = super.parsePayload(parser);
116            PayloadType.Audio pt = new PayloadType.Audio(pte);
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 pt;
126        }
127
128        /**
129         * Get a new instance of this object.
130         */
131        @Override
132        protected JingleDescription getInstance() {
133            return new JingleDescription.Audio();
134        }
135    }
136}