KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > generator > JavaWriter


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.generator;
19
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.OutputStreamWriter JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25
26 public class JavaWriter extends CodeWriter {
27     private GenOptions genOptions;
28     private PrintWriter JavaDoc pw;
29     private boolean needIndent = true;
30     private int indentPos = 0;
31     private String JavaDoc indentStr = "";
32     private String JavaDoc spaces = " ";
33
34     public JavaWriter(GenOptions genOptions, String JavaDoc fileName, String JavaDoc ext) {
35         super(genOptions, fileName, ext);
36     }
37
38     protected File JavaDoc getFile()
39             throws GenException {
40         File JavaDoc file = null;
41         GenOptions go = getGenOptions();
42         String JavaDoc fileName = getFileName() + getFileExt();
43
44         try {
45             file = new File JavaDoc(go.getGenSrcDir(), fileName);
46
47             if (file.exists() && !go.isOverwrite()) {
48                 fileName = fileName + ".new";
49
50                 file = new File JavaDoc(go.getGenSrcDir(), fileName);
51             }
52         } catch (Exception JavaDoc ex) {
53             throw new GenException("Error: Unable to open output dir: " + go.getGenSrcDir() + ", file: " + fileName, ex);
54         }
55
56         return file;
57     }
58
59     public void openFile()
60             throws GenException {
61         OutputStream JavaDoc os = null;
62
63         if (file != null) {
64             //System.out.println( "Output file already opened" );
65
return;
66         }
67
68         file = getFile();
69
70         if (file == null) {
71             throw new GenException("Error: Unable to obtain output file.");
72         }
73
74         if (getGenOptions().isVerbose()) {
75             System.out.println("Generating: " + file);
76         }
77
78         os = null;
79
80         //if (_file.isFile())
81
//{
82
file.getParentFile().mkdirs();
83         //}
84

85         if (file.exists() && !file.canWrite()) {
86             throw new GenException("Error: Unable to write to file: " + file);
87         }
88
89         if (!file.exists() && !file.getParentFile().canWrite()) {
90             throw new GenException("Error: Unable to write to directory: " + file.getParentFile());
91         }
92
93         try {
94             os = new FileOutputStream JavaDoc(file);
95         } catch (Exception JavaDoc ex) {
96             throw new GenException("Error: Unable to init output file: " + file, ex);
97         }
98
99         try {
100             pw = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(os));
101         } catch (Exception JavaDoc ex) {
102             throw new GenException("Error: Unable to init output file: " + file, ex);
103         }
104     }
105
106     public void closeFile()
107             throws GenException {
108         if (pw != null) {
109             try {
110                 pw.flush();
111                 pw.close();
112             } catch (Exception JavaDoc e) {
113                 throw new GenException("Error: Unable to close output file: " + file, e);
114             }
115
116             pw = null;
117         }
118
119         file = null;
120     }
121
122     public void indent() {
123         indentPos += 4;
124         if (indentPos > spaces.length()) {
125             indentPos -= 4;
126         }
127         indentStr = spaces.substring(0, indentPos);
128     }
129
130     public void outdent() {
131         indentPos -= 4;
132         if (indentPos < 0) {
133             indentPos = 0;
134         }
135         indentStr = spaces.substring(0, indentPos);
136     }
137
138     public void begin() {
139         needIndent = true;
140         println("{");
141         indent();
142     }
143
144     public void end() {
145         outdent();
146         needIndent = true;
147         println("}");
148     }
149
150     public void newln() {
151         println("");
152         needIndent = true;
153     }
154
155     public void comment(String JavaDoc msg) {
156         println("// " + msg);
157     }
158
159     public void println(String JavaDoc line) {
160         if (needIndent) {
161             needIndent = false;
162             pw.print(indentStr);
163         }
164
165         pw.println(line);
166         needIndent = true;
167     }
168
169     public void print(String JavaDoc line) {
170         if (needIndent) {
171             needIndent = false;
172             pw.print(indentStr);
173         }
174
175         pw.print(line);
176     }
177 }
178
Popular Tags