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 */
017package org.jivesoftware.smackx.search;
018
019import java.io.IOException;
020
021import org.jivesoftware.smack.packet.IQ;
022import org.jivesoftware.smack.packet.IqData;
023import org.jivesoftware.smack.packet.SimpleIQ;
024import org.jivesoftware.smack.packet.XmlEnvironment;
025import org.jivesoftware.smack.parsing.SmackParsingException;
026import org.jivesoftware.smack.provider.IqProvider;
027import org.jivesoftware.smack.util.PacketParserUtils;
028import org.jivesoftware.smack.xml.XmlPullParser;
029import org.jivesoftware.smack.xml.XmlPullParserException;
030
031import org.jxmpp.JxmppContext;
032
033/**
034 * Implements the protocol currently used to search information repositories on the Jabber network. To date, the jabber:iq:search protocol
035 * has been used mainly to search for people who have registered with user directories (e.g., the "Jabber User Directory" hosted at users.jabber.org).
036 * However, the jabber:iq:search protocol is not limited to user directories, and could be used to search other Jabber information repositories
037 * (such as chatroom directories) or even to provide a Jabber interface to conventional search engines.
038 *
039 * The basic functionality is to query an information repository regarding the possible search fields, to send a search query, and to receive search results.
040 *
041 * @author Derek DeMoro
042 */
043public class UserSearch extends SimpleIQ {
044
045    public static final String ELEMENT = QUERY_ELEMENT;
046    public static final String NAMESPACE = "jabber:iq:search";
047
048    /**
049     * Creates a new instance of UserSearch.
050     */
051    public UserSearch() {
052        super(ELEMENT, NAMESPACE);
053    }
054
055    /**
056     * Internal Search service Provider.
057     */
058    public static class Provider extends IqProvider<IQ> {
059
060        // FIXME this provider does return two different types of IQs
061        @Override
062        public IQ parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException, SmackParsingException {
063            UserSearch search = null;
064            SimpleUserSearch simpleUserSearch = new SimpleUserSearch();
065
066            boolean done = false;
067            while (!done) {
068                XmlPullParser.Event eventType = parser.next();
069                if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getName().equals("item")) {
070                    simpleUserSearch.parseItems(parser);
071                    return simpleUserSearch;
072                }
073                else if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getNamespace().equals("jabber:x:data")) {
074                    // Otherwise, it must be a packet extension.
075                    search = new UserSearch();
076                    PacketParserUtils.addExtensionElement(search, parser, xmlEnvironment, jxmppContext);
077                }
078                else if (eventType == XmlPullParser.Event.END_ELEMENT) {
079                    if (parser.getName().equals("query")) {
080                        done = true;
081                    }
082                }
083            }
084
085            if (search != null) {
086                return search;
087            }
088            return simpleUserSearch;
089        }
090    }
091
092}