View Javadoc
1   package org.codehaus.mojo.jaxb2.shared.environment.locale;
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 org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.logging.Log;
24  import org.codehaus.mojo.jaxb2.shared.Validate;
25  import org.codehaus.mojo.jaxb2.shared.environment.AbstractLogAwareFacet;
26  
27  import java.util.Locale;
28  import java.util.StringTokenizer;
29  
30  /**
31   * EnvironmentFacet implementation which alters the default Locale for the
32   * remainder of the tool execution.
33   *
34   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
35   */
36  public class LocaleFacet extends AbstractLogAwareFacet {
37  
38      // Internal state
39      private Locale originalLocale;
40      private Locale newLocale;
41  
42      /**
43       * Compound constructor creating a LocaleFacet wrapping the supplied instances.
44       *
45       * @param log       The active Maven Log.
46       * @param newLocale The non-null Locale to be set by this LocaleFacet during execution.
47       */
48      public LocaleFacet(final Log log, final Locale newLocale) {
49          super(log);
50  
51          // Check sanity
52          Validate.notNull(newLocale, "usedLocale");
53  
54          // Assign internal state
55          this.originalLocale = Locale.getDefault();
56          this.newLocale = newLocale;
57      }
58  
59      /**
60       * {@inheritDoc}
61       * <p>Changes the Locale during the execution of the plugin.</p>
62       */
63      @Override
64      public void setup() {
65  
66          if (log.isInfoEnabled()) {
67              log.info("Setting default locale to [" + newLocale + "]");
68          }
69  
70          try {
71              Locale.setDefault(newLocale);
72          } catch (Exception e) {
73              log.error("Could not switch locale to ["
74                      + newLocale + "]. Continuing with standard locale.", e);
75          }
76      }
77  
78      /**
79       * {@inheritDoc}
80       * <p>Restores the original locale following the plugin's execution.</p>
81       */
82      @Override
83      public void restore() {
84  
85          if (log.isInfoEnabled()) {
86              log.info("Restoring default locale to [" + originalLocale + "]");
87          }
88  
89          try {
90              Locale.setDefault(originalLocale);
91          } catch (Exception e) {
92              log.error("Could not restore locale to [" + originalLocale + "]. Continuing with ["
93                      + Locale.getDefault() + "]", e);
94          }
95      }
96  
97      /**
98       * Helper method used to parse a locale configuration string into a Locale instance.
99       *
100      * @param localeString A configuration string parameter on the form
101      *                     {@code &lt;language&gt;[,&lt;country&gt;[,&lt;variant&gt;]]}
102      * @param log          The active Maven Log. Cannot be null.
103      * @return A fully constructed Locale.
104      * @throws MojoExecutionException if the localeString was not supplied on the required form.
105      */
106     public static LocaleFacet createFor(final String localeString, final Log log) throws MojoExecutionException {
107 
108         // Check sanity
109         Validate.notNull(log, "log");
110         Validate.notEmpty(localeString, "localeString");
111 
112         final StringTokenizer tok = new StringTokenizer(localeString, ",", false);
113         final int numTokens = tok.countTokens();
114         if (numTokens > 3 || numTokens == 0) {
115             throw new MojoExecutionException("A localeString must consist of up to 3 comma-separated parts on the "
116                     + "form <language>[,<country>[,<variant>]]. Received incorrect value '" + localeString + "'");
117         }
118 
119         Locale locale = null;
120         switch (numTokens) {
121             case 3:
122                 locale = new Locale(tok.nextToken().trim(), tok.nextToken().trim(), tok.nextToken().trim());
123                 break;
124 
125             case 2:
126                 locale = new Locale(tok.nextToken().trim(), tok.nextToken().trim());
127                 break;
128 
129             default:
130                 locale = new Locale(tok.nextToken().trim());
131                 break;
132         }
133 
134         // All done.
135         return new LocaleFacet(log, locale);
136     }
137 }