• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2019 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 package org.unicode.icu.tool.cldrtoicu.ant;
4 
5 import static com.google.common.base.Preconditions.checkNotNull;
6 
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.OutputStreamWriter;
10 import java.io.PrintWriter;
11 import java.nio.file.Files;
12 import java.nio.file.Path;
13 import java.nio.file.Paths;
14 
15 import org.apache.tools.ant.BuildException;
16 import org.apache.tools.ant.Task;
17 import org.unicode.icu.tool.cldrtoicu.CodeGenerator;
18 import org.unicode.icu.tool.cldrtoicu.generator.ResourceFallbackCodeGenerator;
19 
20 // Note: Auto-magical Ant methods are listed as "unused" by IDEs, unless the warning is suppressed.
21 public final class GenerateCodeTask extends Task {
22     private Path cldrPath;
23     private Path cOutDir;
24     private Path javaOutDir;
25     private String action;
26 
27     private class GeneratedFileDef {
28         String cRelativePath;
29         String javaRelativePath;
30         CodeGenerator generator;
31 
GeneratedFileDef(String cRelativePath, String javaRelativePath, CodeGenerator generator)32         public GeneratedFileDef(String cRelativePath, String javaRelativePath, CodeGenerator generator) {
33             this.cRelativePath = cRelativePath;
34             this.javaRelativePath = javaRelativePath;
35             this.generator = generator;
36         }
37     }
38 
39     private GeneratedFileDef[] generatedFileDefs = {
40         new GeneratedFileDef("common/localefallback_data.h", "src/main/java/com/ibm/icu/impl/LocaleFallbackData.java", new ResourceFallbackCodeGenerator()),
41     };
42 
43     @SuppressWarnings("unused")
setCldrDir(String path)44     public void setCldrDir(String path) {
45         // Use String here since on some systems Ant doesn't support automatically converting Path instances.
46         this.cldrPath = checkNotNull(Paths.get(path));
47     }
48 
49     @SuppressWarnings("unused")
setCOutDir(String path)50     public void setCOutDir(String path) {
51         // Use String here since on some systems Ant doesn't support automatically converting Path instances.
52         this.cOutDir = Paths.get(path);
53     }
54 
55     @SuppressWarnings("unused")
setJavaOutDir(String path)56     public void setJavaOutDir(String path) {
57         // Use String here since on some systems Ant doesn't support automatically converting Path instances.
58         this.javaOutDir = Paths.get(path);
59     }
60 
61     @SuppressWarnings("unused")
setAction(String action)62     public void setAction(String action) {
63         // Use String here since on some systems Ant doesn't support automatically converting Path instances.
64         this.action = action;
65     }
66 
67     @SuppressWarnings("unused")
execute()68     public void execute() throws BuildException {
69         for (GeneratedFileDef task : generatedFileDefs) {
70             Path cOutPath = cOutDir.resolve(task.cRelativePath);
71             Path javaOutPath = javaOutDir.resolve(task.javaRelativePath);
72 
73             try {
74                 if (this.action != null && this.action.equals("clean")) {
75                     log("Deleting " + cOutPath + " and " + javaOutPath + "...");
76                     Files.deleteIfExists(cOutPath);
77                     Files.deleteIfExists(javaOutPath);
78                 } else {
79                     Files.createDirectories(cOutPath.getParent());
80                     Files.createDirectories(javaOutPath.getParent());
81 
82                     try (PrintWriter cOut = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cOutPath.toFile())));
83                          PrintWriter javaOut = new PrintWriter(new OutputStreamWriter(new FileOutputStream(javaOutPath.toFile())))) {
84 
85                         log("Generating " + cOutPath + " and " + javaOutPath + "...");
86                         task.generator.generateCode(cldrPath, cOut, javaOut);
87                     }
88                 }
89             } catch (IOException ioException) {
90                 throw new BuildException("IOException: " + ioException.toString());
91             }
92         }
93     }
94 
95 }
96