1
2
3 package org.galagosearch.tupleflow.typebuilder;
4
5 import java.io.File;
6 import java.io.IOException;
7 import org.antlr.runtime.RecognitionException;
8 import org.apache.maven.plugin.AbstractMojo;
9 import org.apache.maven.plugin.MojoExecutionException;
10
11 /***
12 * This is the Maven plugin that generates Type objects from galagotype specification
13 * files. See the build.xml file and the galagotype files in the galagosearch-core
14 * project to see some examples of this.
15 *
16 * @phase generate-sources
17 * @goal typebuilder
18 * @author trevor
19 */
20 public class TypeBuilderMojo extends AbstractMojo {
21 /***
22 * @parameter expression="${basedir}/src/main/galagotype" default-value="y"
23 */
24 private String sourceDirectory;
25
26 /***
27 * @parameter expression="${basedir}" default-value="y"
28 */
29 private String baseDirectory;
30
31 public String getOutputFilename(String packageName, String typeName) {
32 String[] packagePathComponents = packageName.split("//.");
33 String packageStringPath = baseDirectory;
34 if (baseDirectory == null)
35 System.err.println("baseDir == null");
36 packageStringPath += File.separator + "src" +
37 File.separator + "main" +
38 File.separator + "java";
39 for (String component : packagePathComponents) {
40 packageStringPath += File.separator + component;
41 }
42
43 return packageStringPath + File.separator + typeName + ".java";
44 }
45
46 public void execute() throws MojoExecutionException {
47 if (sourceDirectory == null) return;
48 File[] files = new File(sourceDirectory).listFiles();
49 if (files == null) return;
50
51 for (File f : files) {
52 if (f.isFile() && f.getName().endsWith("galagotype")) {
53 TypeSpecification spec = null;
54 java.io.FileWriter writer = null;
55
56 try {
57 spec = ParserDriver.getTypeSpecification(f.getAbsolutePath());
58 } catch (IOException ex) {
59 throw new MojoExecutionException("Couldn't open file: " + f.getAbsolutePath(), ex);
60 } catch (RecognitionException ex) {
61 throw new MojoExecutionException("Parsing failed: " + f.getAbsolutePath(), ex);
62 }
63
64 String outputFilename =
65 getOutputFilename(spec.getPackageName(), spec.getTypeName());
66 File outputFile = new File(outputFilename);
67
68 if (!outputFile.exists() || f.lastModified() > outputFile.lastModified()) {
69 System.err.println("Generating " + spec.getTypeName());
70 new File(outputFile.getParent()).mkdirs();
71 TemplateTypeBuilder builder = new TemplateTypeBuilder(spec);
72 try {
73 writer = new java.io.FileWriter(outputFilename);
74 } catch(IOException ex) {
75 throw new MojoExecutionException("Trouble creating " + outputFilename, ex);
76 }
77 String comment =
78 "// This file was automatically generated with the command: \n" +
79 "// java org.galagosearch.tupleflow.typebuilder.TypeBuilderMojo ...\n";
80
81 try {
82 writer.write(comment);
83 writer.write(builder.toString());
84 writer.close();
85 } catch(IOException e) {
86 throw new MojoExecutionException("Trouble writing " + outputFilename);
87 }
88 }
89 }
90 }
91 }
92 }