FileDocCategorySizeDatePackage
IOJavaFileWriter.javaAPI DocGlassfish v2 API6465Fri May 04 22:35:24 BST 2007com.sun.jdo.spi.persistence.utility.generator.io

IOJavaFileWriter

public class IOJavaFileWriter extends Object implements com.sun.jdo.spi.persistence.utility.generator.JavaFileWriter
This implementation of the {@link JavaFileWriter} interface is based on {@link java.io.File} and simple {@link java.lang.StringBuffer} "println" type statements.

Use this interface in conjunction with one or more {@link JavaClassWriter} instances to describe the class(es) in a java file.

author
raccah

Fields Summary
private static final ResourceBundle
_messages
I18N message handler
private File
_file
private String
_packageBlock
private List
_importStatements
private List
_classes
Constructors Summary
public IOJavaFileWriter(File file)
Creates a new instance of IOJavaFileWriter.

param
file The file object which will be used at save time.


	                   	 
	   
	
		_file = file;
	
Methods Summary
public voidaddClass(com.sun.jdo.spi.persistence.utility.generator.JavaClassWriter classWriter)
Adds a class to this source file.

param
classWriter The definition of the class.

		if (classWriter != null)
			_classes.add(classWriter);
	
public voidaddImport(java.lang.String importName, java.lang.String[] comments)
Adds an import statement for this source file.

param
importName Name of the class or package (including the *) to be imported. This string should not contain "import" or the ;
param
comments The comments shown just above the import statement. The comments are passed as an array so the line separators can be added by the implementation. Note that not all implementations will choose to make use of this comment.

		final FormattedWriter writerHelper =  new FormattedWriter();

		writerHelper.writeComments(comments);
		if (importName != null && importName.length() > 0)
			writerHelper.writeln("import " + importName + ';");		// NOI18N

		_importStatements.add(writerHelper.toString());
	
protected static final java.util.ResourceBundlegetMessages()

return
I18N message handler for this element

 return _messages; 
public voidsave()
Saves the file by writing out the source contents to whatever file (or alternate representation) was specified (usually by the constructor of the implementation class.

throws
IOException If the file cannot be saved.

		if (_file != null)
		{
			final File directory = _file.getParentFile();
			final FileWriter fileWriter;

			if (directory != null)
			{
				if (!directory.exists() && !directory.mkdirs())
				{
					throw new IOException(I18NHelper.getMessage(getMessages(), 
						"utility.unable_create_destination_directory",	// NOI18N
						directory.getPath()));
				}
			}
			
			fileWriter = new FileWriter(_file);

			try
			{
				fileWriter.write(toString());
			}
			finally
			{
				fileWriter.close();
			}
		}
	
public voidsetPackage(java.lang.String packageName, java.lang.String[] comments)
Sets the package for this file. Note that the package name format must be package style (that is - it can contain . but not / or $).

param
packageName The name of the package for this source file.
param
comments The comments shown just above the package statement. The comments are passed as an array so the line separators can be added by the implementation. Note that not all implementations will choose to make use of this comment.

		final FormattedWriter writerHelper =  new FormattedWriter();

		writerHelper.writeComments(comments);
		if (packageName != null  &&  packageName.length() > 0)
		{
			writerHelper.writeln("package " + packageName + ';");	// NOI18N
			writerHelper.writeln();
		}

		_packageBlock = writerHelper.toString();
	
public java.lang.StringtoString()
Returns a string representation of this object.

return
The string representation of the generated file.

		final FormattedWriter writerHelper =  new FormattedWriter();

		// package block
		writerHelper.writeln();
		if (_packageBlock != null)
			writerHelper.write(_packageBlock);

		writerHelper.writeList(_importStatements);		// imports
		writerHelper.writeList(_classes);				// classes

		return writerHelper.toString();