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