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.JingleContentDescription;
028import org.jivesoftware.smackx.jingleold.packet.JingleContentDescription.JinglePayloadType;
029
030/**
031 * Parser for a Jingle description.
032 *
033 * @author Alvaro Saurin
034 */
035public abstract class JingleContentDescriptionProvider extends ExtensionElementProvider<JingleContentDescription> {
036
037    /**
038     * Parse a iq/jingle/description/payload-type element.
039     *
040     * @param parser the input to parse
041     * @return a payload type element
042     */
043    protected JinglePayloadType 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 JinglePayloadType(new PayloadType(ptId, ptName, ptChannels));
055    }
056
057    /**
058     * Parse a iq/jingle/description element.
059     *
060     * @param parser the input to parse
061     * @return a description element
062     * @throws IOException if an I/O error occurred.
063     * @throws XmlPullParserException if an error in the XML parser occurred.
064     */
065    @Override
066    public JingleContentDescription parse(XmlPullParser parser,
067                    int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException,
068                    IOException {
069        JingleContentDescription desc = getInstance();
070
071        outerloop: while (true) {
072            XmlPullParser.Event eventType = parser.next();
073
074            if (eventType == XmlPullParser.Event.START_ELEMENT) {
075                String name = parser.getName();
076                if (name.equals(JingleContentDescription.JinglePayloadType.NODENAME)) {
077                    desc.addJinglePayloadType(parsePayload(parser));
078                } else {
079                    // TODO: Should be SmackParseException.
080                    throw new IOException("Unknow element \"" + name + "\" in content.");
081                }
082            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
083                if (parser.getDepth() == initialDepth) {
084                    break outerloop;
085                }
086            }
087        }
088        return desc;
089    }
090
091    /**
092     * Return a new instance of this class. Subclasses must overwrite this
093     * method.
094     *
095     * @return the jingle content description.
096     */
097    protected abstract JingleContentDescription getInstance();
098
099    /**
100     * Jingle audio.
101     */
102    public static class Audio extends JingleContentDescriptionProvider {
103
104        /**
105         * Parse an audio payload type.
106         */
107        @Override
108        public JinglePayloadType parsePayload(final XmlPullParser parser) {
109            JinglePayloadType pte = super.parsePayload(parser);
110            PayloadType.Audio pt = new PayloadType.Audio(pte.getPayloadType());
111            int ptClockRate = Integer.parseInt(parser.getAttributeValue("", "clockrate"));
112            pt.setClockRate(ptClockRate);
113
114            return new JinglePayloadType.Audio(pt);
115        }
116
117        /**
118         * Get a new instance of this object.
119         */
120        @Override
121        protected JingleContentDescription getInstance() {
122            return new JingleContentDescription.Audio();
123        }
124    }
125}