KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > csharp > CSharpFile


1 /*
2  * CSharpFile.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.csharp;
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.CodeElementContainer;
42 import net.percederberg.grammatica.code.CodeStyle;
43
44 /**
45  * A class generating a C# source code file.
46  *
47  * @author Per Cederberg, <per at percederberg dot net>
48  * @version 1.5
49  */

50 public class CSharpFile extends CodeElementContainer {
51
52     /**
53      * The file to write to.
54      */

55     private File JavaDoc file;
56
57     /**
58      * Creates a new C# source code file.
59      *
60      * @param basedir the base output directory
61      * @param basename the base file name (without extension)
62      */

63     public CSharpFile(File JavaDoc basedir, String JavaDoc basename) {
64         this.file = new File JavaDoc(basedir, basename + ".cs");
65     }
66
67     /**
68      * Returns the file name.
69      *
70      * @return the file name.
71      */

72     public String JavaDoc toString() {
73         return file.getName();
74     }
75
76     /**
77      * Returns a numeric category number for the code element. A lower
78      * category number implies that the code element should be placed
79      * before code elements with a higher category number within a
80      * declaration.
81      *
82      * @return the category number
83      */

84     public int category() {
85         return 0;
86     }
87
88     /**
89      * Adds a file comment.
90      *
91      * @param comment the file comment to add
92      */

93     public void addComment(CSharpComment comment) {
94         addElement(comment);
95     }
96
97     /**
98      * Adds a using declaration to the file.
99      *
100      * @param u the using declaration to add
101      */

102     public void addUsing(CSharpUsing u) {
103         addElement(u);
104     }
105
106     /**
107      * Adds a namespace declaration to the file.
108      *
109      * @param n the namespace declaration to add
110      */

111     public void addNamespace(CSharpNamespace n) {
112         addElement(n);
113     }
114
115     /**
116      * Adds a class declaration to the file.
117      *
118      * @param c the class declaration to add
119      */

120     public void addClass(CSharpClass c) {
121         addElement(c);
122     }
123
124     /**
125      * Adds an enumeration declaration to the file.
126      *
127      * @param e the enumeration to add
128      */

129     public void addEnumeration(CSharpEnumeration e) {
130         addElement(e);
131     }
132
133     /**
134      * Writes the source code for this file. Any previous file with
135      * this name will be overwritten.
136      *
137      * @param style the code style to use
138      *
139      * @throws IOException if the file could not be written properly
140      */

141     public void writeCode(CodeStyle style) throws IOException JavaDoc {
142         PrintWriter JavaDoc out;
143
144         createFile(file);
145         out = new PrintWriter JavaDoc(new FileWriter JavaDoc(file));
146         print(out, style, 0);
147         out.close();
148     }
149
150     /**
151      * Prints the file contents to the specified output stream.
152      *
153      * @param out the output stream
154      * @param style the code style to use
155      * @param indent the indentation level
156      */

157     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
158         printContents(out, style, indent);
159     }
160 }
161
Popular Tags