KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > generator > io > IOJavaFileWriter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * IOJavaFileWriter.java
26  *
27  * Created on November 14, 2001, 5:19 PM
28  */

29
30 package com.sun.jdo.spi.persistence.utility.generator.io;
31
32 import java.io.*;
33 import java.util.*;
34
35 import com.sun.jdo.spi.persistence.utility.I18NHelper;
36 import com.sun.jdo.spi.persistence.utility.generator.JavaFileWriter;
37 import com.sun.jdo.spi.persistence.utility.generator.JavaClassWriter;
38
39 /**
40  * This implementation of the {@link JavaFileWriter} interface is based on
41  * {@link java.io.File} and simple {@link java.lang.StringBuffer} "println"
42  * type statements.
43  *<p>
44  * Use this interface in conjunction with one or more {@link JavaClassWriter}
45  * instances to describe the class(es) in a java file.
46  *
47  * @author raccah
48  */

49 public class IOJavaFileWriter implements JavaFileWriter
50 {
51     /** I18N message handler */
52     private static final ResourceBundle _messages =
53         I18NHelper.loadBundle(IOJavaFileWriter.class);
54
55     private File _file;
56     private String JavaDoc _packageBlock;
57     private List _importStatements = new ArrayList();
58     private List _classes = new ArrayList();
59
60     /** Creates a new instance of IOJavaFileWriter.
61      * @param file The file object which will be used at save time.
62      */

63     public IOJavaFileWriter (File file)
64     {
65         _file = file;
66     }
67     
68     /** @return I18N message handler for this element
69      */

70     protected static final ResourceBundle getMessages () { return _messages; }
71
72     /** Sets the package for this file. Note that the package name format
73      * must be package style (that is - it can contain . but not / or $).
74      * @param packageName The name of the package for this source file.
75      * @param comments The comments shown just above the package statement.
76      * The comments are passed as an array so the line separators can be added
77      * by the implementation. Note that not all implementations will choose
78      * to make use of this comment.
79      */

80     public void setPackage (final String JavaDoc packageName, final String JavaDoc[] comments)
81     {
82         final FormattedWriter writerHelper = new FormattedWriter();
83
84         writerHelper.writeComments(comments);
85         if (packageName != null && packageName.length() > 0)
86         {
87             writerHelper.writeln("package " + packageName + ';'); // NOI18N
88
writerHelper.writeln();
89         }
90
91         _packageBlock = writerHelper.toString();
92     }
93
94     /** Adds an import statement for this source file.
95      * @param importName Name of the class or package (including the *) to be
96      * imported. This string should not contain "import" or the ;
97      * @param comments The comments shown just above the import statement.
98      * The comments are passed as an array so the line separators can be added
99      * by the implementation. Note that not all implementations will choose
100      * to make use of this comment.
101      */

102     public void addImport (final String JavaDoc importName, final String JavaDoc[] comments)
103     {
104         final FormattedWriter writerHelper = new FormattedWriter();
105
106         writerHelper.writeComments(comments);
107         if (importName != null && importName.length() > 0)
108             writerHelper.writeln("import " + importName + ';'); // NOI18N
109

110         _importStatements.add(writerHelper.toString());
111     }
112
113     /** Adds a class to this source file.
114      * @param classWriter The definition of the class.
115      */

116     public void addClass (final JavaClassWriter classWriter)
117     {
118         if (classWriter != null)
119             _classes.add(classWriter);
120     }
121
122     /** Saves the file by writing out the source contents to whatever
123      * file (or alternate representation) was specified (usually by the
124      * constructor of the implementation class.
125      * @throws IOException If the file cannot be saved.
126      */

127     public void save () throws IOException
128     {
129         if (_file != null)
130         {
131             final File directory = _file.getParentFile();
132             final FileWriter fileWriter;
133
134             if (directory != null)
135             {
136                 if (!directory.exists() && !directory.mkdirs())
137                 {
138                     throw new IOException(I18NHelper.getMessage(getMessages(),
139                         "utility.unable_create_destination_directory", // NOI18N
140
directory.getPath()));
141                 }
142             }
143             
144             fileWriter = new FileWriter(_file);
145
146             try
147             {
148                 fileWriter.write(toString());
149             }
150             finally
151             {
152                 fileWriter.close();
153             }
154         }
155     }
156
157     /** Returns a string representation of this object.
158      * @return The string representation of the generated file.
159      */

160     public String JavaDoc toString ()
161     {
162         final FormattedWriter writerHelper = new FormattedWriter();
163
164         // package block
165
writerHelper.writeln();
166         if (_packageBlock != null)
167             writerHelper.write(_packageBlock);
168
169         writerHelper.writeList(_importStatements); // imports
170
writerHelper.writeList(_classes); // classes
171

172         return writerHelper.toString();
173     }
174 }
175
Popular Tags