KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * VisualBasicElement.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) 2004-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.IOException JavaDoc;
26
27 import org.apache.tools.ant.BuildException;
28
29 import net.percederberg.grammatica.Grammar;
30 import net.percederberg.grammatica.output.VisualBasicParserGenerator;
31
32 /**
33  * A Visual Basic output element. This element creates Visual
34  * Basic.NET source code for a parser.
35  *
36  * @author Per Cederberg, <per at percederberg dot net>
37  * @version 1.5
38  * @since 1.5
39  */

40 public class VisualBasicElement implements ProcessingElement {
41
42     /**
43      * The output directory.
44      */

45     private File JavaDoc dir = null;
46
47     /**
48      * The namespace name.
49      */

50     private String JavaDoc namespace = null;
51
52     /**
53      * The class name prefix.
54      */

55     private String JavaDoc prefix = null;
56
57     /**
58      * The public access flag.
59      */

60     private boolean publicAccess = false;
61
62     /**
63      * Creates a new Visual Basic output element.
64      */

65     public VisualBasicElement() {
66     }
67
68     /**
69      * Sets the output directory.
70      *
71      * @param dir the new output directory
72      */

73     public void setDir(File JavaDoc dir) {
74         this.dir = dir;
75     }
76
77     /**
78      * Sets the output namespace. By default no namespace declaration
79      * will be used.
80      *
81      * @param namespace the new output namespace
82      */

83     public void setNamespace(String JavaDoc namespace) {
84         this.namespace = namespace;
85     }
86
87     /**
88      * Sets the output class name prefix. By default the grammar file
89      * name will be used.
90      *
91      * @param prefix the new output class name prefix
92      */

93     public void setPrefix(String JavaDoc prefix) {
94         this.prefix = prefix;
95     }
96
97     /**
98      * Sets the public access to types flag. By default only internal
99      * access is allowed.
100      *
101      * @param publicAccess the public access flag
102      */

103     public void setPublic(boolean publicAccess) {
104         this.publicAccess = publicAccess;
105     }
106
107     /**
108      * Validates all attributes in the element.
109      *
110      * @throws BuildException if some attribute was missing or had an
111      * invalid value
112      */

113     public void validate() throws BuildException {
114         if (dir == null) {
115             throw new BuildException(
116                 "missing 'dir' attribute in <visualbasic> element");
117         }
118     }
119
120     /**
121      * Proceses the specified grammar.
122      *
123      * @param grammar the grammar to process
124      *
125      * @throws BuildException if the grammar couldn't be processed
126      * correctly
127      */

128     public void process(Grammar grammar) throws BuildException {
129         VisualBasicParserGenerator gen;
130
131         gen = new VisualBasicParserGenerator(grammar);
132         gen.setBaseDir(dir);
133         if (namespace != null) {
134             gen.setNamespace(namespace);
135         }
136         if (prefix != null) {
137             gen.setBaseName(prefix);
138         }
139         gen.setPublicAccess(publicAccess);
140         try {
141             System.out.println("Writing Visual Basic parser source code...");
142             gen.write();
143             System.out.println("Done.");
144         } catch (IOException JavaDoc e) {
145             throw new BuildException(e);
146         }
147     }
148 }
149
Popular Tags