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