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 = 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 JinglePayloadType(new PayloadType(ptId, ptName, ptChannels));
061    }
062
063    /**
064     * Parse a iq/jingle/description element.
065     *
066     * @param parser the input to parse
067     * @return a description element
068     * @throws IOException if an I/O error occurred.
069     * @throws XmlPullParserException if an error in the XML parser occurred.
070     */
071    @Override
072    public JingleContentDescription parse(XmlPullParser parser,
073                    int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException,
074                    IOException {
075        boolean done = false;
076        JingleContentDescription desc = getInstance();
077
078        while (!done) {
079            XmlPullParser.Event eventType = parser.next();
080            String name = parser.getName();
081
082            if (eventType == XmlPullParser.Event.START_ELEMENT) {
083                if (name.equals(JingleContentDescription.JinglePayloadType.NODENAME)) {
084                    desc.addJinglePayloadType(parsePayload(parser));
085                } else {
086                    // TODO: Should be SmackParseException.
087                    throw new IOException("Unknow element \"" + name + "\" in content.");
088                }
089            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
090                if (name.equals(JingleContentDescription.NODENAME)) {
091                    done = true;
092                }
093            }
094        }
095        return desc;
096    }
097
098    /**
099     * Return a new instance of this class. Subclasses must overwrite this
100     * method.
101     *
102     * @return the jingle content description.
103     */
104    protected abstract JingleContentDescription getInstance();
105
106    /**
107     * Jingle audio.
108     */
109    public static class Audio extends JingleContentDescriptionProvider {
110
111        /**
112         * Parse an audio payload type.
113         */
114        @Override
115        public JinglePayloadType parsePayload(final XmlPullParser parser) {
116            JinglePayloadType pte = super.parsePayload(parser);
117            PayloadType.Audio pt = new PayloadType.Audio(pte.getPayloadType());
118            int ptClockRate = 0;
119
120            try {
121                ptClockRate = Integer.parseInt(parser.getAttributeValue("", "clockrate"));
122            } catch (Exception e) {
123            }
124            pt.setClockRate(ptClockRate);
125
126            return new JinglePayloadType.Audio(pt);
127        }
128
129        /**
130         * Get a new instance of this object.
131         */
132        @Override
133        protected JingleContentDescription getInstance() {
134            return new JingleContentDescription.Audio();
135        }
136    }
137}