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.util.List;
22  
23  import org.mozilla.javascript.Scriptable;
24  
25  /**
26   * Helper to resolve an I18N message for JS scripts.
27   * 
28   * @author Kevin Roast
29   */
30  public final class ScriptMessage extends AbstractMessageHelper
31  {
32      /**
33       * Constructor
34       * 
35       * @param webscript
36       */
37      public ScriptMessage(WebScript webscript)
38      {
39          super(webscript);
40      }
41      
42      
43      /**
44       * Get an I18N message
45       * 
46       * @param id    Message Id
47       * 
48       * @return resolved message
49       */
50      public String get(String id)
51      {
52          String result = null;
53          
54          if (id != null && id.length() != 0)
55          {
56              result = resolveMessage(id);
57          }
58          
59          return (result != null ? result : "");
60      }
61      
62      /**
63       * Get an I18N message with the given message args
64       * 
65       * @param id    Message Id
66       * @param args  Message args
67       * 
68       * @return resolved message
69       */
70      public String get(String id, Scriptable args)
71      {
72          String result = null;
73          
74          if (id != null && id.length() != 0)
75          {
76              Object params = ScriptValueConverter.unwrapValue(args);
77              if (params instanceof List)
78              {
79                  result = resolveMessage(id, ((List)params).toArray());
80              }
81              else
82              {
83                  result = resolveMessage(id);
84              }
85          }
86          
87          return (result != null ? result : "");
88      }
89  }