View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.text.MessageFormat;
22  import java.util.MissingResourceException;
23  import java.util.ResourceBundle;
24  
25  import org.springframework.extensions.surf.util.I18NUtil;
26  
27  /**
28   * @author Kevin Roast
29   * 
30   * Base class for returning an I18N message string for a WebScript.
31   * <p>
32   * Returns an I18N message resolved for the current locale and specified message ID.
33   * <p>
34   * Firstly the service resource for the parent WebScript will be used for the lookup,
35   * followed by the global webscripts.properties resource bundle. 
36   */
37  public class AbstractMessageHelper
38  {
39      private WebScript webscript;
40      
41      
42      /**
43       * Constructor
44       * 
45       * @param webscript     The WebScript to lookup resources against first
46       */
47      public AbstractMessageHelper(WebScript webscript)
48      {
49          if (webscript == null)
50          {
51              throw new IllegalArgumentException("WebScript must be provided to constructor.");
52          }
53          this.webscript = webscript;
54      }
55      
56      
57      /**
58       * Get an I18Ned message.
59       * 
60       * @param id        The message Id
61       * @param args      The optional list of message arguments
62       * 
63       * @return resolved message string or the original ID if unable to find
64       */
65      protected final String resolveMessage(String id, Object... args)
66      {
67          String result = null;
68          
69          // lookup msg resource in webscript specific bundle
70          ResourceBundle resources = webscript.getResources();
71          if (resources != null)
72          {
73              try
74              {
75                  result = resources.getString(id);
76              }
77              catch (MissingResourceException mre)
78              {
79                  // key not present
80              }
81          }
82          
83          // if not found, try global bundles
84          if (result == null)
85          {
86              result = I18NUtil.getMessage(id);
87          }
88          
89          if (args.length == 0)
90          {
91              // for no args, just return found msg or the id on failure
92              if (result == null)
93              {
94              	result = id;
95              }
96          }
97          else
98          {
99              // for supplied msg args, format msg or return id on failure
100             if (result != null)
101             {
102                 result = MessageFormat.format(result, args);
103             }
104             else
105             {
106                 result = id;
107             }
108         }
109         
110         return result;
111     }
112 }