Java Program: A Simple Text Formatter

 Java Program: A Simple Text Formatter


This program is a very primitive text formatter, representative of what people used on most computing platforms before the rise of standalone graphics-based word processors, laser printers, and, eventually, desktop publishing, word processors, and desktop office suites. It simply reads words from a file—previously created with a text editor—and outputs them until it reaches the right margin, when it calls println( ) to append a line ending. For example, here is an input file:

It's a nice
day, isn't it, Mr. Mxyzzptllxy?
I think we should
go for a walk.

Given the above as its input, the Fmt program prints the lines formatted neatly:

It's a nice day, isn't it, Mr. Mxyzzptllxy? I think we should go for a
walk.

As you can see, it fits the text we gave it to the margin and discards all the line breaks present in the original. Here’s the code:

import java.io.*;
import java.util.*;
/**
 * Fmt - format text (like Berkeley Unix fmt).
 */
public class Fmt {
 /** The maximum column width */
 public static final int COLWIDTH=72;
 /** The file that we read and format */
 BufferedReader in;
 /** If files present, format each, else format the standard input. */
 public static void main(String[] av) throws IOException {
 if (av.length == 0)
 new Fmt(System.in).format( );
 else for (int i=0; i<av.length; i++)
 new Fmt(av[i]).format( );
 }
 /** Construct a Formatter given a filename */
 public Fmt(String fname) throws IOException {
 in = new BufferedReader(new FileReader(fname));
 }
 /** Construct a Formatter given an open Stream */
 public Fmt(InputStream file) throws IOException {
 in = new BufferedReader(new InputStreamReader(file));
 }
 /** Format the File contained in a constructed Fmt object */
 public void format( ) throws IOException {
 String w, f;
 int col = 0;
 while ((w = in.readLine( )) != null) {
 if (w.length( ) == 0) { // null line
 System.out.print("\n"); // end current line
 if (col>0) {
 System.out.print("\n"); // output blank line
 col = 0;
 }
 continue;
 }
// otherwise it's text, so format it.
 StringTokenizer st = new StringTokenizer(w);
 while (st.hasMoreTokens( )) {
 f = st.nextToken( );
 if (col + f.length( ) > COLWIDTH) {
 System.out.print("\n");
 col = 0;
 }
 System.out.print(f + " ");
 col += f.length( ) + 1;
 }
 }
 if (col>0) System.out.print("\n");
 in.close( );
 }
}

A slightly fancier version of this program, Fmt2, is in the online source for this book. It uses “dot commands”—lines beginning with periods—to give limited control over the formatting. A family of “dot command” formatters includes Unix’s roff, nroff, troff, and groff, which are in the same family with programs called runoff on Digital Equipment systems. The original for this is J. Saltzer’s runoff, which first appeared on Multics and from there made its way into various OSes. To save trees, I did not include Fmt2 here; it subclasses Fmt and overrides the format( ) method to include additional functionality.

0 comments:

Post a Comment