KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SearchIQProvider


1 import org.jivesoftware.smack.provider.IQProvider;
2 import org.jivesoftware.smack.packet.DefaultPacketExtension;
3 import org.jivesoftware.smack.packet.IQ;
4 import org.jivesoftware.smack.provider.ProviderManager;
5
6 /** Provider for jabber:iq:search IQ packets.*/
7 public final class SearchIQProvider implements IQProvider{
8     
9     /** Creates an instance of SearchIQProvider.*/
10     public SearchIQProvider(){
11         // nothing to do
12
}
13     
14     /** Programmatically installs an SearchIQProvider.*/
15     public static void install(){
16         ProviderManager.addIQProvider("query","jabber:iq:search",new SearchIQProvider());
17     }
18     
19     /** Parse the IQ subpacket.
20     * @return An instance of SearchIQ.
21     * @exception Exception If theres a problem with the parsing.
22     * @see SearchIQ*/

23     public IQ parseIQ(org.xmlpull.v1.XmlPullParser parser) throws Exception JavaDoc{
24         SearchIQ result=new SearchIQ();
25         
26         DefaultPacketExtension item=null;
27         String JavaDoc currentTag=null;
28         String JavaDoc currentValue=null;
29         String JavaDoc key=null;
30         int event=parser.getEventType();
31         while (!(event==parser.END_TAG && parser.getName().equals("query"))){
32             if(event==parser.START_TAG){
33                 if(item==null && parser.getName().equals("item") && (!parser.isEmptyElementTag())){
34                     item=new DefaultPacketExtension("item","");
35                     key=parser.getAttributeValue(0);
36                     currentTag=null;
37                 }
38                 else{
39                     currentTag=parser.getName();
40                     if(item==null && parser.isEmptyElementTag()){
41                         if(!currentTag.equals("instructions")){
42                             result.addField(currentTag);
43                             currentTag=null;
44                         }
45                     }
46                 }
47             }
48             if(event==parser.TEXT && currentTag!=null){
49                 if(item!=null){
50                     item.setValue(currentTag,parser.getText());
51                 }
52                 else{
53                     if(!currentTag.equals("instructions")){
54                         result.setField(currentTag,parser.getText());
55                     }
56                     else{
57                         result.setInstructions(parser.getText());
58                     }
59                 }
60                 currentTag=null;
61             }
62             if(event==parser.END_TAG && parser.getName().equals("item") && item!=null){
63                 result.addItem(key,item);
64                 item=null;
65             }
66             event=parser.next();
67         }
68         return result;
69     }
70 }
Popular Tags