1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util;
21  
22  import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
23  import com.liferay.portal.kernel.json.JSONArray;
24  import com.liferay.portal.kernel.json.JSONFactoryUtil;
25  import com.liferay.portal.kernel.json.JSONObject;
26  import com.liferay.portal.kernel.util.HtmlUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  
29  import java.util.List;
30  
31  /**
32   * <a href="Autocomplete.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class Autocomplete {
38  
39      public static JSONArray arrayToJson(String[] array, int max) {
40          return arrayToJson(_singleToPairArray(array), max);
41      }
42  
43      public static JSONArray arrayToJson(String[][] array, int max) {
44          if (max <= 0) {
45              max = array.length;
46          }
47  
48          JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
49  
50          for (int i = 0; (i < array.length) && (i < max); i++) {
51              String text = array[i][0];
52              String value = array[i][1];
53  
54              JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
55  
56              jsonObj.put("text", text);
57              jsonObj.put("value", value);
58  
59              jsonArray.put(jsonObj);
60          }
61  
62          return jsonArray;
63      }
64  
65      public static String arrayToXml(String[] array, int max) {
66          return arrayToXml(_singleToPairArray(array), max);
67      }
68  
69      public static String arrayToXml(String[][] array, int max) {
70          if (max <= 0) {
71              max = array.length;
72          }
73  
74          StringBuilder sb = new StringBuilder();
75  
76          sb.append("<?xml version=\"1.0\"?>");
77  
78          sb.append("<ajaxresponse>");
79  
80          for (int i = 0; (i < array.length) && (i < max); i++) {
81              String text = array[i][0];
82              String value = array[i][1];
83  
84              sb.append("<item>");
85              sb.append("<text><![CDATA[");
86              sb.append(text);
87              sb.append("]]></text>");
88              sb.append("<value><![CDATA[");
89              sb.append(value);
90              sb.append("]]></value>");
91              sb.append("</item>");
92          }
93  
94          sb.append("</ajaxresponse>");
95  
96          return sb.toString();
97      }
98  
99      public static String[][] listToArray(
100         List<?> list, String textParam, String valueParam) {
101 
102         String[][] array = new String[list.size()][2];
103 
104         for (int i = 0; i < list.size(); i++) {
105             Object bean = list.get(i);
106 
107             Object text = BeanPropertiesUtil.getObject(bean, textParam);
108 
109             if (text == null) {
110                 text = StringPool.BLANK;
111             }
112 
113             Object value = BeanPropertiesUtil.getObject(bean, valueParam);
114 
115             if (value == null) {
116                 value = StringPool.BLANK;
117             }
118 
119             array[i][0] = text.toString();
120             array[i][1] = value.toString();
121         }
122 
123         return array;
124     }
125 
126     public static JSONArray listToJson(
127         List<?> list, String textParam, String valueParam) {
128 
129         return arrayToJson(listToArray(list, textParam, valueParam), -1);
130     }
131 
132     public static String listToXml(
133         List<?> list, String textParam, String valueParam) {
134 
135         return arrayToXml(listToArray(list, textParam, valueParam), -1);
136     }
137 
138     private static String[][] _singleToPairArray(String[] array) {
139         String[][] pairArray = new String[array.length][2];
140 
141         for (int i = 0; i < array.length; i++) {
142             pairArray[i][0] = HtmlUtil.escape(array[i]);
143             pairArray[i][1] = array[i];
144         }
145 
146         return pairArray;
147     }
148 
149 }