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 org.apache.juneau.svl.*; 018 019/** 020 * A transformational variable resolver that replaces matched patterns with given characters. 021 * 022 * <p> 023 * The format for this var is: 024 * <ul> 025 * <li><js>"$PR{stringArg,pattern,replace}"</js> 026 * </ul> 027 * 028 * <p> 029 * The pattern can be any string optionally containing <js>'*'</js> or <js>'?'</js> representing any or one character 030 * respectively. 031 * 032 * The replace can contain matched regex sub classes like \$1, \$2 .. 033 * 034 * <h5 class='section'>Example:</h5> 035 * <p class='bjava'> 036 * <jc>// Create a variable resolver that resolves system properties and $SW vars.</jc> 037 * VarResolver <jv>varResolver</jv> = VarResolver.<jsm>create</jsm>().vars(PatternReplaceVar.<jk>class</jk>, SystemPropertiesVar.<jk>class</jk>).build(); 038 * 039 * <jc>// Use it!</jc> 040 * System.<jsf>out</jsf>.println(<jv>varResolver</jv>.resolve(<js>"Java version=$PR{$S{java.version}, (_([0-9]+)), \\ build=\\$2}"</js>)); 041 * </p> 042 * 043 * <p> 044 * Since this is a {@link MultipartVar}, any variables contained in the result will be recursively resolved. 045 * <br>Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var. 046 * 047 * <h5 class='section'>See Also:</h5><ul> 048 * <li class='link'><a class="doclink" href="../../../../../index.html#jm.SimpleVariableLanguage">Simple Variable Language</a> 049 * </ul> 050 */ 051public class PatternReplaceVar extends MultipartVar { 052 053 /** The name of this variable. */ 054 public static final String NAME = "PR"; 055 056 /** 057 * Constructor. 058 */ 059 public PatternReplaceVar() { 060 super(NAME); 061 } 062 063 @Override /* MultipartVar */ 064 public String resolve(VarResolverSession session, String[] args) { 065 assertArg(args.length >= 3, "Invalid number of arguments passed to $PR var. Must have 3 or more arguments."); 066 067 String stringArg = args[0]; 068 String pattern = args[1]; 069 String replace = args[2]; 070 071 pattern = pattern.replace("*", ".*").replace("?", "."); 072 return stringArg.replaceAll(pattern, replace); 073 } 074}