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.*; 016 017import java.util.regex.*; 018 019import org.apache.juneau.svl.*; 020 021/** 022 * A logical variable resolver that resolves to <js>"true</js> if a pattern matches. 023 * 024 * <p> 025 * The format for this var is one of the following: 026 * <ul> 027 * <li><js>"$PM{stringArg, pattern}"</js> 028 * <li>... 029 * </ul> 030 * 031 * <p> 032 * The pattern can be any string optionally containing <js>'*'</js> or <js>'?'</js> representing any or one character 033 * respectively. 034 * 035 * <h5 class='section'>Example:</h5> 036 * <p class='bjava'> 037 * <jc>// Create a variable resolver that resolves system properties and $SW vars.</jc> 038 * VarResolver <jv>varResolver</jv> = VarResolver.<jsm>create</jsm>().vars(PatternMatchVar.<jk>class</jk>, SystemPropertiesVar.<jk>class</jk>).build(); 039 * 040 * <jc>// Use it!</jc> 041 * System.<jsf>out</jsf>.println(<jv>varResolver</jv>.resolve(<js>"Running on Windows? $PM{$P{os.name},*win*}!"</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><ul> 049 * <li class='link'><a class="doclink" href="../../../../../index.html#jm.SimpleVariableLanguage">Simple Variable Language</a> 050 * </ul> 051 */ 052public class PatternMatchVar extends MultipartVar { 053 054 /** The name of this variable. */ 055 public static final String NAME = "PM"; 056 057 /** 058 * Constructor. 059 */ 060 public PatternMatchVar() { 061 super(NAME); 062 } 063 064 @Override /* MultipartVar */ 065 public String resolve(VarResolverSession session, String[] args) { 066 assertArg(args.length >= 2, "Invalid number of arguments passed to $PM var. Must have 2 or more arguments."); 067 068 String stringArg = args[0]; 069 String pattern = args[1]; 070 Pattern p = Pattern.compile(pattern.replace("*", ".*").replace("?", ".")); 071 return String.valueOf(p.matcher(stringArg).matches()); 072 } 073}