1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.utgenome.gwt.utgb;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Properties;
31
32 import org.xerial.util.FileResource;
33 import org.xerial.util.log.LogLevel;
34 import org.xerial.util.log.Logger;
35 import org.xerial.util.opt.Argument;
36 import org.xerial.util.opt.Option;
37 import org.xerial.util.opt.OptionParser;
38 import org.xerial.util.opt.OptionParserException;
39 import org.xerial.util.opt.Usage;
40 import org.xerial.util.text.Template;
41
42 public class TrackLoaderGenerator {
43 private static Logger _logger = Logger.getLogger(TrackLoaderGenerator.class);
44
45 @Usage(command = "> java -cp bin org.utgenome.gwt.utgb.TrackLoaderGenerator [option] search_folder")
46 public static class Config {
47
48 @Option(symbol = "h", longName = "help", description = "display help messages")
49 boolean displayHelp = false;
50 @Option(symbol = "v", longName = "verbose", description = "display verbose messages")
51 boolean verbose = false;
52 @Option(symbol = "p", longName = "package", varName = "NAME", description = "package name")
53 String packageName = null;
54 @Option(symbol = "c", longName = "class", varName = "NAME", description = "class name")
55 String className = null;
56
57 @Argument
58 String searchFolderName = null;
59 }
60
61 public static void main(String[] args) throws OptionParserException, IOException {
62 Config config = new Config();
63 OptionParser optionParser = new OptionParser(config);
64
65 optionParser.parse(args);
66 if (config.displayHelp) {
67 optionParser.printUsage();
68 }
69
70 if (config.verbose) {
71 Logger.getRootLogger().setLogLevel(LogLevel.ALL);
72 }
73
74 if (config.searchFolderName == null) {
75 System.err.println("no search folder is specified");
76 return;
77 }
78
79 _logger.debug("search folder = " + config.searchFolderName);
80
81 if (config.packageName == null || config.className == null) {
82 System.err.println("no package or class name is given");
83 return;
84 }
85
86
87 File searchFolder = new File(config.searchFolderName);
88 TrackResourceFinder finder = new TrackResourceFinder(searchFolder);
89 finder.enter(searchFolder);
90
91
92 Template template = new Template(FileResource.find(TrackLoaderGenerator.class, "TrackLoader.java.template").openStream());
93 Properties p = new Properties();
94 p.put("trackClasses", finder.trackClassFile);
95 p.put("trackGroupClasses", finder.trackGroupClassFile);
96 p.put("packageName", config.packageName);
97 p.put("className", config.className);
98 String result = template.apply(p);
99 System.out.print(result);
100 }
101
102 static class TrackResourceFinder {
103 ArrayList<String> trackClassFile = new ArrayList<String>();
104 ArrayList<String> trackGroupClassFile = new ArrayList<String>();
105
106 File searchFolder;
107
108 public TrackResourceFinder(File searchFolder) {
109 if (!searchFolder.isDirectory())
110 throw new IllegalArgumentException(searchFolder.getName() + " is not a directory");
111 this.searchFolder = searchFolder;
112 }
113
114 private String getClassName(File classFile) {
115 String classFilePath = classFile.getAbsolutePath().replace(searchFolder.getAbsolutePath() + File.separator, "");
116 return classFilePath.replace(File.separator, ".").replace(".class", "");
117 }
118
119 public void enter(File directory) {
120 assert (directory.isDirectory());
121
122 _logger.trace("enter the directory: " + directory.getName());
123 File[] fileList = directory.listFiles();
124 for (File file : fileList) {
125 if (file.isDirectory()) {
126 enter(file);
127 }
128 else {
129 if (file.getName().endsWith("Track.class") && !file.getName().equals("Track.class")) {
130 String className = getClassName(file);
131 _logger.trace("found a track class: " + className);
132 trackClassFile.add(className);
133 }
134 if (file.getName().endsWith("TrackGroup.class")) {
135 String className = getClassName(file);
136 _logger.trace("found a track group class: " + className);
137 trackGroupClassFile.add(className);
138 }
139
140 }
141 }
142
143 }
144
145 }
146
147 }