// Uncolored, plain source file:  WantMore.java
// <p> // An example file distributed with com.stevesoft.pat // 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 java.io.*;
public class WantMore { public static void main(String[] unused) throws Exception { // Some lines of text to process.... String[] s = new String[]{ "Some uncommented stuff /* and some commented\r\n", "stuff.\r\n", "Some short\r\n", "and wasteful\r\n", "lines. */ Yet more uncommented stuff.\r\n", "And even a bit more.\r\n" };
Transformer t = new Transformer(true); // We want to replace comments with just /* comment */ t.add("s'/\\*[\\d\\D]*\\*/'/* comment */'"); // If the above complete rule failed, but "/*" matches, then we know // that we saw the beginning of a comment and must wait // for more text to be read in before the match can succeed. // So we use WANT_MORE_TEXT to tell RegexWriter to wait. t.add("s'/\\*'$WANT_MORE_TEXT'"); // Just some inane rule to apply outside of comments for // the sake of an example. Surrounds all words with angle // brackets. t.add("s/\\w+/<$&>/");
// Let RegexWriter do its stuff, getting help from WANT_MORE_TEXT. OutputStreamWriter osw = new OutputStreamWriter(System.out); RegexWriter rw = new RegexWriter(t,osw); for(int i=0;i<s.length;i++) rw.write(s[i],0,s[i].length()); rw.flush(); } }