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