Package org.jzonic.jlo.formatter.tokens

Source Code of org.jzonic.jlo.formatter.tokens.TokenParser

package org.jzonic.jlo.formatter.tokens;

import org.jzonic.jlo.LogRecord;
import org.jzonic.jlo.error.ErrorHandler;
import gnu.regexp.RE;
import gnu.regexp.REMatch;

import java.util.Map;
import java.util.HashMap;

/**
* User: Mecky
* Date: 19.07.2005
* Time: 12:20:09
*/
public class TokenParser {

    private static Map tokens = new HashMap();
    static {
        tokens.put("text",new TextToken());
        tokens.put("class",new ClassToken());
        tokens.put("shortclass",new ShortClassToken());
        tokens.put("target",new TargetToken());
        tokens.put("date",new DateToken());
        tokens.put("ndc",new NDCToken());
        tokens.put("elapsed",new TimeTrackerToken());
    }

    public String parseLine(LogRecord lr,String format) {
        String ret = format;
        try {
          String expression = "\\$\\{([^\\}]+)\\}";
            RE re = new RE(expression);
            REMatch[] matches = re.getAllMatches(format);
            for ( int i = 0; i < matches.length;i++) {
                String token = matches[i].toString(1);
                String placeHolder = matches[i].toString(1);
                String txt = null;
                if ( token.startsWith("${")) {
                    token = token.substring(2);
                }
                if ( token.endsWith("}")) {
                    token = token.substring(0,token.length()-1);
                }
                if ( token.indexOf(":") != -1 ) {
                    token = token.substring(0,token.indexOf(":"));
                    txt = placeHolder.substring(placeHolder.indexOf(":")+1);
                    txt = txt.substring(0,txt.length());
                }
                String match = null;
                if ( tokens.containsKey(token)) {
                    FormatterToken ft = (FormatterToken)tokens.get(token);
                    if ( txt != null ) {
                        ft.setParameterString(txt);
                    }
                    match = ft.format(lr);
                }
                if ( match != null ) {
                    ret = re.substitute(ret,match);
                }
            }
            return ret;
        }
        catch (Exception e) {
            ErrorHandler.reportError("Error while formatting logrecord",e);
        }
        return ret;
    }
}
TOP

Related Classes of org.jzonic.jlo.formatter.tokens.TokenParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.