001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.tools.template_engine;
003
004import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match;
005
006/**
007 * Conditional {@link TemplateEntry} that executes another template in case a search expression applies
008 * to the given data provider.
009 */
010public class SearchExpressionCondition implements TemplateEntry {
011
012    private final Match condition;
013    private final TemplateEntry text;
014
015    /**
016     * Creates a new {@link SearchExpressionCondition}.
017     * @param condition the match condition that is checked before applying the child template
018     * @param text the child template to execute in case the condition is fulfilled
019     */
020    public SearchExpressionCondition(Match condition, TemplateEntry text) {
021        this.condition = condition;
022        this.text = text;
023    }
024
025    @Override
026    public void appendText(StringBuilder result, TemplateEngineDataProvider dataProvider) {
027        text.appendText(result, dataProvider);
028    }
029
030    @Override
031    public boolean isValid(TemplateEngineDataProvider dataProvider) {
032        return dataProvider.evaluateCondition(condition);
033    }
034
035    @Override
036    public String toString() {
037        return condition + " '" + text + '\'';
038    }
039}