1 21 22 package net.percederberg.grammatica.ant; 23 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.util.Vector ; 27 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Task; 30 31 import net.percederberg.grammatica.Grammar; 32 import net.percederberg.grammatica.GrammarException; 33 import net.percederberg.grammatica.parser.ParserLogException; 34 35 42 public class GrammaticaTask extends Task { 43 44 47 private File file = null; 48 49 52 private boolean failOnError = true; 53 54 57 private Vector processors = new Vector (); 58 59 62 public GrammaticaTask() { 63 } 64 65 70 public void setGrammar(File file) { 71 this.file = file; 72 } 73 74 79 public void setFailonerror(boolean failOnError) { 80 this.failOnError = failOnError; 81 } 82 83 88 public void addValidation(ValidationElement elem) { 89 processors.add(elem); 90 } 91 92 97 public void addCSharp(CSharpElement elem) { 98 processors.add(elem); 99 } 100 101 106 public void addJava(JavaElement elem) { 107 processors.add(elem); 108 } 109 110 115 public void addVisualBasic(VisualBasicElement elem) { 116 processors.add(elem); 117 } 118 119 124 public void execute() throws BuildException { 125 Grammar grammar; 126 int i; 127 128 if (file == null) { 130 throw new BuildException("missing 'grammar' attribute"); 131 } 132 if (processors.size() <= 0) { 133 throw new BuildException( 134 "missing <validate>, <java>, <csharp> or <visualbasic> " + 135 "inner element"); 136 } 137 for (i = 0; i < processors.size(); i++) { 138 ((ProcessingElement) processors.get(i)).validate(); 139 } 140 141 try { 143 grammar = new Grammar(file); 144 } catch (FileNotFoundException e) { 145 throw new BuildException(e); 146 } catch (ParserLogException e) { 147 handleError(e); 148 return; 149 } catch (GrammarException e) { 150 handleError(e); 151 return; 152 } 153 154 for (i = 0; i < processors.size(); i++) { 156 try { 157 ((ProcessingElement) processors.get(i)).process(grammar); 158 } catch (BuildException e) { 159 handleError(e); 160 } 161 } 162 } 163 164 172 private void handleError(Exception e) throws BuildException { 173 if (failOnError) { 174 if (e instanceof BuildException) { 175 throw (BuildException) e; 176 } else { 177 throw new BuildException(e); 178 } 179 } 180 System.err.println("ERROR: " + e.getMessage()); 181 } 182 } 183 | Popular Tags |