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 java.util.regex.*; 018 019import org.apache.juneau.internal.*; 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='bcode'> 038 * <jc>// Create a variable resolver that resolves system properties and $SW vars.</jc> 039 * VarResolver r = 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(r.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> 050 * <ul> 051 * <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-svl.SvlVariables">Overview > juneau-svl > SVL Variables</a> 052 * </ul> 053 */ 054public class SwitchVar extends MultipartVar { 055 056 /** The name of this variable. */ 057 public static final String NAME = "SW"; 058 059 /** 060 * Constructor. 061 */ 062 public SwitchVar() { 063 super(NAME); 064 } 065 066 @Override /* MultipartVar */ 067 public String resolve(VarResolverSession session, String[] args) { 068 if (args.length < 2) 069 illegalArg("Invalid number of arguments passed to $SW var. Must have 2 or more arguments."); 070 071 String stringArg = args[0]; 072 for (int i = 1; i < args.length; i++) { 073 String pattern = args[i]; 074 075 String[] parts = StringUtils.split(pattern, ':', 2); 076 if (parts.length < 2) 077 illegalArg("Invalid arguments passed to $SW var. Each case statement must contains 'pattern:value'."); 078 079 Pattern p = Pattern.compile(parts[0].replace("*", ".*").replace("?", ".")); 080 if (p.matcher(stringArg).matches()) 081 return parts[1]; 082 } 083 084 // Nothing matched and no else clause. 085 return ""; 086 } 087}