001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018    import com.liferay.portal.kernel.json.JSONArray;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.util.HtmlUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    
025    import java.util.List;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class Autocomplete {
031    
032            public static JSONArray arrayToJson(String[] array, int max) {
033                    return arrayToJson(_singleToPairArray(array), max);
034            }
035    
036            public static JSONArray arrayToJson(String[][] array, int max) {
037                    if (max <= 0) {
038                            max = array.length;
039                    }
040    
041                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
042    
043                    for (int i = 0; (i < array.length) && (i < max); i++) {
044                            String text = array[i][0];
045                            String value = array[i][1];
046    
047                            JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
048    
049                            jsonObj.put("text", text);
050                            jsonObj.put("value", value);
051    
052                            jsonArray.put(jsonObj);
053                    }
054    
055                    return jsonArray;
056            }
057    
058            public static String arrayToXml(String[] array, int max) {
059                    return arrayToXml(_singleToPairArray(array), max);
060            }
061    
062            public static String arrayToXml(String[][] array, int max) {
063                    if (max <= 0) {
064                            max = array.length;
065                    }
066    
067                    StringBundler sb = new StringBundler(array.length * 8 + 3);
068    
069                    sb.append("<?xml version=\"1.0\"?>");
070    
071                    sb.append("<ajaxresponse>");
072    
073                    for (int i = 0; (i < array.length) && (i < max); i++) {
074                            String text = array[i][0];
075                            String value = array[i][1];
076    
077                            sb.append("<item>");
078                            sb.append("<text><![CDATA[");
079                            sb.append(text);
080                            sb.append("]]></text>");
081                            sb.append("<value><![CDATA[");
082                            sb.append(value);
083                            sb.append("]]></value>");
084                            sb.append("</item>");
085                    }
086    
087                    sb.append("</ajaxresponse>");
088    
089                    return sb.toString();
090            }
091    
092            public static String[][] listToArray(
093                    List<?> list, String textParam, String valueParam) {
094    
095                    String[][] array = new String[list.size()][2];
096    
097                    for (int i = 0; i < list.size(); i++) {
098                            Object bean = list.get(i);
099    
100                            Object text = BeanPropertiesUtil.getObject(bean, textParam);
101    
102                            if (text == null) {
103                                    text = StringPool.BLANK;
104                            }
105    
106                            Object value = BeanPropertiesUtil.getObject(bean, valueParam);
107    
108                            if (value == null) {
109                                    value = StringPool.BLANK;
110                            }
111    
112                            array[i][0] = text.toString();
113                            array[i][1] = value.toString();
114                    }
115    
116                    return array;
117            }
118    
119            public static JSONArray listToJson(
120                    List<?> list, String textParam, String valueParam) {
121    
122                    return arrayToJson(listToArray(list, textParam, valueParam), -1);
123            }
124    
125            public static String listToXml(
126                    List<?> list, String textParam, String valueParam) {
127    
128                    return arrayToXml(listToArray(list, textParam, valueParam), -1);
129            }
130    
131            private static String[][] _singleToPairArray(String[] array) {
132                    String[][] pairArray = new String[array.length][2];
133    
134                    for (int i = 0; i < array.length; i++) {
135                            pairArray[i][0] = HtmlUtil.escape(array[i]);
136                            pairArray[i][1] = array[i];
137                    }
138    
139                    return pairArray;
140            }
141    
142    }