1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util;
16  
17  import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
18  import com.liferay.portal.kernel.json.JSONArray;
19  import com.liferay.portal.kernel.json.JSONFactoryUtil;
20  import com.liferay.portal.kernel.json.JSONObject;
21  import com.liferay.portal.kernel.util.HtmlUtil;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.kernel.util.StringPool;
24  
25  import java.util.List;
26  
27  /**
28   * <a href="Autocomplete.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class Autocomplete {
33  
34      public static JSONArray arrayToJson(String[] array, int max) {
35          return arrayToJson(_singleToPairArray(array), max);
36      }
37  
38      public static JSONArray arrayToJson(String[][] array, int max) {
39          if (max <= 0) {
40              max = array.length;
41          }
42  
43          JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
44  
45          for (int i = 0; (i < array.length) && (i < max); i++) {
46              String text = array[i][0];
47              String value = array[i][1];
48  
49              JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
50  
51              jsonObj.put("text", text);
52              jsonObj.put("value", value);
53  
54              jsonArray.put(jsonObj);
55          }
56  
57          return jsonArray;
58      }
59  
60      public static String arrayToXml(String[] array, int max) {
61          return arrayToXml(_singleToPairArray(array), max);
62      }
63  
64      public static String arrayToXml(String[][] array, int max) {
65          if (max <= 0) {
66              max = array.length;
67          }
68  
69          StringBundler sb = new StringBundler(array.length * 8 + 3);
70  
71          sb.append("<?xml version=\"1.0\"?>");
72  
73          sb.append("<ajaxresponse>");
74  
75          for (int i = 0; (i < array.length) && (i < max); i++) {
76              String text = array[i][0];
77              String value = array[i][1];
78  
79              sb.append("<item>");
80              sb.append("<text><![CDATA[");
81              sb.append(text);
82              sb.append("]]></text>");
83              sb.append("<value><![CDATA[");
84              sb.append(value);
85              sb.append("]]></value>");
86              sb.append("</item>");
87          }
88  
89          sb.append("</ajaxresponse>");
90  
91          return sb.toString();
92      }
93  
94      public static String[][] listToArray(
95          List<?> list, String textParam, String valueParam) {
96  
97          String[][] array = new String[list.size()][2];
98  
99          for (int i = 0; i < list.size(); i++) {
100             Object bean = list.get(i);
101 
102             Object text = BeanPropertiesUtil.getObject(bean, textParam);
103 
104             if (text == null) {
105                 text = StringPool.BLANK;
106             }
107 
108             Object value = BeanPropertiesUtil.getObject(bean, valueParam);
109 
110             if (value == null) {
111                 value = StringPool.BLANK;
112             }
113 
114             array[i][0] = text.toString();
115             array[i][1] = value.toString();
116         }
117 
118         return array;
119     }
120 
121     public static JSONArray listToJson(
122         List<?> list, String textParam, String valueParam) {
123 
124         return arrayToJson(listToArray(list, textParam, valueParam), -1);
125     }
126 
127     public static String listToXml(
128         List<?> list, String textParam, String valueParam) {
129 
130         return arrayToXml(listToArray(list, textParam, valueParam), -1);
131     }
132 
133     private static String[][] _singleToPairArray(String[] array) {
134         String[][] pairArray = new String[array.length][2];
135 
136         for (int i = 0; i < array.length; i++) {
137             pairArray[i][0] = HtmlUtil.escape(array[i]);
138             pairArray[i][1] = array[i];
139         }
140 
141         return pairArray;
142     }
143 
144 }