// Uncolored, plain source file:  setbuf.java
// <p> // An example file explaining how to use // com.stevesoft.pat, com.stevesoft.pat.wrap, // and com.stevesoft.pat.apps // <p> // This software comes without express or implied warranty. // No claim is made about the suitability of this software for // any purpose and neither we nor SteveSoft shall be liable for // damages suffered by the user of this software. import com.stevesoft.pat.*; import com.stevesoft.pat.wrap.*; import java.io.*;
/* By default Regex searches strings. When it does a text substitution it puts the result in a StringBuffer, appending different bits of data as it goes. However, it doesn't have to do this. It asks the StringLike object that it is searching for a new BasicStringBuffer class. By default, package com.stevesoft.pat.wrap has three: 1) CharArrayBufferWrap -- the data is written to a char[] Object. 2) WriterWrap -- the output is written to a Writer object. 3) StringBufferWrap -- the data is written to a StringBuffer Object. */
public class setbuf { public static void main(String[] args) throws Exception { // Create the buffer we want to use to capture the output. Writer out = new OutputStreamWriter(System.out); final WriterWrap ww = new WriterWrap(out); // Make a StringLike that returns it from its // newStringBufferLike() method. String s = "this String has a foo in it."; StringLike sk = new StringWrap(s) { public BasicStringBufferLike newStringBufferLike() { return ww; } };
// Compile a regular expression. Regex r = new Regex("foo","bar"); // Do the replacement. r.replaceAll(sk); // Print the result. out.flush(); } }