001/* 002 * (C) Copyright 2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id: ComponentTagUtils.java 28610 2008-01-09 17:13:52Z sfermigier $ 020 */ 021 022package org.nuxeo.ecm.platform.ui.web.util; 023 024import javax.el.ELContext; 025import javax.el.ELException; 026import javax.el.ExpressionFactory; 027import javax.el.ValueExpression; 028import javax.faces.application.Application; 029import javax.faces.context.FacesContext; 030import javax.faces.view.facelets.FaceletContext; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034 035/** 036 * Component tag utils. 037 * 038 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 039 */ 040public final class ComponentTagUtils { 041 042 private static final Log log = LogFactory.getLog(ComponentTagUtils.class); 043 044 // Utility class. 045 private ComponentTagUtils() { 046 } 047 048 /** 049 * Returns true if the specified value contains a value expression, e.g the start and end of EL markers. 050 * 051 * @param value the value to evaluate, returns false if null 052 */ 053 public static boolean isValueReference(String value) { 054 if (value == null) { 055 return false; 056 } 057 return value.contains("#{") && value.indexOf("#{") < value.indexOf('}') || value.contains("${") 058 && value.indexOf("${") < value.indexOf('}'); 059 } 060 061 /** 062 * Returns true if the specified value is a value expression, e.g starting and ending with EL markers after being 063 * trimmed. 064 * 065 * @param value the value to evaluate, returns false if null 066 * @since 5.6 067 */ 068 public static boolean isStrictValueReference(String value) { 069 if (value == null) { 070 return false; 071 } 072 value = value.trim(); 073 return (value.startsWith("#{") && value.indexOf("#{") < value.indexOf('}') && value.endsWith("}")) 074 || (value.startsWith("${") && value.indexOf("${") < value.indexOf('}') && value.endsWith("}")); 075 } 076 077 /** 078 * Returns a value name for given strict value reference. If reference is #{foo} or ${foo}, will return "foo". 079 * 080 * @since 5.6 081 * @throws IllegalArgumentException if reference is null or {@link #isStrictValueReference(String)} returns false. 082 * @param valueReference 083 */ 084 public static String getBareValueName(String valueReference) { 085 if (!isStrictValueReference(valueReference)) { 086 throw new IllegalArgumentException("Invalid value reference '" + valueReference + "'"); 087 } 088 return valueReference.substring(2, valueReference.length() - 1); 089 } 090 091 /** 092 * Returns true if the specified value conforms to the syntax requirements of a method binding expression. 093 * <p> 094 * The method can have parameters and the expression must use parentheses even if no parameters are needed. 095 * 096 * @param value the value to evaluate (not null) 097 * @deprecated since 5.5: method and value references are now equivalent with jboss EL 098 */ 099 @Deprecated 100 public static boolean isMethodReference(String value) { 101 boolean isValue = isValueReference(value); 102 if (isValue) { 103 if (value.contains("(") && value.indexOf('(') < value.indexOf(')') 104 // make sure it's not a function 105 && (!value.contains(":") || value.indexOf(':') > value.indexOf('('))) { 106 return true; 107 } 108 } 109 return false; 110 } 111 112 /** 113 * Resolves an expression from a given faces context. 114 * <p> 115 * Resolves the expression a second time when first resolution gives a String value using the EL Expression syntax. 116 * <p> 117 * Does not throw any error when resolution fails (only logs an error message). 118 * 119 * @see #resolveElExpression(FaceletContext, String) 120 */ 121 public static Object resolveElExpression(FacesContext context, String elExpression) { 122 if (!isValueReference(elExpression)) { 123 // literal 124 return elExpression; 125 } else { 126 if (context == null) { 127 log.error("FacesContext is null => cannot resolve el expression '" + elExpression + "'"); 128 return null; 129 } 130 // expression => evaluate 131 Application app = context.getApplication(); 132 try { 133 return app.evaluateExpressionGet(context, elExpression, Object.class); 134 } catch (ELException e) { 135 log.error("Faces context: Error processing expression '" + elExpression + "'", e); 136 return null; 137 } 138 } 139 } 140 141 /** 142 * Resolves given value expression as string and sets given value on it. 143 * 144 * @since 6.0 145 */ 146 public static void applyValueExpression(FacesContext context, String elExpression, Object value) { 147 if (!isStrictValueReference(elExpression)) { 148 log.warn("Cannot set value '" + value + "' for expression '" + elExpression + "'"); 149 } else { 150 if (context == null) { 151 log.error("FacesContext is null => cannot resolve el expression '" + elExpression + "'"); 152 return; 153 } 154 Application app = context.getApplication(); 155 ExpressionFactory eFactory = app.getExpressionFactory(); 156 ELContext elContext = context.getELContext(); 157 try { 158 ValueExpression vExpression = eFactory.createValueExpression(elContext, elExpression, Object.class); 159 vExpression.setValue(elContext, value); 160 } catch (ELException e) { 161 log.error("Error setting value '" + value + "' for expression '" + elExpression + "'", e); 162 } 163 } 164 } 165 166 /** 167 * Resolves an expression from a given facelet context, using its {@link ExpressionFactory} that can hold a wider 168 * context than the faces context behind it. 169 * <p> 170 * Resolves the expression a second time when first resolution gives a String value using the EL Expression syntax. 171 * <p> 172 * Does not throw any error when resolution fails (only logs an error message). 173 */ 174 public static Object resolveElExpression(FaceletContext faceletContext, String elExpression) { 175 if (!isValueReference(elExpression)) { 176 // literal 177 return elExpression; 178 } else { 179 if (faceletContext == null) { 180 log.error("FaceletContext is null => cannot resolve el expression '" + elExpression + "'"); 181 return null; 182 } 183 // expression => evaluate 184 ExpressionFactory eFactory = faceletContext.getExpressionFactory(); 185 ELContext elContext = faceletContext.getFacesContext().getELContext(); 186 ValueExpression expr = eFactory.createValueExpression(faceletContext, elExpression, Object.class); 187 try { 188 return expr.getValue(elContext); 189 } catch (ELException e) { 190 log.error("Facelet context: Error processing expression '" + elExpression + "'", e); 191 return null; 192 } 193 } 194 } 195 196}