1 21 22 package net.percederberg.grammatica.ant; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 27 import org.apache.tools.ant.BuildException; 28 29 import net.percederberg.grammatica.Grammar; 30 import net.percederberg.grammatica.output.JavaParserGenerator; 31 32 40 public class JavaElement implements ProcessingElement { 41 42 45 private File dir = null; 46 47 50 private String basePackage = null; 51 52 55 private String prefix = null; 56 57 60 private boolean publicAccess = false; 61 62 65 public JavaElement() { 66 } 67 68 73 public void setDir(File dir) { 74 this.dir = dir; 75 } 76 77 83 public void setPackage(String basePackage) { 84 this.basePackage = basePackage; 85 } 86 87 93 public void setPrefix(String prefix) { 94 this.prefix = prefix; 95 } 96 97 103 public void setPublic(boolean publicAccess) { 104 this.publicAccess = publicAccess; 105 } 106 107 113 public void validate() throws BuildException { 114 if (dir == null) { 115 throw new BuildException( 116 "missing 'dir' attribute in <java> element"); 117 } 118 } 119 120 128 public void process(Grammar grammar) throws BuildException { 129 JavaParserGenerator gen = new JavaParserGenerator(grammar); 130 131 gen.setBaseDir(dir); 132 if (basePackage != null) { 133 gen.setBasePackage(basePackage); 134 } 135 if (prefix != null) { 136 gen.setBaseName(prefix); 137 } 138 gen.setPublicAccess(publicAccess); 139 try { 140 System.out.println("Writing Java parser source code..."); 141 gen.write(); 142 System.out.println("Done."); 143 } catch (IOException e) { 144 throw new BuildException(e); 145 } 146 } 147 } 148 | Popular Tags |