com.myjavatools.lib
Class FormattedWriter

java.lang.Object
  extended by com.myjavatools.lib.FormattedWriter

public class FormattedWriter
extends java.lang.Object


Constructor Summary
FormattedWriter(java.io.OutputStream os, java.lang.String format)
          Creates a writer that formats its output according to the format specified
FormattedWriter(java.io.OutputStream os, java.lang.String head, java.lang.String body, java.lang.String tail)
          Creates a writer that formats its output according to head, body and tail formats specified
FormattedWriter(java.io.Writer writer, java.lang.String format)
          Creates a writer that formats its output according to the format specified
FormattedWriter(java.io.Writer writer, java.lang.String head, java.lang.String body, java.lang.String tail)
          Creates a writer that formats its output according to head, body and tail formats specified
 
Method Summary
 void close()
          Writes the tail to formatted output and closes it
 void close(java.lang.Object[] args)
          Writes the tail to formatted output and closes it
 java.text.MessageFormat getBody()
          Gets body format string
 java.text.MessageFormat getHead()
          Gets head format string
 java.text.MessageFormat getTail()
          gets tail format string
 void open()
          Opens formatted output, no arguments for head format
 void open(java.lang.Object arg)
          Opens formatted output
 void open(java.lang.Object[] args)
          Opens formatted output
 void write(java.lang.Object[] args)
          Writes a body string to formatted output
 void write(java.lang.String s)
          Writes an arbitrary string to (generally speaking) formatted output
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FormattedWriter

public FormattedWriter(java.io.Writer writer,
                       java.lang.String format)
Creates a writer that formats its output according to the format specified

Parameters:
writer - the writer that outputs the formatted contents
format - head format string

Example
String format = "Copyright (c) {0} {1}\r\n\r\n" +
        "{2}\r\n";

FileFormat ff = new FileFormat(new FileWriter("readme.txt"), format);
ff.write("George Rasputin", "" + new Date().getYear(), "This is a stub for future readme text");

This code will produce something like this:
Copyright (c) George Rasputin 2004

This is a stub for future readme text

See Also:
FormattedWriter(Writer,String,String,String)

FormattedWriter

public FormattedWriter(java.io.Writer writer,
                       java.lang.String head,
                       java.lang.String body,
                       java.lang.String tail)
Creates a writer that formats its output according to head, body and tail formats specified

Parameters:
writer - the writer that outputs the formatted contents
head - format string for the file head
body - format string for (repeating) entries of the file body
tail - format strings for the file tail

Example
String head = "/*\r\n Resource Data for package {0} in project {1}\r\n/*\r\n" +
        "class ResourceData {\r\n";
String body = " public String {0} = \"{1}\";\r\n";
String tail = "}\r\n";

FileFormat ff = new FileFormat(new FileWriter("resource.java", head, body, tail);
ff.open(packageName, "My Cool Project");
Properties p = ...;
for (Enumeration i = p.keys(); i.hasMoreElements();) {
  String key = i.nextElement().toString();
  ff.write(new String[] {key, p.getProperty(key, "undef"});
}
ff.close();

This code will produce something like this:
/*
Resource Data for package com.my.package in project My Cool Project
*/
class ResourceData {
  String dialogTitle = "Romeo vs Juliet";
  String label1 = "One of unmentionable major copyrighted labels";
  String size = "100x200";
}


FormattedWriter

public FormattedWriter(java.io.OutputStream os,
                       java.lang.String format)
Creates a writer that formats its output according to the format specified

Parameters:
os - output stream that outputs the formatted contents
format - head format string
See Also:
FormattedWriter(Writer,String)

FormattedWriter

public FormattedWriter(java.io.OutputStream os,
                       java.lang.String head,
                       java.lang.String body,
                       java.lang.String tail)
Creates a writer that formats its output according to head, body and tail formats specified

Parameters:
os - output stream that outputs the formatted contents
head - format string for the file head
body - format string for (repeating) entries of the file body
tail - format strings for the file tail
See Also:
FormattedWriter(Writer,String,String,String)
Method Detail

getHead

public java.text.MessageFormat getHead()
Gets head format string

Returns:
head format string

getBody

public java.text.MessageFormat getBody()
Gets body format string

Returns:
body format string

getTail

public java.text.MessageFormat getTail()
gets tail format string

Returns:
tail format string

open

public void open(java.lang.Object[] args)
          throws java.io.IOException
Opens formatted output

Parameters:
args - arguments for the head format
Throws:
java.io.IOException - if write operation failes

open

public void open(java.lang.Object arg)
          throws java.io.IOException
Opens formatted output

Parameters:
arg - the only argiment for the head format
Throws:
java.io.IOException - if write operation fails

open

public void open()
          throws java.io.IOException
Opens formatted output, no arguments for head format

Throws:
java.io.IOException

write

public void write(java.lang.Object[] args)
           throws java.io.IOException
Writes a body string to formatted output

Parameters:
args - arguments for body format
Throws:
java.io.IOException - if write operation fails Note. If body format is missing (then there is only head format), then head format is applied to the arguments, the result of formatting is written to the output, and the writer is closed.

write

public void write(java.lang.String s)
           throws java.io.IOException
Writes an arbitrary string to (generally speaking) formatted output

Parameters:
s - the string to output
Throws:
java.io.IOException - if write operation fails This skips any formatting; the string is just sent to output "as is".

close

public void close(java.lang.Object[] args)
           throws java.io.IOException
Writes the tail to formatted output and closes it

Parameters:
args - arguments for tail format
Throws:
java.io.IOException - if write operation fails Note. if there is no tail format, the output just closes.

close

public void close()
           throws java.io.IOException
Writes the tail to formatted output and closes it

Throws:
java.io.IOException - if write operation fails