001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2015 ForgeRock AS. 015 */ 016package org.forgerock.openig.el; 017 018import java.util.ArrayList; 019import java.util.LinkedHashMap; 020import java.util.List; 021import java.util.Map; 022 023 024/** 025 * Utility class for evaluating expression in some collections. 026 */ 027public final class Expressions { 028 029 private Expressions() { 030 // Prevent from instantiating. 031 } 032 033 /** 034 * Evaluate a map that may contain some values that needs to be evaluated. 035 * 036 * @param map the map to evaluate 037 * @param bindings the bindings used for the evaluation 038 * @return a new Map containing the result of the evaluation. 039 * @throws ExpressionException if an error occurs while evaluating the expression 040 */ 041 public static Map<String, Object> evaluate(Map<String, Object> map, Bindings bindings) throws ExpressionException { 042 Map<String, Object> result = new LinkedHashMap<>(map.size()); 043 for (Map.Entry<String, Object> entry : map.entrySet()) { 044 result.put(entry.getKey(), evaluate(entry.getValue(), bindings)); 045 } 046 return result; 047 } 048 049 /** 050 * Evaluate a list that may contain some String that needs to be evaluated. 051 * 052 * @param list the list to evaluate 053 * @param bindings the bindings used for the evaluation 054 * @return a new list containing the results of the evaluations 055 * @throws ExpressionException if an error occurs while evaluating the expression 056 */ 057 public static List<Object> evaluate(List<Object> list, Bindings bindings) throws ExpressionException { 058 List<Object> evaluatedList = new ArrayList<>(); 059 for (Object object : list) { 060 evaluatedList.add(evaluate(object, bindings)); 061 } 062 return evaluatedList; 063 } 064 065 /** 066 * Evaluate a String. 067 * 068 * @param value the String to evaluate 069 * @param bindings the bindings used for the evaluation 070 * @return the result of the evaluation. 071 * @throws ExpressionException if an error occurs while evaluating the expression 072 */ 073 public static Object evaluate(String value, Bindings bindings) throws ExpressionException { 074 return Expression.valueOf(value, Object.class).eval(bindings); 075 } 076 077 /** 078 * Evaluate an Object that may contain some String that needs to be evaluated. The supported types of Object are : 079 * String, List and Map. 080 * 081 * @param value the String to evaluate 082 * @param bindings the bindings used for the evaluation 083 * @return the result of the evaluation. 084 * @throws ExpressionException if an error occurs while evaluating the expression 085 */ 086 @SuppressWarnings("unchecked") 087 public static Object evaluate(Object value, Bindings bindings) throws ExpressionException { 088 if (value instanceof String) { 089 return evaluate((String) value, bindings); 090 } else if (value instanceof List) { 091 return evaluate((List) value, bindings); 092 } else if (value instanceof Map) { 093 return evaluate((Map) value, bindings); 094 } else { 095 return value; 096 } 097 } 098}