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;
045        String ptName;
046        int ptChannels;
047
048        ptId = Integer.parseInt(parser.getAttributeValue("", "id"));
049
050        ptName = parser.getAttributeValue("", "name");
051
052        ptChannels = Integer.parseInt(parser.getAttributeValue("", "channels"));
053
054        return new PayloadType(ptId, ptName, ptChannels);
055    }
056
057    /**
058     * Parse a iq/jingle/description element.
059     *
060     * @param parser TODO javadoc me please
061     *            the input to parse
062     * @return a description element
063     * @throws IOException if an I/O error occurred.
064     * @throws XmlPullParserException if an error in the XML parser occurred.
065     */
066    @Override
067    public JingleDescription parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
068        JingleDescription desc = getInstance();
069
070        outerloop: while (true) {
071            XmlPullParser.Event eventType = parser.next();
072            if (eventType == XmlPullParser.Event.START_ELEMENT) {
073                String name = parser.getName();
074                if (name.equals(PayloadType.NODENAME)) {
075                    desc.addPayloadType(parsePayload(parser));
076                } else {
077                    // TODO: Should be SmackParseException.
078                    throw new IOException("Unknow element \"" + name + "\" in content.");
079                }
080            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
081                if (parser.getDepth() == initialDepth) {
082                    break outerloop;
083                }
084            }
085        }
086        return desc;
087    }
088
089    /**
090     * Return a new instance of this class. Subclasses must overwrite this
091     * method.
092     *
093     * @return the jingle description.
094     */
095    protected abstract JingleDescription getInstance();
096
097    /**
098     * Jingle audio.
099     */
100    public static class Audio extends JingleDescriptionProvider {
101
102        /**
103         * Parse an audio payload type.
104         */
105        @Override
106        public PayloadType parsePayload(final XmlPullParser parser) {
107            PayloadType pte = super.parsePayload(parser);
108            PayloadType.Audio pt = new PayloadType.Audio(pte);
109            int ptClockRate = Integer.parseInt(parser.getAttributeValue("", "clockrate"));
110            pt.setClockRate(ptClockRate);
111
112            return pt;
113        }
114
115        /**
116         * Get a new instance of this object.
117         */
118        @Override
119        protected JingleDescription getInstance() {
120            return new JingleDescription.Audio();
121        }
122    }
123}