001: /*
002: * GrammaticaTask.java
003: *
004: * This work is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published
006: * by the Free Software Foundation; either version 2 of the License,
007: * or (at your option) any later version.
008: *
009: * This work is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * As a special exception, the copyright holders of this library give
020: * you permission to link this library with independent modules to
021: * produce an executable, regardless of the license terms of these
022: * independent modules, and to copy and distribute the resulting
023: * executable under terms of your choice, provided that you also meet,
024: * for each linked independent module, the terms and conditions of the
025: * license of that module. An independent module is a module which is
026: * not derived from or based on this library. If you modify this
027: * library, you may extend this exception to your version of the
028: * library, but you are not obligated to do so. If you do not wish to
029: * do so, delete this exception statement from your version.
030: *
031: * Copyright (c) 2003 Per Cederberg. All rights reserved.
032: */
033:
034: package net.percederberg.grammatica.ant;
035:
036: import java.io.File;
037: import java.io.FileNotFoundException;
038: import java.util.Vector;
039:
040: import org.apache.tools.ant.BuildException;
041: import org.apache.tools.ant.Task;
042:
043: import net.percederberg.grammatica.Grammar;
044: import net.percederberg.grammatica.GrammarException;
045: import net.percederberg.grammatica.parser.ParserLogException;
046:
047: /**
048: * A Grammatica Ant task.
049: *
050: * @author Per Cederberg, <per at percederberg dot net>
051: * @version 1.4
052: * @since 1.4
053: */
054: public class GrammaticaTask extends Task {
055:
056: /**
057: * The grammar file to process.
058: */
059: private File file = null;
060:
061: /**
062: * The fail on error flag.
063: */
064: private boolean failOnError = true;
065:
066: /**
067: * The list of processing elements.
068: */
069: private Vector processors = new Vector();
070:
071: /**
072: * Creates a new Grammatica Ant task.
073: */
074: public GrammaticaTask() {
075: }
076:
077: /**
078: * Sets the grammar file.
079: *
080: * @param file the new grammar file
081: */
082: public void setGrammar(File file) {
083: this .file = file;
084: }
085:
086: /**
087: * Sets the fail on error flag. This flag defaults to true.
088: *
089: * @param failOnError the new fail on error flag value
090: */
091: public void setFailonerror(boolean failOnError) {
092: this .failOnError = failOnError;
093: }
094:
095: /**
096: * Adds a new validation inner element.
097: *
098: * @param elem the validation element
099: */
100: public void addValidation(ValidationElement elem) {
101: processors.add(elem);
102: }
103:
104: /**
105: * Adds a new C# code generation inner element.
106: *
107: * @param elem the C# code generation element
108: */
109: public void addCSharp(CSharpElement elem) {
110: processors.add(elem);
111: }
112:
113: /**
114: * Adds a new Java code generation inner element.
115: *
116: * @param elem the Java code generation element
117: */
118: public void addJava(JavaElement elem) {
119: processors.add(elem);
120: }
121:
122: /**
123: * Executes the task.
124: *
125: * @throws BuildException if the task execution failed
126: */
127: public void execute() throws BuildException {
128: Grammar grammar;
129: int i;
130:
131: // Validate all elements
132: if (file == null) {
133: throw new BuildException("missing 'grammar' attribute");
134: }
135: if (processors.size() <= 0) {
136: throw new BuildException(
137: "missing <validate>, <java>, or <csharp> inner element");
138: }
139: for (i = 0; i < processors.size(); i++) {
140: ((ProcessingElement) processors.get(i)).validate();
141: }
142:
143: // Read grammar file
144: try {
145: grammar = new Grammar(file);
146: } catch (FileNotFoundException e) {
147: throw new BuildException(e);
148: } catch (ParserLogException e) {
149: handleError(e);
150: return;
151: } catch (GrammarException e) {
152: handleError(e);
153: return;
154: }
155:
156: // Process grammar file
157: for (i = 0; i < processors.size(); i++) {
158: try {
159: ((ProcessingElement) processors.get(i))
160: .process(grammar);
161: } catch (BuildException e) {
162: handleError(e);
163: }
164: }
165: }
166:
167: /**
168: * Handles an error. This will either print the error or throw
169: * a build exception, depending of the failOnError flag.
170: *
171: * @param e the error exception
172: *
173: * @throws BuildException if the build should fail on errors
174: */
175: private void handleError(Exception e) throws BuildException {
176: if (failOnError) {
177: if (e instanceof BuildException) {
178: throw (BuildException) e;
179: } else {
180: throw new BuildException(e);
181: }
182: }
183: System.err.println("ERROR: " + e.getMessage());
184: }
185: }
|