KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > output > ParserGenerator


1 /*
2  * ParserGenerator.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.output;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import net.percederberg.grammatica.Grammar;
28
29 /**
30  * The grammar parser generator base class.
31  *
32  * @author Per Cederberg, <per at percederberg dot net>
33  * @version 1.0
34  */

35 public abstract class ParserGenerator {
36
37     /**
38      * The default file comment string.
39      */

40     protected static final String JavaDoc FILE_COMMENT =
41         "THIS FILE HAS BEEN GENERATED AUTOMATICALLY. DO NOT EDIT!";
42
43     /**
44      * The grammar to generate a parser for.
45      */

46     private Grammar grammar;
47
48     /**
49      * The base directory where to write the files.
50      */

51     private File JavaDoc baseDir = null;
52
53     /**
54      * The file comment.
55      */

56     private String JavaDoc fileComment = null;
57
58     /**
59      * Creates a new parser generator.
60      *
61      * @param grammar the grammar to use
62      */

63     protected ParserGenerator(Grammar grammar) {
64         this.grammar = grammar;
65         initialize();
66     }
67
68     /**
69      * Initializes various instance variables.
70      */

71     private void initialize() {
72         StringBuffer JavaDoc buffer;
73         String JavaDoc str;
74         int pos;
75
76         // Create file comment
77
buffer = new StringBuffer JavaDoc();
78         buffer.append(FILE_COMMENT);
79         str = grammar.getDeclaration(Grammar.LICENSE_DECLARATION);
80         if (str != null) {
81             buffer.append("\n\n");
82             pos = str.indexOf('\n');
83             while (pos >= 0) {
84                 buffer.append(str.substring(0, pos).trim());
85                 buffer.append('\n');
86                 str = str.substring(pos + 1);
87                 pos = str.indexOf('\n');
88             }
89             buffer.append(str.trim());
90         }
91         str = grammar.getDeclaration(Grammar.COPYRIGHT_DECLARATION);
92         if (str != null) {
93             buffer.append("\n\n");
94             buffer.append(str);
95         }
96         fileComment = buffer.toString();
97     }
98
99     /**
100      * Returns the grammar that this parser generator works on.
101      *
102      * @return the parser generator grammar
103      */

104     public Grammar getGrammar() {
105         return grammar;
106     }
107
108     /**
109      * Returns the base directory where files will be created.
110      *
111      * @return the base directory
112      */

113     public File JavaDoc getBaseDir() {
114         return baseDir;
115     }
116
117     /**
118      * Sets the base directory where files will be created.
119      *
120      * @param dir the base directory
121      */

122     public void setBaseDir(File JavaDoc dir) {
123         this.baseDir = dir;
124     }
125
126     /**
127      * Returns the file comment.
128      *
129      * @return the file comment
130      */

131     public String JavaDoc getFileComment() {
132         return fileComment;
133     }
134
135     /**
136      * Writes the source code files.
137      *
138      * @throws IOException if the files couldn't be written correctly
139      */

140     public abstract void write() throws IOException JavaDoc;
141 }
142
Popular Tags