Source code: macbuild/Util.java
1 /*
2 * Util.java
3 *
4 * Created on September 15, 2003, 10:30 PM
5 * This software is released under the Apache Software License, version 1.1.
6 * See the file LICENSE.txt for more information.
7 */
8
9 package macbuild;
10
11 import java.io.*;
12 import org.apache.tools.ant.types.FilterSet;
13 import org.apache.tools.ant.util.FileUtils;
14
15 /** Various utility functions.
16 *
17 * @author Mike Baranczak
18 */
19 public class Util {
20
21 private Util() {}
22
23 private static FileUtils fileUtils = FileUtils.newFileUtils();
24
25 /** Compile-time switch. */
26 public static final boolean DEBUG = true;
27
28 /** If (DEBUG == true), print debug info, otherwise do nothing. */
29 public final static void dblog(String msg) {
30 if (DEBUG)
31 System.out.println("- DEBUG: " + msg);
32 }
33
34 /** dest may be a regular file or a directory. */
35 public static void copyFile(File src, File dest) throws IOException {
36 File realDest = null;
37 if (dest.isDirectory()) {
38 realDest = new File(dest, src.getName());
39 } else {
40 realDest = dest;
41 }
42 dblog("copying '"+src+"' to '"+realDest+"'.");
43 fileUtils.copyFile(src, realDest);
44 }
45
46 /** Copies text data from a resource to a file, applying filters. */
47 public static void copyResource(String src, File dest, FilterSet filters) throws IOException {
48 dblog("writing data to '"+dest+"'");
49 InputStreamReader inISR = new InputStreamReader(Util.class.getResourceAsStream(src), "UTF-8");
50 BufferedReader inBR = new BufferedReader(inISR);
51 PrintWriter out = new PrintWriter(new FileOutputStream(dest));
52 String line;
53 while ((line = inBR.readLine()) != null) {
54 out.println(filters.replaceTokens(line));
55 }
56 out.flush();
57 out.close();
58 inBR.close();
59 }
60 }