|
1 | 1 | package org.hamcrest.generator.config;
|
2 | 2 |
|
| 3 | +import org.hamcrest.generator.HamcrestFactoryWriter; |
| 4 | +import org.hamcrest.generator.QuickReferenceWriter; |
3 | 5 | import org.hamcrest.generator.ReflectiveFactoryReader;
|
4 | 6 | import org.hamcrest.generator.SugarConfiguration;
|
5 | 7 | import org.hamcrest.generator.SugarGenerator;
|
|
11 | 13 | import javax.xml.parsers.ParserConfigurationException;
|
12 | 14 | import javax.xml.parsers.SAXParser;
|
13 | 15 | import javax.xml.parsers.SAXParserFactory;
|
| 16 | +import java.io.File; |
| 17 | +import java.io.FileWriter; |
14 | 18 | import java.io.IOException;
|
15 | 19 |
|
16 | 20 | public class XmlConfigurator {
|
@@ -47,8 +51,54 @@ private void addClass(String className) throws ClassNotFoundException {
|
47 | 51 | sugarConfiguration.addFactoryMethods(new ReflectiveFactoryReader(cls));
|
48 | 52 | }
|
49 | 53 |
|
| 54 | + |
50 | 55 | public static void main(String[] args) throws Exception {
|
51 |
| - XmlConfigurator config = new XmlConfigurator(new SugarGenerator(), XmlConfigurator.class.getClassLoader()); |
52 |
| - config.load(new InputSource("matchers.xml")); |
| 56 | + |
| 57 | + if (args.length != 3) { |
| 58 | + System.err.println("Args: config-file generated-class output-dir"); |
| 59 | + System.err.println(""); |
| 60 | + System.err.println(" config-file : Path to config file listing matchers to generate sugar for."); |
| 61 | + System.err.println(" e.g. path/to/matchers.xml"); |
| 62 | + System.err.println(""); |
| 63 | + System.err.println("generated-class : Full name of class to generate."); |
| 64 | + System.err.println(" e.g. org.myproject.MyMatchers"); |
| 65 | + System.err.println(""); |
| 66 | + System.err.println(" output-dir : Where to output generated code (package subdirs will be"); |
| 67 | + System.err.println(" automatically created)."); |
| 68 | + System.err.println(" e.g. build/generated-code"); |
| 69 | + System.exit(-1); |
| 70 | + } |
| 71 | + |
| 72 | + String configFile = args[0]; |
| 73 | + String fullClassName = args[1]; |
| 74 | + File outputDir = new File(args[2]); |
| 75 | + |
| 76 | + String fileName = fullClassName.replaceAll("\\.", File.separator) + ".java"; |
| 77 | + int dotIndex = fullClassName.lastIndexOf("."); |
| 78 | + String packageName = dotIndex == -1 ? "" : fullClassName.substring(0, dotIndex); |
| 79 | + String shortClassName = fullClassName.substring(dotIndex + 1); |
| 80 | + |
| 81 | + if (!outputDir.isDirectory()) { |
| 82 | + System.err.println("Output directory not found : " + outputDir.getAbsolutePath()); |
| 83 | + System.exit(-1); |
| 84 | + } |
| 85 | + |
| 86 | + File outputFile = new File(outputDir, fileName); |
| 87 | + outputFile.getParentFile().mkdirs(); |
| 88 | + |
| 89 | + SugarGenerator sugarGenerator = new SugarGenerator(); |
| 90 | + try { |
| 91 | + sugarGenerator.addWriter(new HamcrestFactoryWriter( |
| 92 | + packageName, shortClassName, new FileWriter(outputFile))); |
| 93 | + sugarGenerator.addWriter(new QuickReferenceWriter(System.out)); |
| 94 | + |
| 95 | + XmlConfigurator xmlConfigurator = new XmlConfigurator(sugarGenerator, XmlConfigurator.class.getClassLoader()); |
| 96 | + xmlConfigurator.load(new InputSource(configFile)); |
| 97 | + |
| 98 | + System.out.println("Generating " + fullClassName); |
| 99 | + sugarGenerator.generate(); |
| 100 | + } finally { |
| 101 | + sugarGenerator.close(); |
| 102 | + } |
53 | 103 | }
|
54 | 104 | }
|
0 commit comments