package org.elasticsearch.index.analysis;

import org.apache.lucene.analysis.charfilter.BaseCharFilter;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by medcl on 15/12/24.
 */

public final class STConvertCharFilter extends BaseCharFilter {

    private STConvertType convertType=STConvertType.traditional2simple;
    private Reader transformedInput;
    public STConvertCharFilter(Reader in,STConvertType convertType) {
        super(in);
        this.convertType = convertType;
    }

    public int read(char[] cbuf, int off, int len) throws IOException {
        if(this.transformedInput == null) {
            this.fill();
        }

        return this.transformedInput.read(cbuf, off, len);
    }

    private void fill() throws IOException {
        StringBuilder buffered = new StringBuilder();
        char[] temp = new char[1024];

        for(int cnt = this.input.read(temp); cnt > 0; cnt = this.input.read(temp)) {
            buffered.append(temp, 0, cnt);
        }

        this.transformedInput = new StringReader(this.processPattern(buffered).toString());
    }

    public int read() throws IOException {
        if(this.transformedInput == null) {
            this.fill();
        }

        return this.transformedInput.read();
    }

    protected int correct(int currentOff) {
        return Math.max(0, super.correct(currentOff));
    }

    CharSequence processPattern(CharSequence input) {
        System.out.println("stconvert char filter step in");
        String result = STConverter.getInstance().convert(convertType, input.toString());
        System.out.println("stfilter: "+input.toString()+"->"+result);
        return result;
    }
}

@@ -1536,243 +1536,79 @@
     
-System.out.println(%22stconvert char filter step in%22);%0A        String result = STConverter.getInstance().convert(convertType, input.toString());%0A        System.out.println(%22stfilter: %22+input.toString()+%22-%3E%22+result);%0A        return result
+return STConverter.getInstance().convert(convertType, input.toString())
 ;%0A  

remove debug info