1 18 package org.apache.beehive.netui.pageflow.internal; 19 20 import org.apache.struts.util.MessageResources; 21 import org.apache.beehive.netui.util.logging.Logger; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.ServletContext ; 25 import java.util.HashMap ; 26 import java.util.Locale ; 27 28 32 public class ExpressionAwareMessageResources 33 extends MessageResources 34 { 35 private static final Logger _log = Logger.getInstance( ExpressionAwareMessageResources.class ); 36 37 private MessageResources _delegate; 38 private Object _formBean; 39 private transient HttpServletRequest _request; 40 private transient ServletContext _servletContext; 41 42 public ExpressionAwareMessageResources( Object formBean, HttpServletRequest request, ServletContext servletContext ) 43 { 44 super( defaultFactory, null, true ); 45 _formBean = formBean; 46 _request = request; 47 _servletContext = servletContext; 48 } 49 50 public ExpressionAwareMessageResources( MessageResources delegate, Object formBean, HttpServletRequest request, 51 ServletContext servletContext ) 52 { 53 super( delegate.getFactory(), delegate.getConfig(), delegate.getReturnNull() ); 54 _delegate = delegate; 55 _formBean = formBean; 56 _request = request; 57 _servletContext = servletContext; 58 } 59 60 public String getMessage( Locale locale, String key ) 61 { 62 if ( key.startsWith( InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX ) ) 63 { 64 String messageExpr = key.substring( InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX_LENGTH ); 65 66 try 67 { 68 return InternalExpressionUtils.evaluateMessage( messageExpr, _formBean, _request, _servletContext ); 69 } 70 catch ( Exception e ) 71 { 72 _log.error( "Could not evaluate message expression " + messageExpr, e ); 73 } 74 75 return null; 76 } 77 78 return _delegate != null ? _delegate.getMessage( locale, key ) : null; 79 } 80 81 public String getMessage( Locale locale, String key, Object args[] ) 82 { 83 for ( int i = 0; i < args.length; i++ ) 84 { 85 Object arg = args[i]; 86 87 if ( arg instanceof String ) 88 { 89 String argStr = ( String ) arg; 90 91 if ( argStr.startsWith( InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX ) ) 92 { 93 String argExpr = argStr.substring( InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX_LENGTH ); 94 95 try 96 { 97 args[i] = 98 InternalExpressionUtils.evaluateMessage( argExpr, _formBean, _request, _servletContext ); 99 } 100 catch ( Exception e ) 101 { 102 _log.error( "Could not evaluate message arg expression " + argExpr, e ); 103 } 104 } 105 } 106 } 107 108 return super.getMessage( locale, key, args ); 109 } 110 111 protected void setFormBean( Object formBean ) 112 { 113 _formBean = formBean; 114 } 115 116 public static void update( MessageResources resources, Object formBean ) 117 { 118 if ( resources instanceof ExpressionAwareMessageResources ) 119 { 120 ( ( ExpressionAwareMessageResources ) resources ).setFormBean( formBean ); 121 } 122 } 123 } 124 125 126 | Popular Tags |