KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ValidationElement.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.io.FileReader JavaDoc;
27
28 import org.apache.tools.ant.BuildException;
29
30 import net.percederberg.grammatica.Grammar;
31 import net.percederberg.grammatica.GrammarException;
32 import net.percederberg.grammatica.TreePrinter;
33 import net.percederberg.grammatica.parser.Analyzer;
34 import net.percederberg.grammatica.parser.Node;
35 import net.percederberg.grammatica.parser.ParseException;
36 import net.percederberg.grammatica.parser.Parser;
37 import net.percederberg.grammatica.parser.ParserCreationException;
38 import net.percederberg.grammatica.parser.ParserLogException;
39 import net.percederberg.grammatica.parser.Token;
40 import net.percederberg.grammatica.parser.Tokenizer;
41
42 /**
43  * A grammar validation element. This element validates or tests the
44  * grammar in various ways.
45  *
46  * @author Per Cederberg, <per at percederberg dot net>
47  * @version 1.4
48  * @since 1.4
49  */

50 public class ValidationElement implements ProcessingElement {
51
52     /**
53      * The validation type.
54      */

55     private String JavaDoc type = null;
56
57     /**
58      * The input test file.
59      */

60     private File JavaDoc file = null;
61
62     /**
63      * The quiet output flag.
64      */

65     private boolean quiet = false;
66
67     /**
68      * Creates a new validation element.
69      */

70     public ValidationElement() {
71     }
72
73     /**
74      * Sets the validation type. The type must be one of "debug",
75      * "tokenize", "parse", or "profile".
76      *
77      * @param type the validation type
78      */

79     public void setType(String JavaDoc type) {
80         this.type = type;
81     }
82
83     /**
84      * Sets the input test file. The test file is not needed for the
85      * debug validation type.
86      *
87      * @param file the input test file
88      */

89     public void setInputfile(File JavaDoc file) {
90         this.file = file;
91     }
92
93     /**
94      * Sets the quiet output flag.
95      *
96      * @param quiet the quiet output flag
97      */

98     public void setQuiet(boolean quiet) {
99         this.quiet = quiet;
100     }
101
102     /**
103      * Validates all attributes in the element.
104      *
105      * @throws BuildException if some attribute was missing or had an
106      * invalid value
107      */

108     public void validate() throws BuildException {
109         if (type == null) {
110             throw new BuildException(
111                 "missing 'type' attribute in <validate>");
112         }
113         if (!type.equals("debug")
114          && !type.equals("tokenize")
115          && !type.equals("parse")
116          && !type.equals("profile")) {
117
118             throw new BuildException(
119                 "value of 'type' attribute in <validate> must be one " +
120                 "of 'debug', 'tokenize', 'parse', or 'profile'");
121         }
122         if (file == null && !type.equals("debug")) {
123             throw new BuildException(
124                 "missing 'inputfile' attribute in <validate>");
125         }
126     }
127
128     /**
129      * Proceses the specified grammar.
130      *
131      * @param grammar the grammar to process
132      *
133      * @throws BuildException if the grammar couldn't be processed
134      * correctly
135      */

136     public void process(Grammar grammar) throws BuildException {
137         if (type.equals("debug")) {
138             debug(grammar);
139         } else if (type.equals("tokenize")) {
140             tokenize(grammar);
141         } else if (type.equals("parse")) {
142             parse(grammar);
143         } else if (type.equals("profile")) {
144             profile(grammar);
145         } else {
146             throw new BuildException("unknown <validation> type: " + type);
147         }
148     }
149
150     /**
151      * Debugs a grammar by printing the internal representation.
152      *
153      * @param grammar the grammar to use
154      *
155      * @throws BuildException if a parser couldn't be created
156      */

157     private void debug(Grammar grammar) throws BuildException {
158         Tokenizer tokenizer = null;
159         Parser parser = null;
160
161         // Create tokenizer and parser
162
try {
163             tokenizer = grammar.createTokenizer(null);
164             parser = grammar.createParser(tokenizer);
165         } catch (GrammarException e) {
166             throw new BuildException("in grammar " + grammar.getFileName() +
167                                      ": " + e.getMessage());
168         }
169
170         // Print tokenizer and parser
171
if (!quiet) {
172             System.out.println("Contents of " + grammar.getFileName() + ":");
173             System.out.println();
174             System.out.println("Token Declarations:");
175             System.out.println("-------------------");
176             System.out.print(tokenizer);
177             System.out.println("Production Declarations:");
178             System.out.println("------------------------");
179             System.out.print(parser);
180         }
181     }
182
183     /**
184      * Tokenizes the input file with the token patterns from the
185      * grammar.
186      *
187      * @param grammar the grammar to use
188      *
189      * @throws BuildException if the input file couldn't be tokenized
190      * correctly
191      */

192     private void tokenize(Grammar grammar) throws BuildException {
193         Tokenizer tokenizer;
194         Token token;
195
196         try {
197             tokenizer = grammar.createTokenizer(new FileReader JavaDoc(file));
198             if (!quiet) {
199                 System.out.println("Tokens from " + file + ":");
200             }
201             while ((token = tokenizer.next()) != null) {
202                 if (!quiet) {
203                     System.out.println(token);
204                 }
205             }
206         } catch (FileNotFoundException JavaDoc e) {
207             throw new BuildException(e.getMessage());
208         } catch (GrammarException e) {
209             throw new BuildException("in grammar " + grammar.getFileName() +
210                                      ": " + e.getMessage());
211         } catch (ParseException e) {
212             throw new BuildException("in file " + file + ": " +
213                                      e.getMessage());
214         }
215     }
216
217     /**
218      * Parses the input file with the grammar.
219      *
220      * @param grammar the grammar to use
221      *
222      * @throws BuildException if the input file couldn't be parsed
223      * correctly
224      */

225     private void parse(Grammar grammar) throws BuildException {
226         Tokenizer tokenizer;
227         Analyzer analyzer;
228         Parser parser;
229
230         try {
231             tokenizer = grammar.createTokenizer(new FileReader JavaDoc(file));
232             if (quiet) {
233                 analyzer = null;
234             } else {
235                 analyzer = new TreePrinter(System.out);
236             }
237             parser = grammar.createParser(tokenizer, analyzer);
238             if (!quiet) {
239                 System.out.println("Parse tree from " + file + ":");
240             }
241             parser.parse();
242         } catch (FileNotFoundException JavaDoc e) {
243             throw new BuildException(e.getMessage());
244         } catch (GrammarException e) {
245             throw new BuildException("in grammar " + grammar.getFileName() +
246                                      ": " + e.getMessage());
247         } catch (ParserCreationException e) {
248             throw new BuildException("in grammar " + grammar.getFileName() +
249                                      ": " + e.getMessage());
250         } catch (ParserLogException e) {
251             throw new BuildException("in file " + file + ": " +
252                                      e.getMessage());
253         }
254     }
255
256     /**
257      * Parses the input file with the grammar and prints profiling
258      * information.
259      *
260      * @param grammar the grammar to use
261      *
262      * @throws BuildException if the input file couldn't be profiled
263      * correctly
264      */

265     private void profile(Grammar grammar) throws BuildException {
266         Tokenizer tokenizer;
267         Parser parser;
268         Node node;
269         long time;
270         int counter;
271
272         // Profile tokenizer
273
try {
274             tokenizer = grammar.createTokenizer(new FileReader JavaDoc(file));
275             System.out.println("Tokenizing " + file);
276             time = System.currentTimeMillis();
277             counter = 0;
278             while (tokenizer.next() != null) {
279                 counter++;
280             }
281             time = System.currentTimeMillis() - time;
282             System.out.println(" Time elapsed: " + time + " millisec");
283             System.out.println(" Tokens found: " + counter);
284             System.out.println(" Average speed: " + (counter / time) +
285                                " tokens/millisec");
286         } catch (FileNotFoundException JavaDoc e) {
287             throw new BuildException(e.getMessage());
288         } catch (GrammarException e) {
289             throw new BuildException("in grammar " + grammar.getFileName() +
290                                      ": " + e.getMessage());
291         } catch (ParseException e) {
292             throw new BuildException("in file " + file + ": " +
293                                      e.getMessage());
294         }
295
296         // Profile parser
297
try {
298             tokenizer = grammar.createTokenizer(new FileReader JavaDoc(file));
299             parser = grammar.createParser(tokenizer);
300             System.out.println("Parsing " + file);
301             time = System.currentTimeMillis();
302             node = parser.parse();
303             time = System.currentTimeMillis() - time;
304             counter = 1 + node.getDescendantCount();
305             System.out.println(" Time elapsed: " + time + " millisec");
306             System.out.println(" Nodes found: " + counter);
307             System.out.println(" Average speed: " + (counter / time) +
308                                " nodes/millisec");
309         } catch (FileNotFoundException JavaDoc e) {
310             throw new BuildException(e.getMessage());
311         } catch (GrammarException e) {
312             throw new BuildException("in grammar " + grammar.getFileName() +
313                                      ": " + e.getMessage());
314         } catch (ParserCreationException e) {
315             throw new BuildException("in grammar " + grammar.getFileName() +
316                                      ": " + e.getMessage());
317         } catch (ParserLogException e) {
318             throw new BuildException("in file " + file + ": " +
319                                      e.getMessage());
320         }
321     }
322 }
323
Popular Tags