1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util;
24  
25  import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
26  import com.liferay.portal.kernel.json.JSONArray;
27  import com.liferay.portal.kernel.json.JSONFactoryUtil;
28  import com.liferay.portal.kernel.json.JSONObject;
29  import com.liferay.portal.kernel.util.HtmlUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  
32  import java.util.List;
33  
34  /**
35   * <a href="Autocomplete.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class Autocomplete {
41  
42      public static JSONArray arrayToJson(String[] array, int max) {
43          return arrayToJson(_singleToPairArray(array), max);
44      }
45  
46      public static JSONArray arrayToJson(String[][] array, int max) {
47          if (max <= 0) {
48              max = array.length;
49          }
50  
51          JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
52  
53          for (int i = 0; (i < array.length) && (i < max); i++) {
54              String text = array[i][0];
55              String value = array[i][1];
56  
57              JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
58  
59              jsonObj.put("text", text);
60              jsonObj.put("value", value);
61  
62              jsonArray.put(jsonObj);
63          }
64  
65          return jsonArray;
66      }
67  
68      public static String arrayToXml(String[] array, int max) {
69          return arrayToXml(_singleToPairArray(array), max);
70      }
71  
72      public static String arrayToXml(String[][] array, int max) {
73          if (max <= 0) {
74              max = array.length;
75          }
76  
77          StringBuilder sb = new StringBuilder();
78  
79          sb.append("<?xml version=\"1.0\"?>");
80  
81          sb.append("<ajaxresponse>");
82  
83          for (int i = 0; (i < array.length) && (i < max); i++) {
84              String text = array[i][0];
85              String value = array[i][1];
86  
87              sb.append("<item>");
88              sb.append("<text><![CDATA[");
89              sb.append(text);
90              sb.append("]]></text>");
91              sb.append("<value><![CDATA[");
92              sb.append(value);
93              sb.append("]]></value>");
94              sb.append("</item>");
95          }
96  
97          sb.append("</ajaxresponse>");
98  
99          return sb.toString();
100     }
101 
102     public static String[][] listToArray(
103         List<?> list, String textParam, String valueParam) {
104 
105         String[][] array = new String[list.size()][2];
106 
107         for (int i = 0; i < list.size(); i++) {
108             Object bean = list.get(i);
109 
110             Object text = BeanPropertiesUtil.getObject(bean, textParam);
111 
112             if (text == null) {
113                 text = StringPool.BLANK;
114             }
115 
116             Object value = BeanPropertiesUtil.getObject(bean, valueParam);
117 
118             if (value == null) {
119                 value = StringPool.BLANK;
120             }
121 
122             array[i][0] = text.toString();
123             array[i][1] = value.toString();
124         }
125 
126         return array;
127     }
128 
129     public static JSONArray listToJson(
130         List<?> list, String textParam, String valueParam) {
131 
132         return arrayToJson(listToArray(list, textParam, valueParam), -1);
133     }
134 
135     public static String listToXml(
136         List<?> list, String textParam, String valueParam) {
137 
138         return arrayToXml(listToArray(list, textParam, valueParam), -1);
139     }
140 
141     private static String[][] _singleToPairArray(String[] array) {
142         String[][] pairArray = new String[array.length][2];
143 
144         for (int i = 0; i < array.length; i++) {
145             pairArray[i][0] = HtmlUtil.escape(array[i]);
146             pairArray[i][1] = array[i];
147         }
148 
149         return pairArray;
150     }
151 
152 }