1 /* 2 * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, is permitted under the terms of the 6 * GNU General Public License. 7 * 8 * For redistributing this software or a derivative work under a license other 9 * than the GPL-compatible Free Software License as defined by the Free 10 * Software Foundation or approved by OSI, you must first obtain a commercial 11 * license to this software product from rapiddweller GmbH & Volker Bergmann. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS, 15 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE 17 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 package com.rapiddweller.benerator.engine.expression; 28 29 import com.rapiddweller.common.Context; 30 import com.rapiddweller.format.script.ScriptUtil; 31 import com.rapiddweller.script.Expression; 32 import com.rapiddweller.script.expression.ConstantExpression; 33 import com.rapiddweller.script.expression.DynamicExpression; 34 35 /** 36 * Evaluates a string which may be a script (indicated by {}).<br/><br/> 37 * Created: 19.02.2010 10:39:29 38 * 39 * @author Volker Bergmann 40 * @since 0.6.0 41 */ 42 public class ScriptableExpression extends DynamicExpression<Object> { 43 44 private final String scriptOrText; 45 private final Expression<?> defaultValueExpression; 46 private final boolean isScript; 47 48 /** 49 * Instantiates a new Scriptable expression. 50 * 51 * @param scriptOrText the script or text 52 * @param defaultValue the default value 53 */ 54 public ScriptableExpression(String scriptOrText, Object defaultValue) { 55 this(scriptOrText, (defaultValue != null ? new ConstantExpression<>(defaultValue) : null)); 56 } 57 58 private ScriptableExpression(String scriptOrText, Expression<?> defaultValueExpression) { 59 this.defaultValueExpression = defaultValueExpression; 60 this.isScript = ScriptUtil.isScript(scriptOrText); 61 this.scriptOrText = scriptOrText; 62 } 63 64 /** 65 * Create with default expression expression. 66 * 67 * @param scriptOrText the script or text 68 * @param defaultValueExpression the default value expression 69 * @return the expression 70 */ 71 public static Expression<?> createWithDefaultExpression( 72 String scriptOrText, Expression<?> defaultValueExpression) { 73 return new ScriptableExpression(scriptOrText, defaultValueExpression); 74 } 75 76 @Override 77 public Object evaluate(Context context) { 78 Object result; 79 if (scriptOrText == null) { 80 result = (defaultValueExpression != null ? defaultValueExpression.evaluate(context) : null); 81 } else if (isScript) { 82 result = ScriptUtil.evaluate(scriptOrText, context); 83 } else { 84 result = scriptOrText; 85 } 86 return result; 87 } 88 89 @Override 90 public String toString() { 91 return (ScriptUtil.isScript(scriptOrText) ? scriptOrText : "'" + scriptOrText + "'"); 92 } 93 94 }