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.common.internal.ArgUtils.*;
016import static org.apache.juneau.common.internal.StringUtils.*;
017
018import java.util.regex.*;
019
020import org.apache.juneau.svl.*;
021
022/**
023 * A basic switch/case logic variable resolver.
024 *
025 * <p>
026 * The format for this var is one of the following:
027 * <ul>
028 *    <li><js>"$SW{stringArg, pattern:thenValue[, pattern:thenValue...]}"</js>
029 *    <li>...
030 * </ul>
031 *
032 * <p>
033 * The pattern can be any string optionally containing <js>'*'</js> or <js>'?'</js> representing any or one character
034 * respectively.
035 *
036 * <h5 class='section'>Example:</h5>
037 * <p class='bjava'>
038 *    <jc>// Create a variable resolver that resolves system properties and $SW vars.</jc>
039 *    VarResolver <jv>varResolver</jv> = VarResolver.<jsm>create</jsm>().vars(SwitchVar.<jk>class</jk>, SystemPropertiesVar.<jk>class</jk>).build();
040 *
041 *    <jc>// Use it!</jc>
042 *    System.<jsf>out</jsf>.println(<jv>varResolver</jv>.resolve(<js>"We are running on $SW{$P{os.name},*win*:Windows,*:Something else}!"</js>));
043 * </p>
044 *
045 * <p>
046 * Since this is a {@link MultipartVar}, any variables contained in the result will be recursively resolved.
047 * <br>Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var.
048 *
049 * <h5 class='section'>See Also:</h5><ul>
050 *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.SimpleVariableLanguage">Simple Variable Language</a>
051 * </ul>
052 */
053public class SwitchVar extends MultipartVar {
054
055   /** The name of this variable. */
056   public static final String NAME = "SW";
057
058   /**
059    * Constructor.
060    */
061   public SwitchVar() {
062      super(NAME);
063   }
064
065   @Override /* MultipartVar */
066   public String resolve(VarResolverSession session, String[] args) {
067      assertArg(args.length >= 2, "Invalid number of arguments passed to $SW var.  Must have 2 or more arguments.");
068
069      String stringArg = args[0];
070      for (int i = 1; i < args.length; i++) {
071         String pattern = args[i];
072
073         String[] parts = split(pattern, ':', 2);
074         assertArg(parts.length >= 2, "Invalid arguments passed to $SW var.  Each case statement must contains 'pattern:value'.");
075
076         Pattern p = Pattern.compile(parts[0].replace("*", ".*").replace("?", "."));
077         if (p.matcher(stringArg).matches())
078            return parts[1];
079      }
080
081      // Nothing matched and no else clause.
082      return "";
083   }
084}