From: <die...@us...> - 2010-03-08 13:49:20
|
Revision: 2105 http://openutils.svn.sourceforge.net/openutils/?rev=2105&view=rev Author: diego_schivo Date: 2010-03-08 13:49:14 +0000 (Mon, 08 Mar 2010) Log Message: ----------- MGNLUTILS-6 EL functions mapTokens, multiMapTokens Modified Paths: -------------- trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlUtilsElFunctions.java trunk/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld Modified: trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlUtilsElFunctions.java =================================================================== --- trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlUtilsElFunctions.java 2010-03-08 13:22:12 UTC (rev 2104) +++ trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/el/MgnlUtilsElFunctions.java 2010-03-08 13:49:14 UTC (rev 2105) @@ -36,6 +36,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import javax.jcr.RepositoryException; import javax.servlet.http.HttpServletRequest; @@ -450,6 +451,67 @@ } /** + * Extracts a map from a bi-dimensional array of strings, getting keys and values from the specified columns + * @param tokens + * @param keyIndex index of the column storing keys + * @param valueIndex index of the column storing values + * @return + */ + public static Map<String, String> mapTokens(String[][] tokens, int keyIndex, int valueIndex) + { + Map<String, String> map = new HashMap<String, String>(tokens.length); + for (String[] row : tokens) + { + if (keyIndex < row.length && valueIndex < row.length) + { + map.put(row[keyIndex], row[valueIndex]); + } + } + return map; + } + + /** + * Same as mapTokens, but the resulting map is multi-value, i.e. the same key can have more than one value. + * Rows having no key specified are considered entries of the last used key. + * @param tokens + * @param keyIndex + * @param valueIndex + * @return + */ + public static Map<String, String[]> multiMapTokens(String[][] tokens, int keyIndex, int valueIndex) + { + Map<String, List<String>> tmpMap = new HashMap<String, List<String>>(); + String currentKey = null; + for (String[] row : tokens) + { + if (keyIndex < row.length && valueIndex < row.length) + { + String key = StringUtils.trimToNull(row[keyIndex]); + if (key == null) + { + key = currentKey; + } + if (key != null) + { + List<String> values = tmpMap.get(key); + if (values == null) + { + values = new ArrayList<String>(); + tmpMap.put(key, values); + } + values.add(row[valueIndex]); + } + } + } + Map<String, String[]> map = new HashMap<String, String[]>(tmpMap.size()); + for (Entry<String, List<String>> entry : tmpMap.entrySet()) + { + map.put(entry.getKey(), entry.getValue().toArray(new String[0])); + } + return map; + } + + /** * Returns the base URL for the request (scheme + server + port + context path, like http://server:81/context/) * @return the base URL for the request (scheme + server + port + context path, like http://server:81/context/ */ Modified: trunk/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld =================================================================== --- trunk/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld 2010-03-08 13:22:12 UTC (rev 2104) +++ trunk/openutils-mgnlutils/src/main/resources/META-INF/tld/mgnlutils.tld 2010-03-08 13:49:14 UTC (rev 2105) @@ -180,6 +180,21 @@ <function-signature>java.lang.String[][] splitAndTokenize(java.lang.String)</function-signature> </function> <function> + <name>mapTokens</name> + <description>Extracts a map from a bi-dimensional array of strings, getting keys and values from the specified columns</description> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>java.util.Map mapTokens(java.lang.String[][], int, int)</function-signature> + </function> + <function> + <name>multiMapTokens</name> + <description> + Same as mapTokens, but the resulting map is multi-value, i.e. the same key can have more than one value. + Rows having no key specified are considered entries of the last used key. + </description> + <function-class>it.openutils.mgnlutils.el.MgnlUtilsElFunctions</function-class> + <function-signature>java.util.Map multiMapTokens(java.lang.String[][], int, int)</function-signature> + </function> + <function> <name>baseUrl</name> <description> Returns the base URL for the request (scheme + server + port + context path, like http://server:81/context/) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |