info.bliki.wiki.template.Ifexpr Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bliki-core Show documentation
Show all versions of bliki-core Show documentation
This is the core project.
The newest version!
package info.bliki.wiki.template;
import info.bliki.wiki.model.Configuration;
import info.bliki.wiki.model.IWikiModel;
import info.bliki.wiki.template.expr.eval.DoubleEvaluator;
import java.util.List;
/**
* A template parser function for {{ #ifexpr: ... }}
syntax
*
* See
* Mediwiki's Help:Extension:ParserFunctions See: Expr.php in MediaWiki SVN
*/
public class Ifexpr extends AbstractTemplateFunction {
public final static ITemplateFunction CONST = new Ifexpr();
@Override
public String parseFunction(List list, IWikiModel model, char[] src, int beginIndex, int endIndex, boolean isSubst) {
if (list.size() > 1) {
String condition = isSubst ? list.get(0) : parseTrim(list.get(0), model);
if (condition.length() > 0) {
try {
DoubleEvaluator engine = new DoubleEvaluator();
double d = engine.evaluate(condition);
// if (d == 0.0) {
if (Math.abs(d - 0.0) < DoubleEvaluator.EPSILON) {
if (list.size() >= 3) {
// <else text>
return isSubst ? list.get(2) : parseTrim(list.get(2), model);
}
return null;
}
return isSubst ? list.get(1) : parseTrim(list.get(1), model);
} catch (Exception e) {
if (Configuration.DEBUG) {
System.out.println("#ifexpr error: "+condition);
}
if (Configuration.STACKTRACE) {
e.printStackTrace();
}
return "Expression error: " + e.getMessage() + "";
}
} else {
if (list.size() >= 3) {
// <else text>
return isSubst ? list.get(2) : parseTrim(list.get(2), model);
}
}
}
return null;
}
}