home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 October / pcp156b.iso / alphawrk / TEXML / TEXML.ZIP / com / ibm / texml / TextProc.java < prev    next >
Encoding:
Java Source  |  1999-06-23  |  4.1 KB  |  178 lines

  1. package com.ibm.texml;
  2.  
  3. import java.io.Writer;
  4. import java.io.IOException;
  5.  
  6. public class TextProc
  7.   {
  8.   private Writer out;
  9.   private int width;      // width of page
  10.   private int curIndent;  // current indentation
  11.   private int incrIndent; // increment of indentation
  12.   private int maxIndent;  // maximum indentation
  13.   private char[] buffer;  // buffer of text
  14.   private int nextChar;   // position of next empty character in buffer.
  15.   private int lastWord;   // start position of last word in buffer.
  16.   private boolean inStart; // supress leading white space
  17.  
  18.   public TextProc (Writer ostream)
  19.     {
  20.     out = ostream;
  21.     width = 75;
  22.     incrIndent = 4;
  23.     // -incrIndent allows first startSection() to cause zero indent
  24.     curIndent = -incrIndent;
  25.     //curIndent = 0;
  26.     maxIndent = width / 2;
  27.     buffer = new char[width+1];
  28.     nextChar = 0;
  29.     lastWord = 0;
  30.     inStart = true;
  31.     }
  32.  
  33.   private void flushBuffer()
  34.     {
  35.     if (!inStart)
  36.       {
  37.       try
  38.         {
  39.         out.write(buffer, 0, nextChar);
  40.         out.write('\n');
  41.         }
  42.       catch (Exception e)
  43.         {
  44.         System.err.println("Failed to write buffer.");
  45.         e.printStackTrace(System.err);
  46.         }
  47.       nextChar = lastWord = 0;
  48.       }
  49.     }
  50.  
  51.   private void applyIndent()
  52.     {
  53.     for (int i = 0; i < curIndent; ++i)
  54.       {
  55.       buffer[i] = ' ';
  56.       }
  57.     }
  58.  
  59.   /**
  60.   Start a new section of text.
  61.   Increases indentation.
  62.   */
  63.   public void startSection()
  64.     {
  65.     //System.err.println("start of line");
  66.     //flushBuffer();
  67.     curIndent += incrIndent;
  68.     if (curIndent > maxIndent)
  69.       {
  70.       curIndent -= incrIndent;
  71.       }
  72.     if (inStart)
  73.       {
  74.       applyIndent();
  75.       lastWord = nextChar = curIndent;
  76.       }
  77.     }
  78.  
  79.   /**
  80.   Output an empty line
  81.   */
  82.   public void appendLine()
  83.     {
  84.     //System.err.println("empty line");
  85.     flushBuffer();
  86.     applyIndent();
  87.     try
  88.       {
  89.       out.write('\n');
  90.       }
  91.     catch (Exception e)
  92.       {
  93.       System.err.println("failed to write new line");
  94.       e.printStackTrace(System.err);
  95.       }
  96.     lastWord = nextChar = curIndent;
  97.     inStart = true;
  98.     }
  99.  
  100.   /**
  101.   Append words to the output.
  102.   This assumes only whole words are in the text string,
  103.   i.e. there is an implied space between calls to appendText()
  104.   */
  105.   public void appendText(String text)
  106.     {
  107.     boolean sawWhite = true;
  108.     char[] curText = text.toCharArray();
  109.     int curChar = 0;
  110.  
  111.     //System.err.println("text:"+text);
  112.     lastWord = nextChar;
  113.     while (curChar < curText.length)
  114.       {
  115.       // fill the buffer
  116.       while (curChar < curText.length && nextChar < width)
  117.         {
  118.         char c = curText[curChar];
  119.         if (Character.isWhitespace(c))
  120.           {
  121.           c = ' ';
  122.           sawWhite = true;
  123.           }
  124.         else
  125.           {
  126.           if (sawWhite)
  127.             {
  128.             lastWord = nextChar;
  129.             sawWhite = false;
  130.             }
  131.           }
  132.         if (!inStart || !sawWhite)
  133.           {
  134.           buffer[nextChar] = c;
  135.           inStart = false;
  136.           ++nextChar;
  137.           }
  138.         ++curChar;
  139.         }
  140.       if (nextChar == width)
  141.         {
  142.         // output the buffer
  143.         try
  144.           {
  145.           out.write(buffer, 0, lastWord - 1);
  146.           out.write('\n');
  147.           }
  148.         catch (Exception e)
  149.           {
  150.           System.err.println("Failed to write buffer");
  151.           e.printStackTrace(System.err);
  152.           }
  153.         System.arraycopy(buffer, lastWord,
  154.                          buffer, curIndent, nextChar - lastWord);
  155.         applyIndent();
  156.         nextChar = curIndent + nextChar - lastWord;
  157.         inStart = nextChar == curIndent;
  158.         }
  159.       }
  160.     }
  161.  
  162.   /**
  163.   Finish a paragraph.
  164.   Output all words held in buffer.
  165.   Output a new line.
  166.   Decrease indentation.
  167.   */
  168.   public void endSection()
  169.     {
  170.     //System.err.println("end of line");
  171.     curIndent -= incrIndent;
  172.     flushBuffer();
  173.     applyIndent();
  174.     lastWord = nextChar = curIndent;
  175.     inStart = true;
  176.     }
  177.   }
  178.