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 freemarker.template.TemplateDateModel;
24  import freemarker.template.TemplateMethodModelEx;
25  import freemarker.template.TemplateModelException;
26  import freemarker.template.TemplateNumberModel;
27  import freemarker.template.TemplateScalarModel;
28  
29  /**
30   * @author Kevin Roast
31   * 
32   * Custom FreeMarker method for returning an I18N message string.
33   * <p>
34   * Returns an I18N message resolved for the current locale and specified message ID.
35   * <p>
36   * Firstly the service resource for the parent WebScript will be used for the lookup,
37   * followed by the global webscripts.properties resource bundle. 
38   * <p>
39   * Usage: message(String id)
40   */
41  public final class MessageMethod extends AbstractMessageHelper implements TemplateMethodModelEx
42  {
43      /**
44       * Constructor
45       * 
46       * @param webscript
47       */
48      public MessageMethod(WebScript webscript)
49      {
50          super(webscript);
51      }
52      
53      /**
54       * @see freemarker.template.TemplateMethodModel#exec(java.util.List)
55       */
56      public Object exec(List args) throws TemplateModelException
57      {
58          String result = null;
59          int argSize = args.size();
60          
61          if (argSize != 0)
62          {
63              String id = null;
64              Object arg0 = args.get(0);
65              if (arg0 instanceof TemplateScalarModel)
66              {
67                  id = ((TemplateScalarModel)arg0).getAsString();
68              }
69              
70              if (id != null)
71              {
72                  if (argSize == 1)
73                  {
74                      // shortcut for no additional msg params
75                      result = resolveMessage(id);
76                  }
77                  else
78                  {
79                      Object arg;
80                      Object[] params = new Object[argSize - 1];
81                      for (int i = 0; i < argSize-1; i++)
82                      {
83                          // ignore first passed-in arg which is the msg id
84                          arg = args.get(i + 1);
85                          if (arg instanceof TemplateScalarModel)
86                          {
87                              params[i] = ((TemplateScalarModel)arg).getAsString();
88                          }
89                          else if (arg instanceof TemplateNumberModel)
90                          {
91                              params[i] = ((TemplateNumberModel)arg).getAsNumber();
92                          }
93                          else if (arg instanceof TemplateDateModel)
94                          {
95                              params[i] = ((TemplateDateModel)arg).getAsDate();
96                          }
97                          else
98                          {
99                              params[i] = "";
100                         }
101                     }
102                     
103                     result = resolveMessage(id, params);
104                 }
105             }
106         }
107         
108         return (result != null ? result : "");
109     }
110 }