001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.svl.vars; 014 015import static org.apache.juneau.internal.ThrowableUtils.*; 016 017import org.apache.juneau.svl.*; 018 019/** 020 * A basic if-else logic variable resolver. 021 * 022 * <p> 023 * The format for this var is one of the following: 024 * <ul> 025 * <li><js>"$IF{booleanArg,thenValue}"</js> 026 * <li><js>"$IF{booleanArg,thenValue,elseValue}"</js> 027 * </ul> 028 * 029 * <p> 030 * The boolean argument is any string. 031 * <br>The following values are interpreted as <jk>true</jk>: <js>"true"</js>,<js>"TRUE"</js>,<js>"t"</js>, 032 * <js>"T"</js>,<js>"1"</js>. 033 * <br>All else are interpreted as <jk>false</jk> 034 * 035 * <h5 class='section'>Example:</h5> 036 * <p class='bcode'> 037 * <jc>// Create a variable resolver that resolves system properties and $IF vars.</jc> 038 * VarResolver r = VarResolver.<jsm>create</jsm>().vars(IfVar.<jk>class</jk>, SystemPropertiesVar.<jk>class</jk>).build(); 039 * 040 * <jc>// Use it!</jc> 041 * System.<jsf>out</jsf>.println(r.resolve(<js>"Property $IF{$S{someBooleanFlag},IS,IS NOT} set!"</js>)); 042 * </p> 043 * 044 * <p> 045 * Since this is a {@link MultipartVar}, any variables contained in the result will be recursively resolved. 046 * <br>Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var. 047 * 048 * <h5 class='section'>See Also:</h5> 049 * <ul> 050 * <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-svl.SvlVariables">Overview > juneau-svl > SVL Variables</a> 051 * </ul> 052 */ 053public class IfVar extends MultipartVar { 054 055 /** The name of this variable. */ 056 public static final String NAME = "IF"; 057 058 /** 059 * Constructor. 060 */ 061 public IfVar() { 062 super(NAME); 063 } 064 065 @Override /* MultipartVar */ 066 public String resolve(VarResolverSession session, String[] args) { 067 if (args.length < 2 || args.length > 3) 068 illegalArg("Invalid number of arguments passed to $IF var. Must be either $IF{booleanArg,thenValue} or $IF{booleanArg,thenValue,elseValue}"); 069 070 String b = args[0].toLowerCase(); 071 if ("1".equals(b) || "t".equals(b) || "true".equals(b)) 072 return args[1]; 073 return args.length == 2 ? "" : args[2]; 074 } 075}