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 returns substring for given start and end (optional)
024 *
025 * <p>
026 * The format for this var is one of the following:
027 * <ul>
028 *    <li><js>"$ST{arg,start}"</js>
029 *    <li><js>"$ST{arg,start,end}"</js>
030 * </ul>
031 *
032 * <p>
033 *
034 *
035 * <h5 class='section'>Example:</h5>
036 * <p class='bjava'>
037 *    <jc>// Create a variable resolver that resolves system properties and $SW vars.</jc>
038 *    VarResolver <jv>varResolver</jv> = VarResolver.<jsm>create</jsm>().vars(SubstringVar.<jk>class</jk>, SystemPropertiesVar.<jk>class</jk>).build();
039 *
040 *    <jc>// Use it!</jc>
041 *    System.<jsf>out</jsf>.println(<jv>varResolver</jv>.resolve(<js>"Version = $ST{$P{java.version}, 0, 3}"</js>));
042 * </p>
043 *
044 * <p>
045 * Since this is a {@link MultipartVar}, any variables contained in the result will be recursively resolved.
046 * <br>Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var.
047 *
048 * <h5 class='section'>See Also:</h5><ul>
049 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SimpleVariableLanguageBasics">Simple Variable Language Basics</a>
050 * </ul>
051 */
052public class SubstringVar extends MultipartVar {
053
054   /** The name of this variable. */
055   public static final String NAME = "ST";
056
057   /**
058    * Constructor.
059    */
060   public SubstringVar() {
061      super(NAME);
062   }
063
064   @Override /* MultipartVar */
065   public String resolve(VarResolverSession session, String[] args) {
066      Utils.assertArg(args.length >= 2 && args.length <= 3, "Invalid number of arguments passed to $ST var.  Must have 2 or 3 arguments.");
067
068      String stringArg = args[0];
069      String result = "";
070      if (args.length == 2) {
071         int start = Integer.parseInt(args[1]);
072         if (start >= 0 && start <= stringArg.length())
073            result = stringArg.substring(start);
074         if (start < 0 && -start <= stringArg.length())
075            result = stringArg.substring(stringArg.length() + start);
076      }
077      else if (args.length == 3) {
078         int start = Integer.parseInt(args[1]);
079         int end = Integer.parseInt(args[2]);
080         if (start >= 0 && start < stringArg.length() && end >= 0 && end <= stringArg.length() && start < end)
081            result = stringArg.substring(start, end);
082      }
083      return result;
084   }
085}