001/**
002 *
003 * Copyright 2003-2007 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 */
017
018package org.jivesoftware.smackx.disco.provider;
019
020import org.jivesoftware.smack.packet.IQ;
021import org.jivesoftware.smack.provider.IQProvider;
022import org.jivesoftware.smack.util.PacketParserUtils;
023import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
024import org.xmlpull.v1.XmlPullParser;
025
026/**
027* The DiscoverInfoProvider parses Service Discovery information packets.
028*
029* @author Gaston Dombiak
030*/
031public class DiscoverInfoProvider implements IQProvider {
032
033    public IQ parseIQ(XmlPullParser parser) throws Exception {
034        DiscoverInfo discoverInfo = new DiscoverInfo();
035        boolean done = false;
036        DiscoverInfo.Identity identity = null;
037        String category = "";
038        String name = "";
039        String type = "";
040        String variable = "";
041        String lang = "";
042        discoverInfo.setNode(parser.getAttributeValue("", "node"));
043        while (!done) {
044            int eventType = parser.next();
045            if (eventType == XmlPullParser.START_TAG) {
046                if (parser.getName().equals("identity")) {
047                    // Initialize the variables from the parsed XML
048                    category = parser.getAttributeValue("", "category");
049                    name = parser.getAttributeValue("", "name");
050                    type = parser.getAttributeValue("", "type");
051                    lang = parser.getAttributeValue(parser.getNamespace("xml"), "lang");
052                }
053                else if (parser.getName().equals("feature")) {
054                    // Initialize the variables from the parsed XML
055                    variable = parser.getAttributeValue("", "var");
056                }
057                // Otherwise, it must be a packet extension.
058                else {
059                    discoverInfo.addExtension(PacketParserUtils.parsePacketExtension(parser
060                            .getName(), parser.getNamespace(), parser));
061                }
062            } else if (eventType == XmlPullParser.END_TAG) {
063                if (parser.getName().equals("identity")) {
064                    // Create a new identity and add it to the discovered info.
065                    identity = new DiscoverInfo.Identity(category, name, type);
066                    if (lang != null)
067                        identity.setLanguage(lang);
068                    discoverInfo.addIdentity(identity);
069                }
070                if (parser.getName().equals("feature")) {
071                    // Create a new feature and add it to the discovered info.
072                    discoverInfo.addFeature(variable);
073                }
074                if (parser.getName().equals("query")) {
075                    done = true;
076                }
077            }
078        }
079
080        return discoverInfo;
081    }
082}