source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/Native2Ascii.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 9.6 KB
Line 
1/*
2 * Copyright 2000-2005 The Apache Software Foundation
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 */
17
18package org.apache.tools.ant.taskdefs.optional;
19
20import java.io.File;
21import org.apache.tools.ant.BuildException;
22import org.apache.tools.ant.DirectoryScanner;
23import org.apache.tools.ant.Project;
24import org.apache.tools.ant.taskdefs.MatchingTask;
25import org.apache.tools.ant.taskdefs.optional.native2ascii.Native2AsciiAdapter;
26import org.apache.tools.ant.taskdefs.optional.native2ascii.Native2AsciiAdapterFactory;
27import org.apache.tools.ant.types.Commandline;
28import org.apache.tools.ant.types.Mapper;
29import org.apache.tools.ant.util.FileNameMapper;
30import org.apache.tools.ant.util.IdentityMapper;
31import org.apache.tools.ant.util.SourceFileScanner;
32import org.apache.tools.ant.util.facade.FacadeTaskHelper;
33import org.apache.tools.ant.util.facade.ImplementationSpecificArgument;
34
35/**
36 * Converts files from native encodings to ASCII.
37 *
38 * @since Ant 1.2
39 */
40public class Native2Ascii extends MatchingTask {
41
42 private boolean reverse = false; // convert from ascii back to native
43 private String encoding = null; // encoding to convert to/from
44 private File srcDir = null; // Where to find input files
45 private File destDir = null; // Where to put output files
46 private String extension = null; // Extension of output files if different
47
48 private Mapper mapper;
49 private FacadeTaskHelper facade = null;
50
51 public Native2Ascii() {
52 facade = new FacadeTaskHelper(Native2AsciiAdapterFactory.getDefault());
53 }
54
55 /**
56 * Flag the conversion to run in the reverse sense,
57 * that is Ascii to Native encoding.
58 *
59 * @param reverse True if the conversion is to be reversed,
60 * otherwise false;
61 */
62 public void setReverse(boolean reverse) {
63 this.reverse = reverse;
64 }
65
66 /**
67 * The value of the reverse attribute.
68 *
69 * @since Ant 1.6.3
70 */
71 public boolean getReverse() {
72 return reverse;
73 }
74
75 /**
76 * Set the encoding to translate to/from.
77 * If unset, the default encoding for the JVM is used.
78 *
79 * @param encoding String containing the name of the Native
80 * encoding to convert from or to.
81 */
82 public void setEncoding(String encoding) {
83 this.encoding = encoding;
84 }
85
86 /**
87 * The value of the reverse attribute.
88 *
89 * @since Ant 1.6.3
90 */
91 public String getEncoding() {
92 return encoding;
93 }
94
95 /**
96 * Set the source directory in which to find files to convert.
97 *
98 * @param srcDir directory to find input file in.
99 */
100 public void setSrc(File srcDir) {
101 this.srcDir = srcDir;
102 }
103
104
105 /**
106 * Set the destination directory to place converted files into.
107 *
108 * @param destDir directory to place output file into.
109 */
110 public void setDest(File destDir) {
111 this.destDir = destDir;
112 }
113
114 /**
115 * Set the extension which converted files should have.
116 * If unset, files will not be renamed.
117 *
118 * @param ext File extension to use for converted files.
119 */
120 public void setExt(String ext) {
121 this.extension = ext;
122 }
123
124 /**
125 * Choose the implementation for this particular task.
126 * @param impl the name of the implemenation
127 * @since Ant 1.6.3
128 */
129 public void setImplementation(String impl) {
130 if ("default".equals(impl)) {
131 facade.setImplementation(Native2AsciiAdapterFactory.getDefault());
132 } else {
133 facade.setImplementation(impl);
134 }
135 }
136
137 /**
138 * Defines the FileNameMapper to use (nested mapper element).
139 *
140 * @return the mapper to use for file name translations.
141 *
142 * @throws BuildException if more than one mapper is defined.
143 */
144 public Mapper createMapper() throws BuildException {
145 if (mapper != null) {
146 throw new BuildException("Cannot define more than one mapper",
147 getLocation());
148 }
149 mapper = new Mapper(getProject());
150 return mapper;
151 }
152
153 /**
154 * A nested filenamemapper
155 * @param fileNameMapper the mapper to add
156 * @since Ant 1.6.3
157 */
158 public void add(FileNameMapper fileNameMapper) {
159 createMapper().add(fileNameMapper);
160 }
161
162 /**
163 * Adds an implementation specific command-line argument.
164 * @return a ImplementationSpecificArgument to be configured
165 *
166 * @since Ant 1.6.3
167 */
168 public ImplementationSpecificArgument createArg() {
169 ImplementationSpecificArgument arg =
170 new ImplementationSpecificArgument();
171 facade.addImplementationArgument(arg);
172 return arg;
173 }
174
175 /**
176 * Execute the task
177 *
178 * @throws BuildException is there is a problem in the task execution.
179 */
180 public void execute() throws BuildException {
181
182 DirectoryScanner scanner = null; // Scanner to find our inputs
183 String[] files; // list of files to process
184
185 // default srcDir to basedir
186 if (srcDir == null) {
187 srcDir = getProject().resolveFile(".");
188 }
189
190 // Require destDir
191 if (destDir == null) {
192 throw new BuildException("The dest attribute must be set.");
193 }
194
195 // if src and dest dirs are the same, require the extension
196 // to be set, so we don't stomp every file. One could still
197 // include a file with the same extension, but ....
198 if (srcDir.equals(destDir) && extension == null && mapper == null) {
199 throw new BuildException("The ext attribute or a mapper must be set if"
200 + " src and dest dirs are the same.");
201 }
202
203 FileNameMapper m = null;
204 if (mapper == null) {
205 if (extension == null) {
206 m = new IdentityMapper();
207 } else {
208 m = new ExtMapper();
209 }
210 } else {
211 m = mapper.getImplementation();
212 }
213
214 scanner = getDirectoryScanner(srcDir);
215 files = scanner.getIncludedFiles();
216 SourceFileScanner sfs = new SourceFileScanner(this);
217 files = sfs.restrict(files, srcDir, destDir, m);
218 int count = files.length;
219 if (count == 0) {
220 return;
221 }
222 String message = "Converting " + count + " file"
223 + (count != 1 ? "s" : "") + " from ";
224 log(message + srcDir + " to " + destDir);
225 for (int i = 0; i < files.length; i++) {
226 convert(files[i], m.mapFileName(files[i])[0]);
227 }
228 }
229
230 /**
231 * Convert a single file.
232 *
233 * @param srcName name of the input file.
234 * @param destName name of the input file.
235 */
236 private void convert(String srcName, String destName)
237 throws BuildException {
238 File srcFile; // File to convert
239 File destFile; // where to put the results
240
241 // Build the full file names
242 srcFile = new File(srcDir, srcName);
243 destFile = new File(destDir, destName);
244
245 // Make sure we're not about to clobber something
246 if (srcFile.equals(destFile)) {
247 throw new BuildException("file " + srcFile
248 + " would overwrite its self");
249 }
250
251 // Make intermediate directories if needed
252 // XXX JDK 1.1 doesn't have File.getParentFile,
253 String parentName = destFile.getParent();
254 if (parentName != null) {
255 File parentFile = new File(parentName);
256
257 if ((!parentFile.exists()) && (!parentFile.mkdirs())) {
258 throw new BuildException("cannot create parent directory "
259 + parentName);
260 }
261 }
262
263 log("converting " + srcName, Project.MSG_VERBOSE);
264 Native2AsciiAdapter ad =
265 Native2AsciiAdapterFactory.getAdapter(facade.getImplementation(),
266 this);
267 if (!ad.convert(this, srcFile, destFile)) {
268 throw new BuildException("conversion failed");
269 }
270 }
271
272 /**
273 * Returns the (implementation specific) settings given as nested
274 * arg elements.
275 *
276 * @since Ant 1.6.3
277 */
278 public String[] getCurrentArgs() {
279 return facade.getArgs();
280 }
281
282 private class ExtMapper implements FileNameMapper {
283
284 public void setFrom(String s) {
285 }
286 public void setTo(String s) {
287 }
288
289 public String[] mapFileName(String fileName) {
290 int lastDot = fileName.lastIndexOf('.');
291 if (lastDot >= 0) {
292 return new String[] {fileName.substring(0, lastDot)
293 + extension};
294 } else {
295 return new String[] {fileName + extension};
296 }
297 }
298 }
299}
Note: See TracBrowser for help on using the repository browser.