home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-06-23 | 4.1 KB | 178 lines |
- package com.ibm.texml;
-
- import java.io.Writer;
- import java.io.IOException;
-
- public class TextProc
- {
- private Writer out;
- private int width; // width of page
- private int curIndent; // current indentation
- private int incrIndent; // increment of indentation
- private int maxIndent; // maximum indentation
- private char[] buffer; // buffer of text
- private int nextChar; // position of next empty character in buffer.
- private int lastWord; // start position of last word in buffer.
- private boolean inStart; // supress leading white space
-
- public TextProc (Writer ostream)
- {
- out = ostream;
- width = 75;
- incrIndent = 4;
- // -incrIndent allows first startSection() to cause zero indent
- curIndent = -incrIndent;
- //curIndent = 0;
- maxIndent = width / 2;
- buffer = new char[width+1];
- nextChar = 0;
- lastWord = 0;
- inStart = true;
- }
-
- private void flushBuffer()
- {
- if (!inStart)
- {
- try
- {
- out.write(buffer, 0, nextChar);
- out.write('\n');
- }
- catch (Exception e)
- {
- System.err.println("Failed to write buffer.");
- e.printStackTrace(System.err);
- }
- nextChar = lastWord = 0;
- }
- }
-
- private void applyIndent()
- {
- for (int i = 0; i < curIndent; ++i)
- {
- buffer[i] = ' ';
- }
- }
-
- /**
- Start a new section of text.
- Increases indentation.
- */
- public void startSection()
- {
- //System.err.println("start of line");
- //flushBuffer();
- curIndent += incrIndent;
- if (curIndent > maxIndent)
- {
- curIndent -= incrIndent;
- }
- if (inStart)
- {
- applyIndent();
- lastWord = nextChar = curIndent;
- }
- }
-
- /**
- Output an empty line
- */
- public void appendLine()
- {
- //System.err.println("empty line");
- flushBuffer();
- applyIndent();
- try
- {
- out.write('\n');
- }
- catch (Exception e)
- {
- System.err.println("failed to write new line");
- e.printStackTrace(System.err);
- }
- lastWord = nextChar = curIndent;
- inStart = true;
- }
-
- /**
- Append words to the output.
- This assumes only whole words are in the text string,
- i.e. there is an implied space between calls to appendText()
- */
- public void appendText(String text)
- {
- boolean sawWhite = true;
- char[] curText = text.toCharArray();
- int curChar = 0;
-
- //System.err.println("text:"+text);
- lastWord = nextChar;
- while (curChar < curText.length)
- {
- // fill the buffer
- while (curChar < curText.length && nextChar < width)
- {
- char c = curText[curChar];
- if (Character.isWhitespace(c))
- {
- c = ' ';
- sawWhite = true;
- }
- else
- {
- if (sawWhite)
- {
- lastWord = nextChar;
- sawWhite = false;
- }
- }
- if (!inStart || !sawWhite)
- {
- buffer[nextChar] = c;
- inStart = false;
- ++nextChar;
- }
- ++curChar;
- }
- if (nextChar == width)
- {
- // output the buffer
- try
- {
- out.write(buffer, 0, lastWord - 1);
- out.write('\n');
- }
- catch (Exception e)
- {
- System.err.println("Failed to write buffer");
- e.printStackTrace(System.err);
- }
- System.arraycopy(buffer, lastWord,
- buffer, curIndent, nextChar - lastWord);
- applyIndent();
- nextChar = curIndent + nextChar - lastWord;
- inStart = nextChar == curIndent;
- }
- }
- }
-
- /**
- Finish a paragraph.
- Output all words held in buffer.
- Output a new line.
- Decrease indentation.
- */
- public void endSection()
- {
- //System.err.println("end of line");
- curIndent -= incrIndent;
- flushBuffer();
- applyIndent();
- lastWord = nextChar = curIndent;
- inStart = true;
- }
- }
-