001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ugc.client; 029 030import java.util.HashMap; 031import java.util.Map; 032import java.util.Random; 033 034import com.google.gwt.core.client.JavaScriptObject; 035import com.google.gwt.json.client.JSONObject; 036import com.google.gwt.json.client.JSONString; 037 038/** 039 * Various utility functions.<p> 040 */ 041public class CmsJsUtils { 042 043 /** The random number generator. */ 044 public static final Random RANDOM = new Random(); 045 046 /** 047 * Converts a Javascript object to a map from strings to strings, ignoring all properties 048 * of the object whose value is not a string.<p> 049 * 050 * @param jso the Javascript object to convert 051 * @return the map containing the string-valued properties of the Javascript object 052 */ 053 public static Map<String, String> convertJsObjectToMap(JavaScriptObject jso) { 054 055 Map<String, String> result = new HashMap<String, String>(); 056 JSONObject json = new JSONObject(jso); 057 for (String key : json.keySet()) { 058 if ((json.get(key) != null) && (json.get(key).isString() != null)) { 059 result.put(key, json.get(key).isString().stringValue()); 060 } else { 061 result.put(key, null); 062 } 063 } 064 return result; 065 } 066 067 /** 068 * Converts a map whose keys and values are strings to a Javascript object with the keys as attributes and the 069 * corresponding values as the attribute values.<p> 070 * 071 * @param stringMap the map to convert 072 * @return the Javascript object with the attributes defined by the map 073 */ 074 public static JavaScriptObject convertMapToJsObject(Map<String, String> stringMap) { 075 076 if (stringMap == null) { 077 return null; 078 } 079 JSONObject json = new JSONObject(); 080 for (Map.Entry<String, String> entry : stringMap.entrySet()) { 081 String key = entry.getKey(); 082 String value = entry.getValue(); 083 json.put(key, new JSONString(value)); 084 } 085 return json.getJavaScriptObject(); 086 } 087 088 /** 089 * Generates a random id.<p> 090 * 091 * @return the random id 092 */ 093 public static String generateRandomId() { 094 095 StringBuffer buffer = new StringBuffer(); 096 int base = 36; 097 for (int i = 0; i < 16; i++) { 098 buffer.append(Integer.toString(RANDOM.nextInt(base), base)); 099 } 100 return buffer.toString(); 101 } 102 103}