| 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 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | 0 | public class TypeBuilderMojo extends AbstractMojo { |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
private String sourceDirectory; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
private String baseDirectory; |
| 30 | |
|
| 31 | |
public String getOutputFilename(String packageName, String typeName) { |
| 32 | 0 | String[] packagePathComponents = packageName.split("\\."); |
| 33 | 0 | String packageStringPath = baseDirectory; |
| 34 | 0 | if (baseDirectory == null) |
| 35 | 0 | System.err.println("baseDir == null"); |
| 36 | 0 | packageStringPath += File.separator + "src" + |
| 37 | |
File.separator + "main" + |
| 38 | |
File.separator + "java"; |
| 39 | 0 | for (String component : packagePathComponents) { |
| 40 | 0 | packageStringPath += File.separator + component; |
| 41 | |
} |
| 42 | |
|
| 43 | 0 | return packageStringPath + File.separator + typeName + ".java"; |
| 44 | |
} |
| 45 | |
|
| 46 | |
public void execute() throws MojoExecutionException { |
| 47 | 0 | if (sourceDirectory == null) return; |
| 48 | 0 | File[] files = new File(sourceDirectory).listFiles(); |
| 49 | 0 | if (files == null) return; |
| 50 | |
|
| 51 | 0 | for (File f : files) { |
| 52 | 0 | if (f.isFile() && f.getName().endsWith("galagotype")) { |
| 53 | 0 | TypeSpecification spec = null; |
| 54 | 0 | java.io.FileWriter writer = null; |
| 55 | |
|
| 56 | |
try { |
| 57 | 0 | spec = ParserDriver.getTypeSpecification(f.getAbsolutePath()); |
| 58 | 0 | } catch (IOException ex) { |
| 59 | 0 | throw new MojoExecutionException("Couldn't open file: " + f.getAbsolutePath(), ex); |
| 60 | 0 | } catch (RecognitionException ex) { |
| 61 | 0 | throw new MojoExecutionException("Parsing failed: " + f.getAbsolutePath(), ex); |
| 62 | 0 | } |
| 63 | |
|
| 64 | 0 | String outputFilename = |
| 65 | |
getOutputFilename(spec.getPackageName(), spec.getTypeName()); |
| 66 | 0 | File outputFile = new File(outputFilename); |
| 67 | |
|
| 68 | 0 | if (!outputFile.exists() || f.lastModified() > outputFile.lastModified()) { |
| 69 | 0 | System.err.println("Generating " + spec.getTypeName()); |
| 70 | 0 | new File(outputFile.getParent()).mkdirs(); |
| 71 | 0 | TemplateTypeBuilder builder = new TemplateTypeBuilder(spec); |
| 72 | |
try { |
| 73 | 0 | writer = new java.io.FileWriter(outputFilename); |
| 74 | 0 | } catch(IOException ex) { |
| 75 | 0 | throw new MojoExecutionException("Trouble creating " + outputFilename, ex); |
| 76 | 0 | } |
| 77 | 0 | String comment = |
| 78 | |
"// This file was automatically generated with the command: \n" + |
| 79 | |
"// java org.galagosearch.tupleflow.typebuilder.TypeBuilderMojo ...\n"; |
| 80 | |
|
| 81 | |
try { |
| 82 | 0 | writer.write(comment); |
| 83 | 0 | writer.write(builder.toString()); |
| 84 | 0 | writer.close(); |
| 85 | 0 | } catch(IOException e) { |
| 86 | 0 | throw new MojoExecutionException("Trouble writing " + outputFilename); |
| 87 | 0 | } |
| 88 | |
} |
| 89 | |
} |
| 90 | |
} |
| 91 | 0 | } |
| 92 | |
} |