KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > Native2Ascii


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional;
20
21 import java.io.File JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.DirectoryScanner;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.taskdefs.MatchingTask;
26 import org.apache.tools.ant.taskdefs.optional.native2ascii.Native2AsciiAdapter;
27 import org.apache.tools.ant.taskdefs.optional.native2ascii.Native2AsciiAdapterFactory;
28 import org.apache.tools.ant.types.Mapper;
29 import org.apache.tools.ant.util.FileNameMapper;
30 import org.apache.tools.ant.util.IdentityMapper;
31 import org.apache.tools.ant.util.SourceFileScanner;
32 import org.apache.tools.ant.util.facade.FacadeTaskHelper;
33 import org.apache.tools.ant.util.facade.ImplementationSpecificArgument;
34
35 /**
36  * Converts files from native encodings to ASCII.
37  *
38  * @since Ant 1.2
39  */

40 public class Native2Ascii extends MatchingTask {
41
42     private boolean reverse = false; // convert from ascii back to native
43
private String JavaDoc encoding = null; // encoding to convert to/from
44
private File JavaDoc srcDir = null; // Where to find input files
45
private File JavaDoc destDir = null; // Where to put output files
46
private String JavaDoc extension = null; // Extension of output files if different
47

48     private Mapper mapper;
49     private FacadeTaskHelper facade = null;
50
51     /** No args constructor */
52     public Native2Ascii() {
53         facade = new FacadeTaskHelper(Native2AsciiAdapterFactory.getDefault());
54     }
55
56     /**
57      * Flag the conversion to run in the reverse sense,
58      * that is Ascii to Native encoding.
59      *
60      * @param reverse True if the conversion is to be reversed,
61      * otherwise false;
62      */

63     public void setReverse(boolean reverse) {
64         this.reverse = reverse;
65     }
66
67     /**
68      * The value of the reverse attribute.
69      * @return the reverse attribute.
70      * @since Ant 1.6.3
71      */

72     public boolean getReverse() {
73         return reverse;
74     }
75
76     /**
77      * Set the encoding to translate to/from.
78      * If unset, the default encoding for the JVM is used.
79      *
80      * @param encoding String containing the name of the Native
81      * encoding to convert from or to.
82      */

83     public void setEncoding(String JavaDoc encoding) {
84         this.encoding = encoding;
85     }
86
87     /**
88      * The value of the encoding attribute.
89      * @return the encoding attribute.
90      * @since Ant 1.6.3
91      */

92     public String JavaDoc getEncoding() {
93         return encoding;
94     }
95
96     /**
97      * Set the source directory in which to find files to convert.
98      *
99      * @param srcDir directory to find input file in.
100      */

101     public void setSrc(File JavaDoc srcDir) {
102         this.srcDir = srcDir;
103     }
104
105
106     /**
107      * Set the destination directory to place converted files into.
108      *
109      * @param destDir directory to place output file into.
110      */

111     public void setDest(File JavaDoc destDir) {
112         this.destDir = destDir;
113     }
114
115     /**
116      * Set the extension which converted files should have.
117      * If unset, files will not be renamed.
118      *
119      * @param ext File extension to use for converted files.
120      */

121     public void setExt(String JavaDoc ext) {
122         this.extension = ext;
123     }
124
125     /**
126      * Choose the implementation for this particular task.
127      * @param impl the name of the implemenation
128      * @since Ant 1.6.3
129      */

130     public void setImplementation(String JavaDoc impl) {
131         if ("default".equals(impl)) {
132             facade.setImplementation(Native2AsciiAdapterFactory.getDefault());
133         } else {
134             facade.setImplementation(impl);
135         }
136     }
137
138     /**
139      * Defines the FileNameMapper to use (nested mapper element).
140      *
141      * @return the mapper to use for file name translations.
142      *
143      * @throws BuildException if more than one mapper is defined.
144      */

145     public Mapper createMapper() throws BuildException {
146         if (mapper != null) {
147             throw new BuildException("Cannot define more than one mapper",
148                                      getLocation());
149         }
150         mapper = new Mapper(getProject());
151         return mapper;
152     }
153
154     /**
155      * A nested filenamemapper
156      * @param fileNameMapper the mapper to add
157      * @since Ant 1.6.3
158      */

159     public void add(FileNameMapper fileNameMapper) {
160         createMapper().add(fileNameMapper);
161     }
162
163     /**
164      * Adds an implementation specific command-line argument.
165      * @return a ImplementationSpecificArgument to be configured
166      *
167      * @since Ant 1.6.3
168      */

169     public ImplementationSpecificArgument createArg() {
170         ImplementationSpecificArgument arg =
171             new ImplementationSpecificArgument();
172         facade.addImplementationArgument(arg);
173         return arg;
174     }
175
176     /**
177      * Execute the task
178      *
179      * @throws BuildException is there is a problem in the task execution.
180      */

181     public void execute() throws BuildException {
182
183         DirectoryScanner scanner = null; // Scanner to find our inputs
184
String JavaDoc[] files; // list of files to process
185

186         // default srcDir to basedir
187
if (srcDir == null) {
188             srcDir = getProject().resolveFile(".");
189         }
190
191         // Require destDir
192
if (destDir == null) {
193             throw new BuildException("The dest attribute must be set.");
194         }
195
196         // if src and dest dirs are the same, require the extension
197
// to be set, so we don't stomp every file. One could still
198
// include a file with the same extension, but ....
199
if (srcDir.equals(destDir) && extension == null && mapper == null) {
200             throw new BuildException("The ext attribute or a mapper must be set if"
201                                      + " src and dest dirs are the same.");
202         }
203
204         FileNameMapper m = null;
205         if (mapper == null) {
206             if (extension == null) {
207                 m = new IdentityMapper();
208             } else {
209                 m = new ExtMapper();
210             }
211         } else {
212             m = mapper.getImplementation();
213         }
214
215         scanner = getDirectoryScanner(srcDir);
216         files = scanner.getIncludedFiles();
217         SourceFileScanner sfs = new SourceFileScanner(this);
218         files = sfs.restrict(files, srcDir, destDir, m);
219         int count = files.length;
220         if (count == 0) {
221             return;
222         }
223         String JavaDoc message = "Converting " + count + " file"
224             + (count != 1 ? "s" : "") + " from ";
225         log(message + srcDir + " to " + destDir);
226         for (int i = 0; i < files.length; i++) {
227             convert(files[i], m.mapFileName(files[i])[0]);
228         }
229     }
230
231     /**
232      * Convert a single file.
233      *
234      * @param srcName name of the input file.
235      * @param destName name of the input file.
236      */

237     private void convert(String JavaDoc srcName, String JavaDoc destName)
238         throws BuildException {
239         File JavaDoc srcFile; // File to convert
240
File JavaDoc destFile; // where to put the results
241

242         // Build the full file names
243
srcFile = new File JavaDoc(srcDir, srcName);
244         destFile = new File JavaDoc(destDir, destName);
245
246         // Make sure we're not about to clobber something
247
if (srcFile.equals(destFile)) {
248             throw new BuildException("file " + srcFile
249                                      + " would overwrite its self");
250         }
251
252         // Make intermediate directories if needed
253
// XXX JDK 1.1 doesn't have File.getParentFile,
254
String JavaDoc parentName = destFile.getParent();
255         if (parentName != null) {
256             File JavaDoc parentFile = new File JavaDoc(parentName);
257
258             if ((!parentFile.exists()) && (!parentFile.mkdirs())) {
259                 throw new BuildException("cannot create parent directory "
260                                          + parentName);
261             }
262         }
263
264         log("converting " + srcName, Project.MSG_VERBOSE);
265         Native2AsciiAdapter ad =
266             Native2AsciiAdapterFactory.getAdapter(facade.getImplementation(),
267                                                   this);
268         if (!ad.convert(this, srcFile, destFile)) {
269             throw new BuildException("conversion failed");
270         }
271     }
272
273     /**
274      * Returns the (implementation specific) settings given as nested
275      * arg elements.
276      * @return the arguments.
277      * @since Ant 1.6.3
278      */

279     public String JavaDoc[] getCurrentArgs() {
280         return facade.getArgs();
281     }
282
283     private class ExtMapper implements FileNameMapper {
284
285         public void setFrom(String JavaDoc s) {
286         }
287         public void setTo(String JavaDoc s) {
288         }
289
290         public String JavaDoc[] mapFileName(String JavaDoc fileName) {
291             int lastDot = fileName.lastIndexOf('.');
292             if (lastDot >= 0) {
293                 return new String JavaDoc[] {fileName.substring(0, lastDot)
294                                          + extension};
295             } else {
296                 return new String JavaDoc[] {fileName + extension};
297             }
298         }
299     }
300 }
301
Popular Tags