Shuyi Chen | d7955ce | 2013-05-22 14:51:55 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2003-2007 Jive Software. |
| 3 | * |
| 4 | * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.jivesoftware.smackx.search; |
| 18 | |
| 19 | import org.jivesoftware.smack.packet.IQ; |
| 20 | import org.jivesoftware.smackx.Form; |
| 21 | import org.jivesoftware.smackx.FormField; |
| 22 | import org.jivesoftware.smackx.ReportedData; |
| 23 | import org.xmlpull.v1.XmlPullParser; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Iterator; |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * SimpleUserSearch is used to support the non-dataform type of JEP 55. This provides |
| 31 | * the mechanism for allowing always type ReportedData to be returned by any search result, |
| 32 | * regardless of the form of the data returned from the server. |
| 33 | * |
| 34 | * @author Derek DeMoro |
| 35 | */ |
| 36 | class SimpleUserSearch extends IQ { |
| 37 | |
| 38 | private Form form; |
| 39 | private ReportedData data; |
| 40 | |
| 41 | public void setForm(Form form) { |
| 42 | this.form = form; |
| 43 | } |
| 44 | |
| 45 | public ReportedData getReportedData() { |
| 46 | return data; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | public String getChildElementXML() { |
| 51 | StringBuilder buf = new StringBuilder(); |
| 52 | buf.append("<query xmlns=\"jabber:iq:search\">"); |
| 53 | buf.append(getItemsToSearch()); |
| 54 | buf.append("</query>"); |
| 55 | return buf.toString(); |
| 56 | } |
| 57 | |
| 58 | private String getItemsToSearch() { |
| 59 | StringBuilder buf = new StringBuilder(); |
| 60 | |
| 61 | if (form == null) { |
| 62 | form = Form.getFormFrom(this); |
| 63 | } |
| 64 | |
| 65 | if (form == null) { |
| 66 | return ""; |
| 67 | } |
| 68 | |
| 69 | Iterator<FormField> fields = form.getFields(); |
| 70 | while (fields.hasNext()) { |
| 71 | FormField field = fields.next(); |
| 72 | String name = field.getVariable(); |
| 73 | String value = getSingleValue(field); |
| 74 | if (value.trim().length() > 0) { |
| 75 | buf.append("<").append(name).append(">").append(value).append("</").append(name).append(">"); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return buf.toString(); |
| 80 | } |
| 81 | |
| 82 | private static String getSingleValue(FormField formField) { |
| 83 | Iterator<String> values = formField.getValues(); |
| 84 | while (values.hasNext()) { |
| 85 | return values.next(); |
| 86 | } |
| 87 | return ""; |
| 88 | } |
| 89 | |
| 90 | protected void parseItems(XmlPullParser parser) throws Exception { |
| 91 | ReportedData data = new ReportedData(); |
| 92 | data.addColumn(new ReportedData.Column("JID", "jid", "text-single")); |
| 93 | |
| 94 | boolean done = false; |
| 95 | |
| 96 | List<ReportedData.Field> fields = new ArrayList<ReportedData.Field>(); |
| 97 | while (!done) { |
| 98 | if (parser.getAttributeCount() > 0) { |
| 99 | String jid = parser.getAttributeValue("", "jid"); |
| 100 | List<String> valueList = new ArrayList<String>(); |
| 101 | valueList.add(jid); |
| 102 | ReportedData.Field field = new ReportedData.Field("jid", valueList); |
| 103 | fields.add(field); |
| 104 | } |
| 105 | |
| 106 | int eventType = parser.next(); |
| 107 | |
| 108 | if (eventType == XmlPullParser.START_TAG && parser.getName().equals("item")) { |
| 109 | fields = new ArrayList<ReportedData.Field>(); |
| 110 | } |
| 111 | else if (eventType == XmlPullParser.END_TAG && parser.getName().equals("item")) { |
| 112 | ReportedData.Row row = new ReportedData.Row(fields); |
| 113 | data.addRow(row); |
| 114 | } |
| 115 | else if (eventType == XmlPullParser.START_TAG) { |
| 116 | String name = parser.getName(); |
| 117 | String value = parser.nextText(); |
| 118 | |
| 119 | List<String> valueList = new ArrayList<String>(); |
| 120 | valueList.add(value); |
| 121 | ReportedData.Field field = new ReportedData.Field(name, valueList); |
| 122 | fields.add(field); |
| 123 | |
| 124 | boolean exists = false; |
| 125 | Iterator<ReportedData.Column> cols = data.getColumns(); |
| 126 | while (cols.hasNext()) { |
| 127 | ReportedData.Column column = cols.next(); |
| 128 | if (column.getVariable().equals(name)) { |
| 129 | exists = true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Column name should be the same |
| 134 | if (!exists) { |
| 135 | ReportedData.Column column = new ReportedData.Column(name, name, "text-single"); |
| 136 | data.addColumn(column); |
| 137 | } |
| 138 | } |
| 139 | else if (eventType == XmlPullParser.END_TAG) { |
| 140 | if (parser.getName().equals("query")) { |
| 141 | done = true; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | } |
| 146 | |
| 147 | this.data = data; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | } |