View Javadoc

1   package org.apache.velocity.tools.generic;
2   
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  import java.util.Locale;
23  import org.apache.velocity.tools.ToolContext;
24  
25  /**
26   * Implements common logic and constants for tools which allow their
27   * default {@link Locale} to be configured.
28   *
29   * @author Nathan Bubna
30   * @since VelocityTools 2.0
31   */
32  public class LocaleConfig extends SafeConfig
33  {
34      /**
35       * The default {@link Locale} to be used when none is specified.
36       */
37      public static final Locale DEFAULT_LOCALE = Locale.getDefault();
38  
39      private Locale locale = DEFAULT_LOCALE;
40  
41      /**
42       * Does the actual configuration. This is protected, so
43       * subclasses may share the same ValueParser and call configure
44       * at any time, while preventing templates from doing so when 
45       * configure(Map) is locked.
46       */
47      protected void configure(ValueParser values)
48      {
49          Locale locale = values.getLocale(ToolContext.LOCALE_KEY);
50          if (locale != null)
51          {
52              setLocale(locale);
53          }
54      }
55  
56      /**
57       * This returns the configured default {@link Locale} for this tool.
58       *
59       * @return the default {@link Locale}
60       */
61      public Locale getLocale()
62      {
63          return this.locale;
64      }
65  
66      /**
67       * Sets the default locale for this instance.
68       */
69      protected void setLocale(Locale locale)
70      {
71          this.locale = locale;
72      }
73  
74  }