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 org.jivesoftware.smack.packet.IQ;
021import org.jivesoftware.smack.provider.IQProvider;
022import org.jivesoftware.smackx.disco.packet.DiscoverItems;
023import org.xmlpull.v1.XmlPullParser;
024
025/**
026* The DiscoverInfoProvider parses Service Discovery items packets.
027*
028* @author Gaston Dombiak
029*/
030public class DiscoverItemsProvider implements IQProvider {
031
032    public IQ parseIQ(XmlPullParser parser) throws Exception {
033        DiscoverItems discoverItems = new DiscoverItems();
034        boolean done = false;
035        DiscoverItems.Item item;
036        String jid = "";
037        String name = "";
038        String action = "";
039        String node = "";
040        discoverItems.setNode(parser.getAttributeValue("", "node"));
041        while (!done) {
042            int eventType = parser.next();
043
044            if (eventType == XmlPullParser.START_TAG && "item".equals(parser.getName())) {
045                // Initialize the variables from the parsed XML
046                jid = parser.getAttributeValue("", "jid");
047                name = parser.getAttributeValue("", "name");
048                node = parser.getAttributeValue("", "node");
049                action = parser.getAttributeValue("", "action");
050            }
051            else if (eventType == XmlPullParser.END_TAG && "item".equals(parser.getName())) {
052                // Create a new Item and add it to DiscoverItems.
053                item = new DiscoverItems.Item(jid);
054                item.setName(name);
055                item.setNode(node);
056                item.setAction(action);
057                discoverItems.addItem(item);
058            }
059            else if (eventType == XmlPullParser.END_TAG && "query".equals(parser.getName())) {
060                done = true;
061            }
062        }
063
064        return discoverItems;
065    }
066}