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.provider.IqProvider;
025import org.jivesoftware.smack.util.ParserUtils;
026import org.jivesoftware.smack.xml.XmlPullParser;
027import org.jivesoftware.smack.xml.XmlPullParserException;
028
029import org.jivesoftware.smackx.disco.packet.DiscoverItems;
030
031import org.jxmpp.JxmppContext;
032import org.jxmpp.jid.Jid;
033
034/**
035* The DiscoverInfoProvider parses Service Discovery items packets.
036*
037* @author Gaston Dombiak
038*/
039public class DiscoverItemsProvider extends IqProvider<DiscoverItems> {
040
041    @Override
042    public DiscoverItems parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext)
043                    throws XmlPullParserException, IOException {
044        DiscoverItems discoverItems = new DiscoverItems();
045        boolean done = false;
046        DiscoverItems.Item item;
047        Jid jid = null;
048        String name = "";
049        String action = "";
050        String node = "";
051        discoverItems.setNode(parser.getAttributeValue("", "node"));
052        while (!done) {
053            XmlPullParser.Event eventType = parser.next();
054
055            if (eventType == XmlPullParser.Event.START_ELEMENT && "item".equals(parser.getName())) {
056                // Initialize the variables from the parsed XML
057                jid = ParserUtils.getJidAttribute(parser, jxmppContext);
058                name = parser.getAttributeValue("", "name");
059                node = parser.getAttributeValue("", "node");
060                action = parser.getAttributeValue("", "action");
061            }
062            else if (eventType == XmlPullParser.Event.END_ELEMENT && "item".equals(parser.getName())) {
063                // Create a new Item and add it to DiscoverItems.
064                item = new DiscoverItems.Item(jid);
065                item.setName(name);
066                item.setNode(node);
067                item.setAction(action);
068                discoverItems.addItem(item);
069            }
070            else if (eventType == XmlPullParser.Event.END_ELEMENT && "query".equals(parser.getName())) {
071                done = true;
072            }
073        }
074
075        return discoverItems;
076    }
077}