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