KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > java > JavaFile


1 /*
2  * JavaFile.java
3  *
4  * This work is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This work is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * As a special exception, the copyright holders of this library give
20  * you permission to link this library with independent modules to
21  * produce an executable, regardless of the license terms of these
22  * independent modules, and to copy and distribute the resulting
23  * executable under terms of your choice, provided that you also meet,
24  * for each linked independent module, the terms and conditions of the
25  * license of that module. An independent module is a module which is
26  * not derived from or based on this library. If you modify this
27  * library, you may extend this exception to your version of the
28  * library, but you are not obligated to do so. If you do not wish to
29  * do so, delete this exception statement from your version.
30  *
31  * Copyright (c) 2003-2004 Per Cederberg. All rights reserved.
32  */

33
34 package net.percederberg.grammatica.code.java;
35
36 import java.io.File JavaDoc;
37 import java.io.FileWriter JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.PrintWriter JavaDoc;
40
41 import net.percederberg.grammatica.code.CodeElement;
42 import net.percederberg.grammatica.code.CodeElementContainer;
43 import net.percederberg.grammatica.code.CodeStyle;
44
45 /**
46  * A class generating a Java code file.
47  *
48  * @author Per Cederberg, <per at percederberg dot net>
49  * @version 1.5
50  */

51 public class JavaFile extends CodeElementContainer {
52
53     /**
54      * The directory to write to.
55      */

56     private File JavaDoc dir;
57
58     /**
59      * The package this file is contained in.
60      */

61     private JavaPackage filePackage;
62
63     /**
64      * The first class or interface added.
65      */

66     private CodeElement first = null;
67
68     /**
69      * Creates a new Java code file in the specified file.
70      *
71      * @param basedir the base output directory
72      */

73     public JavaFile(File JavaDoc basedir) {
74         this.dir = basedir;
75         this.filePackage = null;
76     }
77
78     /**
79      * Creates a new Java code file in the specified base directory
80      * and package. The file name will be retrieved from the first
81      * class or interface added to this file.
82      *
83      * @param basedir the base output directory
84      * @param filePackage the package the file belongs to
85      */

86     public JavaFile(File JavaDoc basedir, JavaPackage filePackage) {
87         this.dir = filePackage.toFile(basedir);
88         this.filePackage = filePackage;
89         addElement(filePackage);
90     }
91
92     /**
93      * Returns the file name. Note that if no class has been added to
94      * the file, a default file name will be returned.
95      *
96      * @return the file name
97      */

98     public String JavaDoc toString() {
99         if (first == null) {
100             return "UnknownFileName.java";
101         } else {
102             return first.toString() + ".java";
103         }
104     }
105
106     /**
107      * Adds a comment to this file.
108      *
109      * @param comment the new file comment
110      */

111     public void addComment(JavaComment comment) {
112         addElement(comment);
113     }
114
115     /**
116      * Adds an import to the file.
117      *
118      * @param imp the import to add
119      */

120     public void addImport(JavaImport imp) {
121         addElement(imp);
122     }
123
124     /**
125      * Adds a class to the file.
126      *
127      * @param cls the class to add
128      */

129     public void addClass(JavaClass cls) {
130         if (first == null) {
131             first = cls;
132         }
133         addElement(cls);
134     }
135
136     /**
137      * Adds an interface to the file.
138      *
139      * @param ifc the interface to add
140      */

141     public void addInterface(JavaInterface ifc) {
142         if (first == null) {
143             first = ifc;
144         }
145         addElement(ifc);
146     }
147
148     /**
149      * Returns a numeric category number for the code element. A lower
150      * category number implies that the code element should be placed
151      * before code elements with a higher category number within a
152      * declaration.
153      *
154      * @return the category number
155      */

156     public int category() {
157         return 0;
158     }
159
160     /**
161      * Writes the source code for this file. Any previous file with
162      * this name will be overwritten.
163      *
164      * @param style the code style to use
165      *
166      * @throws IOException if the file could not be written properly
167      */

168     public void writeCode(CodeStyle style) throws IOException JavaDoc {
169         File JavaDoc file = new File JavaDoc(dir, toString());
170         PrintWriter JavaDoc out;
171
172         createFile(file);
173         out = new PrintWriter JavaDoc(new FileWriter JavaDoc(file));
174         print(out, style, 0);
175         out.close();
176     }
177
178     /**
179      * Prints the file contents to the specified output stream.
180      *
181      * @param out the output stream
182      * @param style the code style to use
183      * @param indent the indentation level
184      */

185     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
186         printContents(out, style, indent);
187     }
188 }
189
Popular Tags