View Javadoc

1   /*
2    * Copyright 2011-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.recode;
17  
18  import org.apache.maven.plugin.AbstractMojo;
19  import org.apache.maven.plugin.MojoExecutionException;
20  import org.apache.maven.plugin.MojoFailureException;
21  
22  /**
23   * Recodes a collection of files.
24   *
25   * @goal recode
26   * @phase process-resources
27   * @description Recodes a collection of files.
28   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
29   * @version $Revision$
30   */
31  public class MavenRecodeMojo extends AbstractMojo
32  {
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    // --- members --------------------------------------------------------------
38  
39    /**
40     * A simple flag to skip recoding. If set on the command line use
41     * <code>-Drecode.skip</code>.
42     *
43     * @parameter expression="${recode.skip}" default-value="false"
44     * @since 1.0
45     */
46    private boolean skip;
47  
48    /**
49     * A simple flag to log verbosely. If set on the command line use
50     * <code>-Drecode.verbose</code>.
51     *
52     * @parameter expression="${recode.verbose}" default-value="false"
53     * @since 1.0
54     */
55    private boolean verbose;
56  
57    /**
58     * The resources to recode.
59     *
60     * @parameter
61     * @required
62     * @since 1.0
63     */
64    private Resources[] resources;
65  
66    /**
67     * The default input encoding of the source files to be recoded, if the
68     * resources do not provide a specific value.
69     *
70     * @parameter default-value="UTF-8"
71     * @since 1.0
72     */
73    private String sourceEncoding;
74  
75    /**
76     * The default input encoding of the target files to be recoded, if the
77     * resources do not provide a specific value.
78     *
79     * @parameter default-value="UTF-8"
80     * @since 1.0
81     */
82    private String targetEncoding;
83  
84    /**
85     * The flag indicates whether <code>\r\n</code> are to be translated to
86     * <code>\n</code>.
87     *
88     * @parameter default-value="false"
89     * @since 1.0
90     */
91    private boolean fromDos;
92  
93    // ****************************** Initializer *******************************
94  
95    // ****************************** Constructors ******************************
96  
97    // ****************************** Inner Classes *****************************
98  
99    // ********************************* Methods ********************************
100 
101   // --- init -----------------------------------------------------------------
102 
103   // --- get&set --------------------------------------------------------------
104 
105   // --- business -------------------------------------------------------------
106 
107   /**
108    * {@inheritDoc}
109    *
110    * @see org.apache.maven.plugin.AbstractMojo#execute()
111    */
112   public void execute() throws MojoExecutionException, MojoFailureException
113   {
114     if (!skip)
115     {
116       if (isResourcesSpecified())
117       {
118         for (final Resources resource : resources)
119         {
120           logProcessingResource(resource);
121 
122           try
123           {
124             final Recoder recoder =
125                 new Recoder(verbose ? getLog() : null, resource);
126             resource.setDefaults(sourceEncoding, targetEncoding, fromDos);
127             recoder.recode();
128           }
129           catch (final Exception e)
130           {
131             throw new MojoFailureException(this, "Problem recoding a file in " // NOPMD
132                                                  + resource.getDir()
133                                                      .getAbsolutePath(),
134                 e.getMessage());
135           }
136         }
137 
138         logProcessingCompleted();
139       }
140       else
141       {
142         getLog().info("No resources specified to recode.");
143       }
144     }
145     else
146     {
147       getLog().info("Skipping recode plugin.");
148     }
149   }
150 
151   private boolean isResourcesSpecified()
152   {
153     return resources != null && resources.length > 0;
154   }
155 
156   private void logProcessingCompleted()
157   {
158     if (verbose)
159     {
160       getLog().info("Resources recoded successfully.");
161     }
162   }
163 
164   private void logProcessingResource(final Resources resource)
165   {
166     if (verbose)
167     {
168       getLog().info("Processing resource for recode: " + resource);
169     }
170   }
171 
172   // --- object basics --------------------------------------------------------
173 
174 }