1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2007 The Codehaus
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21 package org.codehaus.mojo.native2ascii;
22
23 import java.io.*;
24 import java.util.Arrays;
25 import java.util.Iterator;
26 import org.apache.maven.plugin.AbstractMojo;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.MojoFailureException;
29 import org.codehaus.plexus.util.FileUtils;
30 import org.codehaus.plexus.util.StringUtils;
31
32 /**
33 * Converts files with characters in any supported character encoding to one with ASCII and/or Unicode escapes.
34 *
35 * @goal native2ascii
36 * @phase process-classes
37 */
38 public class Native2AsciiMojo
39 extends AbstractMojo
40 {
41 /**
42 * The directory to find files in.
43 *
44 * @parameter default-value="${project.build.outputDirectory}"
45 * @since 1.0-alpha-2
46 */
47 protected File workDir;
48
49 /**
50 * Directory for temporary files.
51 *
52 * @parameter default-value="${project.build.directory}
53 * @since 1.0-alpha-2
54 */
55 protected File tempDir;
56
57 /**
58 * The native encoding the files are in.
59 *
60 * @parameter default-value="${project.build.sourceEncoding}"
61 * @since 1.0-alpha-1
62 */
63 protected String encoding;
64
65 /**
66 * Patterns of files that must be included. Default is "**\/*.properties".
67 *
68 * @parameter
69 * @since 1.0-alpha-2
70 */
71 protected String[] includes;
72
73 /**
74 * Patterns of files that must be excluded.
75 *
76 * @parameter
77 * @since 1.0-alpha-2
78 */
79 protected String[] excludes;
80
81 public void execute()
82 throws MojoExecutionException, MojoFailureException
83 {
84 if ( !workDir.exists() )
85 {
86 return;
87 }
88
89 if ( StringUtils.isEmpty( encoding ) )
90 {
91 encoding = System.getProperty( "file.encoding" );
92 getLog().warn( "Using platform encoding (" + encoding + " actually) to convert resources!" );
93 }
94
95 if ( includes == null || includes.length == 0 )
96 {
97 includes = new String[] { "**/*.properties" };
98 }
99 if ( excludes == null )
100 {
101 excludes = new String[0];
102 }
103
104 Iterator files;
105 try
106 {
107 getLog().info( "Includes: " + Arrays.asList( includes ) );
108 getLog().info( "Excludes: " + Arrays.asList( excludes ) );
109 String incl = StringUtils.join( includes, "," );
110 String excl = StringUtils.join( excludes, "," );
111 files = FileUtils.getFiles( workDir, incl, excl ).iterator();
112 }
113 catch ( IOException e )
114 {
115 throw new MojoExecutionException( "Unable to get list of files" );
116 }
117
118 while ( files.hasNext() )
119 {
120 File file = (File) files.next();
121 getLog().info( "Processing " + file.getAbsolutePath() );
122 try
123 {
124 // Convert file in-place
125 File tempFile = File.createTempFile( file.getName(), "native2ascii", tempDir );
126 Native2Ascii.nativeToAscii( file, tempFile, encoding );
127 FileUtils.rename( tempFile, file );
128 }
129 catch ( IOException e )
130 {
131 throw new MojoExecutionException( "Unable to convert " + file.getAbsolutePath(), e );
132 }
133 }
134 }
135 }