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

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

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

import info.bliki.wiki.model.Configuration;
import info.bliki.wiki.model.IWikiModel;

import java.util.List;

/**
 * A template parser function for {{ #if: ... }} syntax. See Mediwiki's
 * Help:Extension:ParserFunctions
 *
 */
public class If extends AbstractTemplateFunction {
    public final static ITemplateFunction CONST = new If();

    @Override
    public String parseFunction(List list, IWikiModel model, char[] src, int beginIndex, int endIndex, boolean isSubst) {
        if (list.size() > 1) {
            String ifCondition = "";
            try {
                ifCondition = isSubst ? list.get(0) : parseTrim(list.get(0), model);
                if (ifCondition.length() > 0) {
                    // <then text>
                    return isSubst ? list.get(1) : parseTrim(list.get(1), model);
                } else {
                    if (list.size() >= 3) {
                        // <else text>
                        return isSubst ? list.get(2) : parseTrim(list.get(2), model);
                    }
                }
            } catch (Exception e) {
                if (Configuration.DEBUG) {
                    System.out.println("#if error: " + ifCondition);
                }
                if (Configuration.STACKTRACE) {
                    e.printStackTrace();
                }
                return "
Expression error: " + e.getMessage() + "
"; } } return null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy