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.jingle.provider;
018
019import org.jivesoftware.smack.packet.PacketExtension;
020import org.jivesoftware.smack.provider.PacketExtensionProvider;
021import org.jivesoftware.smackx.jingle.media.PayloadType;
022import org.jivesoftware.smackx.jingle.packet.JingleContentDescription;
023import org.jivesoftware.smackx.jingle.packet.JingleContentDescription.JinglePayloadType;
024import org.xmlpull.v1.XmlPullParser;
025
026/**
027 * Parser for a Jingle description
028 *
029 * @author Alvaro Saurin <alvaro.saurin@gmail.com>
030 */
031public abstract class JingleContentDescriptionProvider implements PacketExtensionProvider {
032
033    /**
034     * Default constructor
035     */
036    public JingleContentDescriptionProvider() {
037        super();
038    }
039
040    /**
041     * Parse a iq/jingle/description/payload-type element.
042     *
043     * @param parser the input to parse
044     * @return a payload type element
045     * @throws Exception
046     */
047    protected JinglePayloadType parsePayload(final XmlPullParser parser)
048            throws Exception {
049        int ptId = 0;
050        String ptName;
051        int ptChannels = 0;
052
053        try {
054            ptId = Integer.parseInt(parser.getAttributeValue("", "id"));
055        } catch (Exception e) {
056        }
057
058        ptName = parser.getAttributeValue("", "name");
059
060        try {
061            ptChannels = Integer.parseInt(parser.getAttributeValue("", "channels"));
062        } catch (Exception e) {
063        }
064
065        return new JinglePayloadType(new PayloadType(ptId, ptName, ptChannels));
066    }
067
068    /**
069     * Parse a iq/jingle/description element.
070     *
071     * @param parser the input to parse
072     * @return a description element
073     * @throws Exception
074     */
075    public PacketExtension parseExtension(final XmlPullParser parser) throws Exception {
076        boolean done = false;
077        JingleContentDescription desc = getInstance();
078
079        while (!done) {
080            int eventType = parser.next();
081            String name = parser.getName();
082
083            if (eventType == XmlPullParser.START_TAG) {
084                if (name.equals(JingleContentDescription.JinglePayloadType.NODENAME)) {
085                    desc.addJinglePayloadType(parsePayload(parser));
086                } else {
087                    throw new Exception("Unknow element \"" + name + "\" in content.");
088                }
089            } else if (eventType == XmlPullParser.END_TAG) {
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    protected abstract JingleContentDescription getInstance();
103
104    /**
105     * Jingle audio
106     */
107    public static class Audio extends JingleContentDescriptionProvider {
108
109        /**
110         * Default constructor
111         */
112        public Audio() {
113            super();
114        }
115
116        /**
117         * Parse an audio payload type.
118         */
119        public JinglePayloadType parsePayload(final XmlPullParser parser)
120                throws Exception {
121            JinglePayloadType pte = super.parsePayload(parser);
122            PayloadType.Audio pt = new PayloadType.Audio(pte.getPayloadType());
123            int ptClockRate = 0;
124
125            try {
126                ptClockRate = Integer.parseInt(parser.getAttributeValue("", "clockrate"));
127            } catch (Exception e) {
128            }
129            pt.setClockRate(ptClockRate);
130
131            return new JinglePayloadType.Audio(pt);
132        }
133
134        /**
135         * Get a new instance of this object.
136         */
137        protected JingleContentDescription getInstance() {
138            return new JingleContentDescription.Audio();
139                }
140        }
141}