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