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.internal.ThrowableUtils.*;
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='bcode w800'>
036 *    <jc>// Create a variable resolver that resolves system properties and $SW vars.</jc>
037 *    VarResolver r = 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(r.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 * <ul class='seealso'>
048 *    <li class='link'>{@doc SvlVariables}
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      if (args.length < 3)
066         illegalArg("Invalid number of arguments passed to $PE var.  Must have 3 arguments.");
067
068      String stringArg = args[0];
069      String pattern = args[1];
070      String result = "";
071      int groupId = Integer.parseInt(args[2]);
072
073      Pattern p = Pattern.compile(pattern.replace("*", ".*").replace("?", "."));
074      Matcher m = p.matcher(stringArg);
075
076      if (m.find() && groupId <= m.groupCount() && groupId >= 0) {
077         result = m.group(groupId);
078      }
079
080      return result;
081   }
082}