001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.internal;
020:
021: import org.apache.struts.util.MessageResources;
022: import org.apache.struts.Globals;
023: import org.apache.beehive.netui.util.logging.Logger;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.ServletContext;
027: import java.util.HashMap;
028: import java.util.Locale;
029:
030: /**
031: * Message resources extension that knows how to evaluate JSP 2.0-style expressions (set in the message keys) that are
032: * prefixed with a special indicator string.
033: */
034: public class ExpressionAwareMessageResources extends MessageResources {
035: private static final Logger _log = Logger
036: .getInstance(ExpressionAwareMessageResources.class);
037:
038: private MessageResources _delegate;
039: private Object _formBean;
040: private transient HttpServletRequest _request;
041: private transient ServletContext _servletContext;
042:
043: public ExpressionAwareMessageResources(Object formBean,
044: HttpServletRequest request, ServletContext servletContext) {
045: super (defaultFactory, null, true);
046: _formBean = formBean;
047: _request = request;
048: _servletContext = servletContext;
049:
050: // There is no message resources delegate. Look in the "global" Struts location in the Servlet context.
051: _delegate = (MessageResources) servletContext
052: .getAttribute(Globals.MESSAGES_KEY);
053:
054: }
055:
056: public ExpressionAwareMessageResources(MessageResources delegate,
057: Object formBean, HttpServletRequest request,
058: ServletContext servletContext) {
059: super (delegate.getFactory(), delegate.getConfig(), delegate
060: .getReturnNull());
061: _delegate = delegate;
062: _formBean = formBean;
063: _request = request;
064: _servletContext = servletContext;
065: }
066:
067: public String getMessage(Locale locale, String key) {
068: if (key
069: .startsWith(InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX)) {
070: String messageExpr = key
071: .substring(InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX_LENGTH);
072:
073: try {
074: return InternalExpressionUtils.evaluateMessage(
075: messageExpr, _formBean, _request,
076: _servletContext);
077: } catch (Exception e) {
078: _log.error("Could not evaluate message expression "
079: + messageExpr, e);
080: }
081:
082: return null;
083: }
084:
085: return _delegate != null ? _delegate.getMessage(locale, key)
086: : null;
087: }
088:
089: public String getMessage(Locale locale, String key, Object args[]) {
090: for (int i = 0; i < args.length; i++) {
091: Object arg = args[i];
092:
093: if (arg instanceof String) {
094: String argStr = (String) arg;
095:
096: if (argStr
097: .startsWith(InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX)) {
098: String argExpr = argStr
099: .substring(InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX_LENGTH);
100:
101: try {
102: args[i] = InternalExpressionUtils
103: .evaluateMessage(argExpr, _formBean,
104: _request, _servletContext);
105: } catch (Exception e) {
106: _log.error(
107: "Could not evaluate message arg expression "
108: + argExpr, e);
109: }
110: }
111: }
112: }
113:
114: return super .getMessage(locale, key, args);
115: }
116:
117: protected void setFormBean(Object formBean) {
118: _formBean = formBean;
119: }
120:
121: public static void update(MessageResources resources,
122: Object formBean) {
123: if (resources instanceof ExpressionAwareMessageResources) {
124: ((ExpressionAwareMessageResources) resources)
125: .setFormBean(formBean);
126: }
127: }
128: }
|