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