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 java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileNotFoundException;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  import org.apache.maven.plugin.logging.Log;
27  
28  import org.apache.commons.io.IOUtils;
29  import org.codehaus.plexus.util.DirectoryScanner;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  /**
33   * Utility class to recode files in a folder.
34   *
35   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
36   * @version $Revision:591 $
37   */
38  class Recoder
39  {
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    // --- members --------------------------------------------------------------
45  
46    /**
47     * The logger to use. May be <code>null</code> if no logging is requested.
48     */
49    private final Log log;
50  
51    /**
52     * The descriptor of the resources to be recoded.
53     */
54    private final Resources resource;
55  
56    // ****************************** Initializer *******************************
57  
58    // ****************************** Constructors ******************************
59  
60    /**
61     * Default constructor.
62     *
63     * @param log the logger to use. May be <code>null</code> if no logging is
64     *          requested.
65     * @param resource the descriptor of the resources to be recoded.
66     */
67    Recoder(final Log log, final Resources resource)
68    {
69      this.log = log;
70      this.resource = resource;
71    }
72  
73    // ****************************** Inner Classes *****************************
74  
75    // ********************************* Methods ********************************
76  
77    // --- init -----------------------------------------------------------------
78  
79    // --- get&set --------------------------------------------------------------
80  
81    // --- business -------------------------------------------------------------
82  
83    /**
84     * Starts recoding the resources of this recoder.
85     *
86     * @throws IOException if any resource recoding fails.
87     */
88    void recode() throws IOException
89    {
90      final File baseDir = resource.getDir();
91      if (baseDir != null && baseDir.exists())
92      {
93        final DirectoryScanner scanner = createScanner(resource);
94        for (final String includedFilePath : scanner.getIncludedFiles())
95        {
96          final File file = new File(baseDir, includedFilePath);
97          recode(file);
98        }
99      }
100     else
101     {
102       log.info("Skipping recode for folder '" + baseDir.getAbsolutePath()
103                + "', since it does not exist.");
104     }
105   }
106 
107   /**
108    * Creates the scanner with the calculated includes and excludes.
109    *
110    * @return the scanner with the calculated includes and excludes.
111    */
112   DirectoryScanner createScanner(final Resources resource)
113   {
114     final DirectoryScanner scanner = new DirectoryScanner();
115 
116     scanner.setBasedir(resource.getDir());
117     scanner.setCaseSensitive(true);
118     scanner.setIncludes(resource.getIncludes());
119     scanner.setExcludes(resource.getExcludes());
120 
121     scanner.scan();
122 
123     return scanner;
124   }
125 
126   void recode(final File file) throws IOException
127   {
128     if (log != null)
129     {
130       log.info(" Recoding file " + resource.getSourceEncoding() + "->"
131                + resource.getTargetEncoding() + ": " + file.getAbsolutePath());
132     }
133     String fileContent = readContent(file);
134     fileContent = translate(fileContent);
135     writeContent(file, fileContent);
136     if (log != null)
137     {
138       log.info(" File recoded: " + file.getAbsolutePath());
139     }
140   }
141 
142   private String readContent(final File file) throws FileNotFoundException,
143     IOException
144   {
145     InputStream inputStream = null;
146 
147     try
148     {
149       inputStream = new FileInputStream(file);
150       final String fileContent =
151           IOUtils.toString(inputStream, resource.getSourceEncoding());
152       return fileContent;
153     }
154     finally
155     {
156       IOUtils.closeQuietly(inputStream);
157     }
158   }
159 
160   private String translate(final String fileContent)
161   {
162     if (resource.isFromDos())
163     {
164       return StringUtils.replace(fileContent, "\r\n", "\n");
165     }
166     return fileContent;
167   }
168 
169   private void writeContent(final File file, final String fileContent)
170     throws IOException
171   {
172     Writer writer = null;
173 
174     try
175     {
176       writer =
177           new OutputStreamWriter(new FileOutputStream(file),
178               resource.getTargetEncoding());
179       IOUtils.write(fileContent, writer);
180     }
181     finally
182     {
183       IOUtils.closeQuietly(writer);
184     }
185   }
186 
187   // --- object basics --------------------------------------------------------
188 
189 }