1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class Autocomplete {
40  
41      public static JSONArray arrayToJson(String[] array, int max) {
42          return arrayToJson(_singleToPairArray(array), max);
43      }
44  
45      public static JSONArray arrayToJson(String[][] array, int max) {
46          if (max <= 0) {
47              max = array.length;
48          }
49  
50          JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
51  
52          for (int i = 0; (i < array.length) && (i < max); i++) {
53              String text = array[i][0];
54              String value = array[i][1];
55  
56              JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
57  
58              jsonObj.put("text", text);
59              jsonObj.put("value", value);
60  
61              jsonArray.put(jsonObj);
62          }
63  
64          return jsonArray;
65      }
66  
67      public static String arrayToXml(String[] array, int max) {
68          return arrayToXml(_singleToPairArray(array), max);
69      }
70  
71      public static String arrayToXml(String[][] array, int max) {
72          if (max <= 0) {
73              max = array.length;
74          }
75  
76          StringBuilder sb = new StringBuilder();
77  
78          sb.append("<?xml version=\"1.0\"?>");
79  
80          sb.append("<ajaxresponse>");
81  
82          for (int i = 0; (i < array.length) && (i < max); i++) {
83              String text = array[i][0];
84              String value = array[i][1];
85  
86              sb.append("<item>");
87              sb.append("<text><![CDATA[");
88              sb.append(text);
89              sb.append("]]></text>");
90              sb.append("<value><![CDATA[");
91              sb.append(value);
92              sb.append("]]></value>");
93              sb.append("</item>");
94          }
95  
96          sb.append("</ajaxresponse>");
97  
98          return sb.toString();
99      }
100 
101     public static String[][] listToArray(
102         List<?> list, String textParam, String valueParam) {
103 
104         String[][] array = new String[list.size()][2];
105 
106         for (int i = 0; i < list.size(); i++) {
107             Object bean = list.get(i);
108 
109             Object text = BeanPropertiesUtil.getObject(bean, textParam);
110 
111             if (text == null) {
112                 text = StringPool.BLANK;
113             }
114 
115             Object value = BeanPropertiesUtil.getObject(bean, valueParam);
116 
117             if (value == null) {
118                 value = StringPool.BLANK;
119             }
120 
121             array[i][0] = text.toString();
122             array[i][1] = value.toString();
123         }
124 
125         return array;
126     }
127 
128     public static JSONArray listToJson(
129         List<?> list, String textParam, String valueParam) {
130 
131         return arrayToJson(listToArray(list, textParam, valueParam), -1);
132     }
133 
134     public static String listToXml(
135         List<?> list, String textParam, String valueParam) {
136 
137         return arrayToXml(listToArray(list, textParam, valueParam), -1);
138     }
139 
140     private static String[][] _singleToPairArray(String[] array) {
141         String[][] pairArray = new String[array.length][2];
142 
143         for (int i = 0; i < array.length; i++) {
144             pairArray[i][0] = HtmlUtil.escape(array[i]);
145             pairArray[i][1] = array[i];
146         }
147 
148         return pairArray;
149     }
150 
151 }