1. /*
  2. * Copyright 2000-2004 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. package org.apache.tools.ant.taskdefs.optional;
  18. import java.io.File;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.DirectoryScanner;
  21. import org.apache.tools.ant.Project;
  22. import org.apache.tools.ant.taskdefs.MatchingTask;
  23. import org.apache.tools.ant.types.Commandline;
  24. import org.apache.tools.ant.types.Mapper;
  25. import org.apache.tools.ant.util.FileNameMapper;
  26. import org.apache.tools.ant.util.IdentityMapper;
  27. import org.apache.tools.ant.util.SourceFileScanner;
  28. /**
  29. * Converts files from native encodings to ASCII.
  30. *
  31. * @since Ant 1.2
  32. */
  33. public class Native2Ascii extends MatchingTask {
  34. private boolean reverse = false; // convert from ascii back to native
  35. private String encoding = null; // encoding to convert to/from
  36. private File srcDir = null; // Where to find input files
  37. private File destDir = null; // Where to put output files
  38. private String extension = null; // Extension of output files if different
  39. private Mapper mapper;
  40. /**
  41. * Flag the conversion to run in the reverse sense,
  42. * that is Ascii to Native encoding.
  43. *
  44. * @param reverse True if the conversion is to be reversed,
  45. * otherwise false;
  46. */
  47. public void setReverse(boolean reverse) {
  48. this.reverse = reverse;
  49. }
  50. /**
  51. * Set the encoding to translate to/from.
  52. * If unset, the default encoding for the JVM is used.
  53. *
  54. * @param encoding String containing the name of the Native
  55. * encoding to convert from or to.
  56. */
  57. public void setEncoding(String encoding) {
  58. this.encoding = encoding;
  59. }
  60. /**
  61. * Set the source directory in which to find files to convert.
  62. *
  63. * @param srcDir directory to find input file in.
  64. */
  65. public void setSrc(File srcDir) {
  66. this.srcDir = srcDir;
  67. }
  68. /**
  69. * Set the destination directory to place converted files into.
  70. *
  71. * @param destDir directory to place output file into.
  72. */
  73. public void setDest(File destDir) {
  74. this.destDir = destDir;
  75. }
  76. /**
  77. * Set the extension which converted files should have.
  78. * If unset, files will not be renamed.
  79. *
  80. * @param ext File extension to use for converted files.
  81. */
  82. public void setExt(String ext) {
  83. this.extension = ext;
  84. }
  85. /**
  86. * Defines the FileNameMapper to use (nested mapper element).
  87. *
  88. * @return the mapper to use for file name translations.
  89. *
  90. * @throws BuildException if more than one mapper is defined.
  91. */
  92. public Mapper createMapper() throws BuildException {
  93. if (mapper != null) {
  94. throw new BuildException("Cannot define more than one mapper",
  95. getLocation());
  96. }
  97. mapper = new Mapper(getProject());
  98. return mapper;
  99. }
  100. /**
  101. * Execute the task
  102. *
  103. * @throws BuildException is there is a problem in the task execution.
  104. */
  105. public void execute() throws BuildException {
  106. DirectoryScanner scanner = null; // Scanner to find our inputs
  107. String[] files; // list of files to process
  108. // default srcDir to basedir
  109. if (srcDir == null) {
  110. srcDir = getProject().resolveFile(".");
  111. }
  112. // Require destDir
  113. if (destDir == null) {
  114. throw new BuildException("The dest attribute must be set.");
  115. }
  116. // if src and dest dirs are the same, require the extension
  117. // to be set, so we don't stomp every file. One could still
  118. // include a file with the same extension, but ....
  119. if (srcDir.equals(destDir) && extension == null && mapper == null) {
  120. throw new BuildException("The ext attribute or a mapper must be set if"
  121. + " src and dest dirs are the same.");
  122. }
  123. FileNameMapper m = null;
  124. if (mapper == null) {
  125. if (extension == null) {
  126. m = new IdentityMapper();
  127. } else {
  128. m = new ExtMapper();
  129. }
  130. } else {
  131. m = mapper.getImplementation();
  132. }
  133. scanner = getDirectoryScanner(srcDir);
  134. files = scanner.getIncludedFiles();
  135. SourceFileScanner sfs = new SourceFileScanner(this);
  136. files = sfs.restrict(files, srcDir, destDir, m);
  137. int count = files.length;
  138. if (count == 0) {
  139. return;
  140. }
  141. String message = "Converting " + count + " file"
  142. + (count != 1 ? "s" : "") + " from ";
  143. log(message + srcDir + " to " + destDir);
  144. for (int i = 0; i < files.length; i++) {
  145. convert(files[i], m.mapFileName(files[i])[0]);
  146. }
  147. }
  148. /**
  149. * Convert a single file.
  150. *
  151. * @param srcName name of the input file.
  152. * @param destName name of the input file.
  153. */
  154. private void convert(String srcName, String destName) throws BuildException {
  155. Commandline cmd = new Commandline(); // Command line to run
  156. File srcFile; // File to convert
  157. File destFile; // where to put the results
  158. // Set up the basic args (this could be done once, but
  159. // it's cleaner here)
  160. if (reverse) {
  161. cmd.createArgument().setValue("-reverse");
  162. }
  163. if (encoding != null) {
  164. cmd.createArgument().setValue("-encoding");
  165. cmd.createArgument().setValue(encoding);
  166. }
  167. // Build the full file names
  168. srcFile = new File(srcDir, srcName);
  169. destFile = new File(destDir, destName);
  170. cmd.createArgument().setFile(srcFile);
  171. cmd.createArgument().setFile(destFile);
  172. // Make sure we're not about to clobber something
  173. if (srcFile.equals(destFile)) {
  174. throw new BuildException("file " + srcFile
  175. + " would overwrite its self");
  176. }
  177. // Make intermediate directories if needed
  178. // XXX JDK 1.1 doesn't have File.getParentFile,
  179. String parentName = destFile.getParent();
  180. if (parentName != null) {
  181. File parentFile = new File(parentName);
  182. if ((!parentFile.exists()) && (!parentFile.mkdirs())) {
  183. throw new BuildException("cannot create parent directory "
  184. + parentName);
  185. }
  186. }
  187. log("converting " + srcName, Project.MSG_VERBOSE);
  188. sun.tools.native2ascii.Main n2a
  189. = new sun.tools.native2ascii.Main();
  190. if (!n2a.convert(cmd.getArguments())) {
  191. throw new BuildException("conversion failed");
  192. }
  193. }
  194. private class ExtMapper implements FileNameMapper {
  195. public void setFrom(String s) {
  196. }
  197. public void setTo(String s) {
  198. }
  199. public String[] mapFileName(String fileName) {
  200. int lastDot = fileName.lastIndexOf('.');
  201. if (lastDot >= 0) {
  202. return new String[] {fileName.substring(0, lastDot)
  203. + extension};
  204. } else {
  205. return new String[] {fileName + extension};
  206. }
  207. }
  208. }
  209. }