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