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