SimpleUserSearch.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.search;

  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.List;

  21. import org.jivesoftware.smack.packet.IQ;
  22. import org.jivesoftware.smack.xml.XmlPullParser;
  23. import org.jivesoftware.smack.xml.XmlPullParserException;

  24. import org.jivesoftware.smackx.xdata.FormField;
  25. import org.jivesoftware.smackx.xdata.packet.DataForm;

  26. /**
  27.  * SimpleUserSearch is used to support the non-dataform type of XEP 55. This provides
  28.  * the mechanism for allowing always type ReportedData to be returned by any search result,
  29.  * regardless of the form of the data returned from the server.
  30.  *
  31.  * @author Derek DeMoro
  32.  */
  33. class SimpleUserSearch extends IQ {

  34.     public static final String ELEMENT = UserSearch.ELEMENT;
  35.     public static final String NAMESPACE = UserSearch.NAMESPACE;

  36.     private DataForm form;
  37.     private ReportedData data;

  38.     SimpleUserSearch() {
  39.         super(ELEMENT, NAMESPACE);
  40.     }

  41.     public void setForm(DataForm form) {
  42.         this.form = form;
  43.     }

  44.     public ReportedData getReportedData() {
  45.         return data;
  46.     }

  47.     @Override
  48.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  49.         buf.rightAngleBracket();
  50.         buf.append(getItemsToSearch());
  51.         return buf;
  52.     }

  53.     private String getItemsToSearch() {
  54.         StringBuilder buf = new StringBuilder();

  55.         if (form == null) {
  56.             form = DataForm.from(this);
  57.         }

  58.         if (form == null) {
  59.             return "";
  60.         }

  61.         for (FormField field : form.getFields()) {
  62.             String name = field.getFieldName();
  63.             String value = getSingleValue(field);
  64.             if (value.trim().length() > 0) {
  65.                 buf.append('<').append(name).append('>').append(value).append("</").append(name).append('>');
  66.             }
  67.         }

  68.         return buf.toString();
  69.     }

  70.     private static String getSingleValue(FormField formField) {
  71.         List<String> values = formField.getValuesAsString();
  72.         if (values.isEmpty()) {
  73.             return "";
  74.         } else {
  75.             return values.get(0);
  76.         }
  77.     }

  78.     protected void parseItems(XmlPullParser parser) throws XmlPullParserException, IOException {
  79.         ReportedData data = new ReportedData();
  80.         data.addColumn(new ReportedData.Column("JID", "jid", FormField.Type.text_single));

  81.         boolean done = false;

  82.         List<ReportedData.Field> fields = new ArrayList<>();
  83.         while (!done) {
  84.             if (parser.getAttributeCount() > 0) {
  85.                 String jid = parser.getAttributeValue("", "jid");
  86.                 List<String> valueList = new ArrayList<>();
  87.                 valueList.add(jid);
  88.                 ReportedData.Field field = new ReportedData.Field("jid", valueList);
  89.                 fields.add(field);
  90.             }

  91.             XmlPullParser.Event eventType = parser.next();

  92.             if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getName().equals("item")) {
  93.                 fields = new ArrayList<>();
  94.             }
  95.             else if (eventType == XmlPullParser.Event.END_ELEMENT && parser.getName().equals("item")) {
  96.                 ReportedData.Row row = new ReportedData.Row(fields);
  97.                 data.addRow(row);
  98.             }
  99.             else if (eventType == XmlPullParser.Event.START_ELEMENT) {
  100.                 String name = parser.getName();
  101.                 String value = parser.nextText();

  102.                 List<String> valueList = new ArrayList<>();
  103.                 valueList.add(value);
  104.                 ReportedData.Field field = new ReportedData.Field(name, valueList);
  105.                 fields.add(field);

  106.                 boolean exists = false;
  107.                 for (ReportedData.Column column : data.getColumns()) {
  108.                     if (column.getVariable().equals(name)) {
  109.                         exists = true;
  110.                         break;
  111.                     }
  112.                 }

  113.                 // Column name should be the same
  114.                 if (!exists) {
  115.                     ReportedData.Column column = new ReportedData.Column(name, name, FormField.Type.text_single);
  116.                     data.addColumn(column);
  117.                 }
  118.             }
  119.             else if (eventType == XmlPullParser.Event.END_ELEMENT) {
  120.                 if (parser.getName().equals("query")) {
  121.                     done = true;
  122.                 }
  123.             }

  124.         }

  125.         this.data = data;
  126.     }


  127. }