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