All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kefirsf.bb.proc.Check Maven / Gradle / Ivy

Go to download

KefirBB is a Java-library for text processing. Initially it was developed for BB2HTML translation. But flexible configuration allows to use it in different cases. For example for parsing Markdown, Textile, and for HTML filtration.

The newest version!
package org.kefirsf.bb.proc;

import java.text.MessageFormat;

/**
 * Check if the variable is equals with context value
 */
public class Check extends ProcNamedElement implements ProcPatternElement {
    private final boolean ghost;

    public Check(String name, boolean ghost) {
        super(name);
        this.ghost = ghost;
    }

    public boolean parse(Context context, ProcPatternElement terminator) throws NestingException {
        if(isNextIn(context)){
            if (!ghost) {
                context.getSource().incOffset(getContextLength(context));
            }
            return true;
        } else {
            return false;
        }
    }

    private CharSequence getContextValue(Context context) {
        return (CharSequence) context.getAttribute(getName());
    }

    private int getContextLength(Context context) {
        CharSequence value = getContextValue(context);
        if(value!=null){
            return value.length();
        }else{
            return 0;
        }
    }

    public boolean isNextIn(Context context) {
        Source source = context.getSource();
        int offset = source.getOffset();

        CharSequence old = getContextValue(context);
        if(old==null){
            return false;
        }

        int length = old.length();

        if(offset+length>source.length()){
            return false;
        }

        CharSequence val = source.sub(offset + length);
        return val.equals(old);
    }

    public int findIn(Source source) {
        return -1;
    }

    @Override
    public String toString() {
        return MessageFormat.format("", getName(), ghost);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy