View Javadoc

1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.util;
23  
24  import com.opensymphony.xwork2.util.ValueStack;
25  import com.opensymphony.xwork2.util.logging.Logger;
26  import com.opensymphony.xwork2.util.logging.LoggerFactory;
27  import com.opensymphony.xwork2.TextProvider;
28  
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.LinkedList;
32  
33  /***
34   * Helper methods to access text from TextProviders
35   */
36  public class TextProviderHelper {
37  
38      private static final Logger LOG = LoggerFactory.getLogger(TextProviderHelper.class);
39  
40      /***
41       * <p>Get a message from the first TextProvider encountered in the stack.
42       * If the first TextProvider doesn't provide the message the default message is returned.</p>
43       * <p>The search for a TextProvider is iterative from the root of the stack.</p>
44       * <p>This method was refactored from  {@link org.apache.struts2.components.Text} to use a
45       * consistent implementation across UIBean components.</p>
46       * @param key             the message key in the resource bundle
47       * @param defaultMessage  the message to return if not found (evaluated for OGNL)
48       * @param args            an array args to be used in a {@link java.text.MessageFormat} message
49       * @param stack           the value stack to use for finding the text
50         *
51       * @return the message if found, otherwise the defaultMessage
52       */
53      public static String getText(String key, String defaultMessage, List<String> args, ValueStack stack) {
54          String msg = null;
55          TextProvider tp = null;
56  
57          for (Iterator iterator = stack.getRoot().iterator(); iterator.hasNext();) {
58              Object o = iterator.next();
59  
60              if (o instanceof TextProvider) {
61                  tp = (TextProvider) o;
62                  msg = tp.getText(key, null, args, stack);
63  
64                  break;
65              }
66          }
67  
68          if (msg == null) {
69              // evaluate the defaultMesage as an OGNL expression
70              msg = stack.findString(defaultMessage);
71              if (msg == null) {
72                  // use the defaultMessage literal value
73                  msg = defaultMessage;
74              }
75  
76              if (LOG.isWarnEnabled()) {
77                  if (tp != null) {
78                      LOG.warn("The first TextProvider in the ValueStack ("+tp.getClass().getName()+") could not locate the message resource with key '"+key+"'");
79                  } else {
80                      LOG.warn("Could not locate the message resource '"+key+"' as there is no TextProvider in the ValueStack.");
81                  }
82                  if (msg.equals(defaultMessage)) {
83                      LOG.warn("The default value expression '"+defaultMessage+"' was evaluated and did not match a property.  The literal value '"+defaultMessage+"' will be used.");
84                  } else {
85                      LOG.warn("The default value expression '"+defaultMessage+"' evaluated to '"+msg+"'");
86                  }
87              }
88          }
89          return msg;
90      }
91  
92      /***
93       * <p>Get a message from the first TextProvider encountered in the stack.
94       * If the first TextProvider doesn't provide the message the default message is returned.</p>
95       * <p>The search for a TextProvider is iterative from the root of the stack.</p>
96       * <p>This method was refactored from  {@link org.apache.struts2.components.Text} to use a
97       * consistent implementation across UIBean components.</p>
98       * @param key             the message key in the resource bundle
99       * @param defaultMessage  the message to return if not found
100      * @param stack           the value stack to use for finding the text
101        *
102      * @return the message if found, otherwise the defaultMessage
103      */
104     public static String getText(String key, String defaultMessage,ValueStack stack) {
105         return getText(key, defaultMessage, new LinkedList<String>(), stack);
106     }
107 }