KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SearchIQ


1 import org.jivesoftware.smack.packet.*;
2 import java.util.*;
3
4 /** IQ packet for jabber:iq:search.*/
5 public final class SearchIQ extends IQ{
6     
7     private final Hashtable items=new Hashtable();
8     private String JavaDoc instructions=null;
9     private final Vector fields=new Vector();
10     private final Hashtable fieldvalues=new Hashtable();
11     private int numberofitems=0;
12     
13     public void addItem(String JavaDoc jid, DefaultPacketExtension item){
14         items.put(jid,item);
15         numberofitems++;
16     }
17     
18     public int getNumberOfItems(){
19         return numberofitems;
20     }
21     
22     public DefaultPacketExtension getItem(String JavaDoc jid){
23         return (DefaultPacketExtension)items.get(jid);
24     }
25     
26     public String JavaDoc getInstructions(){
27         return instructions;
28     }
29     
30     public void setInstructions(String JavaDoc value){
31         instructions=value;
32     }
33     
34     public void addField(String JavaDoc name){
35         fields.add(name);
36     }
37     
38     public void setField(String JavaDoc name,String JavaDoc value){
39         fieldvalues.put(name,value);
40     }
41     
42     public int getNumberOfFields(){
43         return fields.size();
44     }
45     
46     public Enumeration getFields(){
47         return fields.elements();
48     }
49     
50     public Enumeration getJids(){
51         return items.keys();
52     }
53     
54     public String JavaDoc getChildElementXML(){
55         Iterator i=getExtensions();
56         StringBuffer JavaDoc result=new StringBuffer JavaDoc();
57         PacketExtension pe;
58         if(i!=null){
59             while(i.hasNext()){
60                 pe=(PacketExtension)i.next();
61                 result.append(pe.toXML());
62             }
63         }
64         if(items.isEmpty() && instructions==null && fields.size()==0){
65             result.append("<query xmlns=\"jabber:iq:search\"/>");
66             return result.toString();
67         }
68         result.append("<query xmlns=\"jabber:iq:search\">");
69         if(instructions!=null){
70             result.append("<instructions>"+instructions+"</instructions>");
71         }
72         
73         String JavaDoc value=null;
74         for(int i2=0;i2<fields.size();i2++){
75             value=(String JavaDoc)fieldvalues.get((String JavaDoc) fields.elementAt(i2));
76             if(value==null){
77                 result.append("<"+(String JavaDoc) fields.elementAt(i2)+"/>");
78             }
79             else{
80                 result.append("<"+(String JavaDoc) fields.elementAt(i2)+">"+value+"</"+(String JavaDoc) fields.elementAt(i2)+">");
81             }
82         }
83         
84         if(!items.isEmpty()){
85             
86             final Enumeration e=items.keys();
87             DefaultPacketExtension dpe=null;
88             String JavaDoc key=null;
89             String JavaDoc tag=null;
90             while(e.hasMoreElements()){
91                 key=(String JavaDoc) e.nextElement();
92                 dpe=getItem(key);
93                 result.append("<item jid='"+key+"'>");
94                 i=dpe.getNames();
95                 while(i.hasNext()){
96                     tag=(String JavaDoc)i.next();
97                     result.append("<"+tag+">"+dpe.getValue(tag)+"</"+tag+">");
98                 }
99                 result.append("</item>");
100             }
101             
102         }
103         result.append("</query>");
104         return result.toString();
105     }
106     
107 }
Popular Tags