001    /*
002     Copyright (C) 2003 Adam Olsen
003     
004     This program is free software; you can redistribute it and/or modify
005     it under the terms of the GNU General Public License as published by
006     the Free Software Foundation; either version 1, or (at your option)
007     any later version.
008     
009     This program is distributed in the hope that it will be useful,
010     but WITHOUT ANY WARRANTY; without even the implied warranty of
011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     GNU General Public License for more details.
013     
014     You should have received a copy of the GNU General Public License
015     along with this program; if not, write to the Free Software
016     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017     */
018    package com.valhalla.jbother.jabber.smack;
019    
020    import org.jivesoftware.smack.packet.IQ;
021    import org.jivesoftware.smackx.*;
022    import org.jivesoftware.smackx.packet.*;
023    
024    import java.util.ArrayList;
025    import java.util.HashMap;
026    import java.util.Iterator;
027    
028    /**
029     * class implementing packets used to search Jabber User Directory
030     *
031     * searching process, according to JEP-0055 looks as following:
032     *   1) query the information repository regarding the possible search fields
033     *   2) send search query
034     *   3) receive search results
035     *
036     * Because search fields are not standard, they are kept in hash map: keys are names of the
037     * fields and values are, well, values of those fields :-)
038     *
039     * TODO: search using data forms
040     *
041     * @author Lukasz Wiechec
042     * @version 1.0
043     * @created Apr 1, 2005 12:53:47 PM
044     */
045    
046    public class Search extends IQ {
047        private static String fId = "$Id$";
048        
049        public final String NAMESPACE = "jabber:iq:search";
050        
051        String instructions = null;
052        
053        HashMap searchFields = null;
054        ArrayList items = null;
055        
056        DataForm searchDataForm = null;
057        
058        public Search(String aInstructions, HashMap aSearchFields) {
059            instructions = aInstructions;
060            searchFields = aSearchFields;
061        }
062        
063        public Search() {
064        }
065        
066        
067        public String getInstructions() {
068            return instructions;
069        }
070        
071        public void setInstructions(String instructions) {
072            this.instructions = instructions;
073        }
074        
075        public HashMap getSearchFields() {
076            return searchFields;
077        }
078        
079        public void setSearchFields(HashMap searchFields) {
080            this.searchFields = searchFields;
081        }
082        
083        public ArrayList getItems() {
084            return items;
085        }
086        
087        public void setItems(ArrayList items) {
088            this.items = items;
089        }
090        
091        public DataForm getSearchDataForm() {
092            return searchDataForm;
093        }
094        
095        public void setSearchDataForm(DataForm searchDataForm) {
096            this.searchDataForm = searchDataForm;
097        }
098        
099        public void addItem(Item aItem) {
100            if(items == null) {
101                items = new ArrayList();
102            }
103            items.add(aItem);
104        }
105        
106        public String getChildElementXML() {
107            StringBuffer buf = new StringBuffer();
108            buf.append("<query xmlns=\"" + NAMESPACE + "\">");
109            if(instructions != null) {
110                buf.append("<instructions>" + instructions + "</instructions>");
111            }
112            if(searchFields != null) {
113                Iterator iKeys = searchFields.keySet().iterator();
114                while(iKeys.hasNext()) {
115                    String field = (String)iKeys.next();
116                    buf.append( "<" + field + ">" );
117                    buf.append( searchFields.get(field) );
118                    buf.append( "</" + field + ">" );
119                }
120            }
121            if(items != null) {
122                Iterator iItems = items.iterator();
123                while(iItems.hasNext()) {
124                    buf.append(((Search.Item)iItems.next()).toXML());
125                }
126            }
127            if(searchDataForm != null) {
128                buf.append(searchDataForm.toXML());
129            }
130            buf.append("</query>");
131            return buf.toString();
132        }
133        
134        /**
135         * inner class for representing "items", ie. the elements that are returned
136         * from querying the JUD
137         */
138        public static class Item {
139            private String jid;
140            HashMap attributes;
141            
142            public Item(String jid) {
143                this.jid = jid;
144            }
145            
146            public String getJid() {
147                return jid;
148            }
149            
150            public void setJid(String jid) {
151                this.jid = jid;
152            }
153            
154            public HashMap getAttributes() {
155                return attributes;
156            }
157            
158            public void setAttributes(HashMap attributes) {
159                this.attributes = attributes;
160            }
161            
162            public String toXML() {
163                StringBuffer out = new StringBuffer();
164                out.append( "<item jid=\"" + jid + "\">" );
165                Iterator iAttrs = attributes.keySet().iterator();
166                while(iAttrs.hasNext()) {
167                    String attr = (String)iAttrs.next();
168                    out.append( "<" + attr + ">" + attributes.get(attr) + "</" + attr + ">" );
169                }
170                out.append( "</item>" );
171                return out.toString();
172            }
173            
174        }
175    }