view src/kryshen/tema/io/ReplaceWriter.java @ 2:6c41a0b43e58 release_0_3

Tema 0.3 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 19 Feb 2008 20:32:17 +0300
parents
children e9d13c7ffeb1
line wrap: on
line source

/*
 *  Copyright 2006-2008 Mikhail Kryshen
 *
 *  This file is part of Tema.
 *
 *  Tema is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  Tema is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the 
 *  GNU Lesser General Public License along with Tema.  
 *  If not, see <http://www.gnu.org/licenses/>.
 *
 *  $Id: ReplaceWriter.java,v 1.8 2008/02/19 16:21:00 mikhail Exp $
 */

package kryshen.tema.io;

import java.io.Writer;
import java.io.FilterWriter;
import java.io.IOException;

/**
 * FilterWriter which replaces characters with escape strings.
 *
 * @author Mikhail Kryshen
 */
public class ReplaceWriter extends FilterWriter {
    private String[] patterns;
    private String[] replaces;

    private String prefix;
    private String postfix;

    private int maxPatternLength = 0;

    /** Have this Writer processed any data? */
    private boolean processedData = false;

    private StringBuffer buffer = new StringBuffer();

    public ReplaceWriter(Writer w, String pattern, String replace) {
        this(w, new String[]{pattern}, new String[]{replace});
    }

    public ReplaceWriter(Writer w, String pattern, String replace,
                         String prefix, String postfix) {

        this(w, new String[]{pattern}, new String[]{replace}, 
             prefix, postfix);
    }

    public ReplaceWriter(Writer w, String[] patterns, String[] replaces) {
        this(w, patterns, replaces, null, null);
    }

    public ReplaceWriter(Writer w, String[] patterns, String[] replaces, 
                         String prefix, String postfix) {
	super(w);
        
	this.patterns = patterns;
	this.replaces = replaces;
        
        this.prefix = prefix;
        this.postfix = postfix;
        
	for (int i = 0; i < patterns.length; i++) {
	    int length = patterns[i].length();
	    if (length > maxPatternLength)
		maxPatternLength = length;
	}
    }

    private boolean processBuffer(int minLength) throws IOException {
        if (buffer.length() == 0) return false;

        if (!processedData) {
            if (prefix != null)
                super.write(prefix, 0, prefix.length());
            processedData = true;
        }

        int k;
	for (k = 0; minLength + k < buffer.length(); k++) {
	    for (int i = 0; i < patterns.length; i++) {
		String pattern = patterns[i];
		int length = pattern.length();

                if (length + k > buffer.length())
                    continue;

		boolean match = true;

		for (int j = 0; j < length; j++) {
		    if (pattern.charAt(j) != buffer.charAt(j + k)) {
			match = false;
                        break;
		    }
		}

		if (match) {
                    super.write(buffer.substring(0, k), 0, k);
                    buffer.delete(0, k + length);
                    super.write(replaces[i], 0, replaces[i].length());
		    return true;
		}
	    }
	}

        super.write(buffer.substring(0, k), 0, k);
        buffer.delete(0, k);

        return false;
    }

    public void write(int c) throws IOException {
	buffer.append((char)c);
	processBuffer(maxPatternLength);
    }

    public void write(char[] cbuf, int off, int len) throws IOException {
	for (int i = off; i < off + len; i++) {
	    buffer.append(cbuf[i]);
	}
	processBuffer(maxPatternLength);
    }

    public void write(String str, int off, int len) throws IOException {
        buffer.append(str.substring(off, off + len));
	processBuffer(maxPatternLength);
    }

    public void finish() throws IOException {
        while (processBuffer(0));

//         super.write(buffer.toString(), 0, buffer.length());
//         buffer.delete(0, buffer.length());

        if (processedData && postfix != null)
            super.write(postfix, 0, postfix.length());
    }

    public void close() throws IOException {
        finish();
        super.close();
    }
}