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