KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > ant > GrammaticaTask


1 /*
2  * GrammaticaTask.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica.ant;
23
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.util.Vector JavaDoc;
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 /**
36  * A Grammatica Ant task.
37  *
38  * @author Per Cederberg, <per at percederberg dot net>
39  * @version 1.5
40  * @since 1.4
41  */

42 public class GrammaticaTask extends Task {
43
44     /**
45      * The grammar file to process.
46      */

47     private File JavaDoc file = null;
48
49     /**
50      * The fail on error flag.
51      */

52     private boolean failOnError = true;
53
54     /**
55      * The list of processing elements.
56      */

57     private Vector JavaDoc processors = new Vector JavaDoc();
58
59     /**
60      * Creates a new Grammatica Ant task.
61      */

62     public GrammaticaTask() {
63     }
64
65     /**
66      * Sets the grammar file.
67      *
68      * @param file the new grammar file
69      */

70     public void setGrammar(File JavaDoc file) {
71         this.file = file;
72     }
73
74     /**
75      * Sets the fail on error flag. This flag defaults to true.
76      *
77      * @param failOnError the new fail on error flag value
78      */

79     public void setFailonerror(boolean failOnError) {
80         this.failOnError = failOnError;
81     }
82
83     /**
84      * Adds a new validation inner element.
85      *
86      * @param elem the validation element
87      */

88     public void addValidation(ValidationElement elem) {
89         processors.add(elem);
90     }
91
92     /**
93      * Adds a new C# code generation inner element.
94      *
95      * @param elem the C# code generation element
96      */

97     public void addCSharp(CSharpElement elem) {
98         processors.add(elem);
99     }
100
101     /**
102      * Adds a new Java code generation inner element.
103      *
104      * @param elem the Java code generation element
105      */

106     public void addJava(JavaElement elem) {
107         processors.add(elem);
108     }
109
110     /**
111      * Adds a new Visual Basic code generation inner element.
112      *
113      * @param elem the Visual Basic code generation element
114      */

115     public void addVisualBasic(VisualBasicElement elem) {
116         processors.add(elem);
117     }
118
119     /**
120      * Executes the task.
121      *
122      * @throws BuildException if the task execution failed
123      */

124     public void execute() throws BuildException {
125         Grammar grammar;
126         int i;
127
128         // Validate all elements
129
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         // Read grammar file
142
try {
143             grammar = new Grammar(file);
144         } catch (FileNotFoundException JavaDoc 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         // Process grammar file
155
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     /**
165      * Handles an error. This will either print the error or throw
166      * a build exception, depending of the failOnError flag.
167      *
168      * @param e the error exception
169      *
170      * @throws BuildException if the build should fail on errors
171      */

172     private void handleError(Exception JavaDoc 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