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 java.io.IOException;
021
022import org.jivesoftware.smack.packet.IqData;
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.parsing.SmackParsingException;
025import org.jivesoftware.smack.provider.IqProvider;
026import org.jivesoftware.smack.util.PacketParserUtils;
027import org.jivesoftware.smack.util.ParserUtils;
028import org.jivesoftware.smack.xml.XmlPullParser;
029import org.jivesoftware.smack.xml.XmlPullParserException;
030
031import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
032import org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder;
033
034/**
035* The DiscoverInfoProvider parses Service Discovery information packets.
036*
037* @author Gaston Dombiak
038*/
039public class DiscoverInfoProvider extends IqProvider<DiscoverInfo> {
040
041    @Override
042    public DiscoverInfo parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
043            throws XmlPullParserException, IOException, SmackParsingException {
044        DiscoverInfoBuilder discoverInfoBuilder = DiscoverInfo.builder(iqData);
045
046        String node = parser.getAttributeValue("node");
047        discoverInfoBuilder.setNode(node);
048
049        outerloop: while (true) {
050            XmlPullParser.Event eventType = parser.next();
051            if (eventType == XmlPullParser.Event.START_ELEMENT) {
052                final String name = parser.getName();
053                final String namespace = parser.getNamespace();
054                if (namespace.equals(DiscoverInfo.NAMESPACE)) {
055                    switch (name) {
056                    case "identity":
057                        String category = parser.getAttributeValue("category");
058                        String identityName = parser.getAttributeValue("name");
059                        String type = parser.getAttributeValue("type");
060                        String lang = ParserUtils.getXmlLang(parser);
061                        DiscoverInfo.Identity identity = new DiscoverInfo.Identity(category, type, identityName, lang);
062                        discoverInfoBuilder.addIdentity(identity);
063                        break;
064                    case "feature":
065                        String feature = parser.getAttributeValue("var");
066                        discoverInfoBuilder.addFeature(feature);
067                        break;
068                    }
069                }
070                // Otherwise, it must be a packet extension.
071                else {
072                    PacketParserUtils.addExtensionElement(discoverInfoBuilder, parser, xmlEnvironment);
073                }
074            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
075                if (parser.getDepth() == initialDepth) {
076                    break outerloop;
077                }
078            }
079        }
080
081        DiscoverInfo discoverInfo = discoverInfoBuilder.buildWithoutValidiation();
082        return discoverInfo;
083    }
084}