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        JingleDescription desc = getInstance();
075
076        outerloop: while (true) {
077            XmlPullParser.Event eventType = parser.next();
078            if (eventType == XmlPullParser.Event.START_ELEMENT) {
079                String name = parser.getName();
080                if (name.equals(PayloadType.NODENAME)) {
081                    desc.addPayloadType(parsePayload(parser));
082                } else {
083                    // TODO: Should be SmackParseException.
084                    throw new IOException("Unknow element \"" + name + "\" in content.");
085                }
086            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
087                if (parser.getDepth() == initialDepth) {
088                    break outerloop;
089                }
090            }
091        }
092        return desc;
093    }
094
095    /**
096     * Return a new instance of this class. Subclasses must overwrite this
097     * method.
098     *
099     * @return the jingle description.
100     */
101    protected abstract JingleDescription getInstance();
102
103    /**
104     * Jingle audio.
105     */
106    public static class Audio extends JingleDescriptionProvider {
107
108        /**
109         * Parse an audio payload type.
110         */
111        @Override
112        public PayloadType parsePayload(final XmlPullParser parser) {
113            PayloadType pte = super.parsePayload(parser);
114            PayloadType.Audio pt = new PayloadType.Audio(pte);
115            int ptClockRate = 0;
116
117            try {
118                ptClockRate = Integer.parseInt(parser.getAttributeValue("", "clockrate"));
119            } catch (Exception e) {
120            }
121            pt.setClockRate(ptClockRate);
122
123            return pt;
124        }
125
126        /**
127         * Get a new instance of this object.
128         */
129        @Override
130        protected JingleDescription getInstance() {
131            return new JingleDescription.Audio();
132        }
133    }
134}