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