001 // BSD License (http://www.galagosearch.org/license)
002
003 package org.galagosearch.tupleflow.typebuilder;
004
005 import java.io.File;
006 import java.io.IOException;
007 import org.antlr.runtime.RecognitionException;
008 import org.apache.maven.plugin.AbstractMojo;
009 import org.apache.maven.plugin.MojoExecutionException;
010
011 /**
012 * This is the Maven plugin that generates Type objects from galagotype specification
013 * files. See the build.xml file and the galagotype files in the galagosearch-core
014 * project to see some examples of this.
015 *
016 * @phase generate-sources
017 * @goal typebuilder
018 * @author trevor
019 */
020 public class TypeBuilderMojo extends AbstractMojo {
021 /**
022 * @parameter expression="${basedir}/src/main/galagotype" default-value="y"
023 */
024 private String sourceDirectory;
025
026 /**
027 * @parameter expression="${basedir}" default-value="y"
028 */
029 private String baseDirectory;
030
031 public String getOutputFilename(String packageName, String typeName) {
032 String[] packagePathComponents = packageName.split("\\.");
033 String packageStringPath = baseDirectory;
034 if (baseDirectory == null)
035 System.err.println("baseDir == null");
036 packageStringPath += File.separator + "src" +
037 File.separator + "main" +
038 File.separator + "java";
039 for (String component : packagePathComponents) {
040 packageStringPath += File.separator + component;
041 }
042 // Create the filename:
043 return packageStringPath + File.separator + typeName + ".java";
044 }
045
046 public void execute() throws MojoExecutionException {
047 if (sourceDirectory == null) return;
048 File[] files = new File(sourceDirectory).listFiles();
049 if (files == null) return;
050
051 for (File f : files) {
052 if (f.isFile() && f.getName().endsWith("galagotype")) {
053 TypeSpecification spec = null;
054 java.io.FileWriter writer = null;
055
056 try {
057 spec = ParserDriver.getTypeSpecification(f.getAbsolutePath());
058 } catch (IOException ex) {
059 throw new MojoExecutionException("Couldn't open file: " + f.getAbsolutePath(), ex);
060 } catch (RecognitionException ex) {
061 throw new MojoExecutionException("Parsing failed: " + f.getAbsolutePath(), ex);
062 }
063
064 String outputFilename =
065 getOutputFilename(spec.getPackageName(), spec.getTypeName());
066 File outputFile = new File(outputFilename);
067
068 if (!outputFile.exists() || f.lastModified() > outputFile.lastModified()) {
069 System.err.println("Generating " + spec.getTypeName());
070 new File(outputFile.getParent()).mkdirs();
071 TemplateTypeBuilder builder = new TemplateTypeBuilder(spec);
072 try {
073 writer = new java.io.FileWriter(outputFilename);
074 } catch(IOException ex) {
075 throw new MojoExecutionException("Trouble creating " + outputFilename, ex);
076 }
077 String comment =
078 "// This file was automatically generated with the command: \n" +
079 "// java org.galagosearch.tupleflow.typebuilder.TypeBuilderMojo ...\n";
080
081 try {
082 writer.write(comment);
083 writer.write(builder.toString());
084 writer.close();
085 } catch(IOException e) {
086 throw new MojoExecutionException("Trouble writing " + outputFilename);
087 }
088 }
089 }
090 }
091 }
092 }