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.acacia.shared; 029 030import java.util.Arrays; 031import java.util.HashMap; 032import java.util.List; 033import java.util.Map; 034import java.util.Map.Entry; 035 036/** Helper with methods commonly used in editor widgets. */ 037public class CmsWidgetUtil { 038 039 /** 040 * Generates a pipe separated option string from the map, 041 * e.g., for {key1 -> value1, key2 -> null, key3 -> value3} the string "key1=value1|key2|key3=value3" will be generated. 042 * @param options the map with the config options. 043 * @return the options as pipe separated string. 044 */ 045 public static String generatePipeSeparatedConfigString(Map<String, String> options) { 046 047 StringBuffer result = new StringBuffer(); 048 for (Entry<String, String> option : options.entrySet()) { 049 result.append('|'); 050 result.append(option.getKey()); 051 if (option.getValue() != null) { 052 result.append('=').append(option.getValue()); 053 } 054 } 055 return result.length() > 0 ? result.substring(1) : ""; 056 } 057 058 /** 059 * Returns a flag, indicating if a boolean option is set, i.e., it is in the map and has value null or (case-insensitive) "true". 060 * @param configOptions the map with the config options. 061 * @param optionKey the boolean option to check. 062 * @return a flag, indicating if a boolean option is set 063 */ 064 public static boolean getBooleanOption(Map<String, String> configOptions, String optionKey) { 065 066 if (configOptions.containsKey(optionKey)) { 067 String value = configOptions.get(optionKey); 068 if ((value == null) || Boolean.valueOf(value).booleanValue()) { 069 return true; 070 } 071 } 072 return false; 073 } 074 075 /** 076 * Returns the value of an option, or the default if the value is null or the key is not part of the map. 077 * @param configOptions the map with the config options. 078 * @param optionKey the option to get the value of 079 * @param defaultValue the default value to return if the option is not set. 080 * @return the value of an option, or the default if the value is null or the key is not part of the map. 081 */ 082 public static String getStringOption(Map<String, String> configOptions, String optionKey, String defaultValue) { 083 084 String result = configOptions.get(optionKey); 085 return null != result ? result : defaultValue; 086 } 087 088 /** 089 * Parses a pipe-separated config string into a map, <i>converting all keys to lowercase</i>. 090 * E.g., for "Key1=Value1|KEY2|key3=value3" the map {key1 -> Value1, key2 -> null, key3 -> value3} is returned 091 * @param configString the config string to parse 092 * @return the config options from the string as map 093 */ 094 public static Map<String, String> parsePipeSeparatedConfigString(String configString) { 095 096 Map<String, String> result = new HashMap<>(); 097 if (null != configString) { 098 List<String> options = Arrays.asList(configString.split("\\|")); 099 for (String option : options) { 100 String optKey; 101 String optValue; 102 int firstEquals = option.indexOf("="); 103 if (firstEquals >= 0) { 104 optKey = option.substring(0, firstEquals); 105 optValue = option.substring(firstEquals + 1); 106 } else { 107 optKey = option.toLowerCase(); 108 optValue = null; 109 } 110 if (optKey.length() > 0) { 111 result.put(optKey, optValue); 112 } 113 } 114 } 115 return result; 116 } 117 118}