1 23 24 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 49 public class IOJavaFileWriter implements JavaFileWriter 50 { 51 52 private static final ResourceBundle _messages = 53 I18NHelper.loadBundle(IOJavaFileWriter.class); 54 55 private File _file; 56 private String _packageBlock; 57 private List _importStatements = new ArrayList(); 58 private List _classes = new ArrayList(); 59 60 63 public IOJavaFileWriter (File file) 64 { 65 _file = file; 66 } 67 68 70 protected static final ResourceBundle getMessages () { return _messages; } 71 72 80 public void setPackage (final String packageName, final String [] 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 + ';'); writerHelper.writeln(); 89 } 90 91 _packageBlock = writerHelper.toString(); 92 } 93 94 102 public void addImport (final String importName, final String [] 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 + ';'); 110 _importStatements.add(writerHelper.toString()); 111 } 112 113 116 public void addClass (final JavaClassWriter classWriter) 117 { 118 if (classWriter != null) 119 _classes.add(classWriter); 120 } 121 122 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", 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 160 public String toString () 161 { 162 final FormattedWriter writerHelper = new FormattedWriter(); 163 164 writerHelper.writeln(); 166 if (_packageBlock != null) 167 writerHelper.write(_packageBlock); 168 169 writerHelper.writeList(_importStatements); writerHelper.writeList(_classes); 172 return writerHelper.toString(); 173 } 174 } 175 | Popular Tags |