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