001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025:
026: import java.util.List;
027:
028: import org.json.JSONArray;
029: import org.json.JSONObject;
030:
031: /**
032: * <a href="Autocomplete.java.html"><b><i>View Source</i></b></a>
033: *
034: * @author Brian Wing Shun Chan
035: *
036: */
037: public class Autocomplete {
038:
039: public static JSONArray arrayToJson(String[] array, int max) {
040: return arrayToJson(_singleToPairArray(array), max);
041: }
042:
043: public static JSONArray arrayToJson(String[][] array, int max) {
044: if (max <= 0) {
045: max = array.length;
046: }
047:
048: JSONArray jsonArray = new JSONArray();
049:
050: for (int i = 0; (i < array.length) && (i < max); i++) {
051: String text = array[i][0];
052: String value = array[i][1];
053:
054: JSONObject jsonObj = new JSONObject();
055:
056: jsonObj.put("text", text);
057: jsonObj.put("value", value);
058:
059: jsonArray.put(jsonObj);
060: }
061:
062: return jsonArray;
063: }
064:
065: public static String arrayToXml(String[] array, int max) {
066: return arrayToXml(_singleToPairArray(array), max);
067: }
068:
069: public static String arrayToXml(String[][] array, int max) {
070: if (max <= 0) {
071: max = array.length;
072: }
073:
074: StringMaker sm = new StringMaker();
075:
076: sm.append("<?xml version=\"1.0\"?>");
077:
078: sm.append("<ajaxresponse>");
079:
080: for (int i = 0; (i < array.length) && (i < max); i++) {
081: String text = array[i][0];
082: String value = array[i][1];
083:
084: sm.append("<item>");
085: sm.append("<text><![CDATA[");
086: sm.append(text);
087: sm.append("]]></text>");
088: sm.append("<value><![CDATA[");
089: sm.append(value);
090: sm.append("]]></value>");
091: sm.append("</item>");
092: }
093:
094: sm.append("</ajaxresponse>");
095:
096: return sm.toString();
097: }
098:
099: public static String[][] listToArray(List list, String textParam,
100: 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 = BeanUtil.getObject(bean, textParam);
108:
109: if (text == null) {
110: text = StringPool.BLANK;
111: }
112:
113: Object value = BeanUtil.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(List list, String textParam,
127: String valueParam) {
128:
129: return arrayToJson(listToArray(list, textParam, valueParam), -1);
130: }
131:
132: public static String listToXml(List list, String textParam,
133: 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] = Html.escape(array[i]);
143: pairArray[i][1] = array[i];
144: }
145:
146: return pairArray;
147: }
148:
149: }
|