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 transformational variable that returns matched regex groups by given index.
023 *
024 * <p>
025 * The format for this var:
026 * <ul>
027 *    <li><js>"$PE{arg,pattern,groupIndex}"</js>
028 * </ul>
029 *
030 * <p>
031 * The pattern can be any string optionally containing <js>'*'</js> or <js>'?'</js> representing any or one character
032 * respectively.
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(PatternExtractVar.<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>"Update = $PE{$P{java.version},_([0-9]+),1}!"</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 PatternExtractVar extends MultipartVar {
052
053   /** The name of this variable. */
054   public static final String NAME = "PE";
055
056   /**
057    * Constructor.
058    */
059   public PatternExtractVar() {
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 $PE var.  Must have 3 arguments.");
066
067      String stringArg = args[0];
068      String pattern = args[1];
069      String result = "";
070      int groupId = Integer.parseInt(args[2]);
071
072      Pattern p = Pattern.compile(pattern.replace("*", ".*").replace("?", "."));
073      Matcher m = p.matcher(stringArg);
074
075      if (m.find() && groupId <= m.groupCount() && groupId >= 0) {
076         result = m.group(groupId);
077      }
078
079      return result;
080   }
081}