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

info.bliki.wiki.template.Iferror Maven / Gradle / Ivy

The newest version!
package info.bliki.wiki.template;

import info.bliki.wiki.model.IWikiModel;

import java.util.List;

/**
 * A template parser function for {{ #iferror: ... }} syntax. See
 * Mediwiki's
 * Help:Extension:ParserFunctions
 *
 */
public class Iferror extends AbstractTemplateFunction {

    public final static ITemplateFunction CONST = new Iferror();

    @Override
    public String parseFunction(List list, IWikiModel model, char[] src, int beginIndex, int endIndex, boolean isSubst) {
        if (list.size() > 0) {
            boolean error = false;
            String iferrorCondition = isSubst ? list.get(0) : parseTrim(list.get(0), model);

            if (iferrorCondition.length() > 0) {
                error = iferrorCondition.indexOf(" class=\"error\"") > 0;
            }
            if (error) {
                // <then text>
                if (list.size() >= 2) {
                    return isSubst ? list.get(1) : parseTrim(list.get(1), model);
                }
                return "";
            } else {
                if (list.size() >= 3) {
                    // <else text>
                    return isSubst ? list.get(2) : parseTrim(list.get(2), model);
                }
                return iferrorCondition;
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy