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