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 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='bjava'> 034 * <jc>// Create a variable resolver that resolves system properties and $SW vars.</jc> 035 * VarResolver <jv>varResolver</jv> = 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(<jv>varResolver</jv>.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 * <h5 class='section'>See Also:</h5><ul> 046 * <li class='link'><a class="doclink" href="../../../../../index.html#jm.SimpleVariableLanguage">Simple Variable Language</a> 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 assertArg(args.length >= 2 && args.length <= 3, "Invalid number of arguments passed to $ST var. Must have 2 or 3 arguments."); 064 065 String stringArg = args[0]; 066 String result = ""; 067 if (args.length == 2) { 068 int start = Integer.parseInt(args[1]); 069 if (start >= 0 && start <= stringArg.length()) 070 result = stringArg.substring(start); 071 if (start < 0 && -start <= stringArg.length()) 072 result = stringArg.substring(stringArg.length() + start); 073 } 074 else if (args.length == 3) { 075 int start = Integer.parseInt(args[1]); 076 int end = Integer.parseInt(args[2]); 077 if (start >= 0 && start < stringArg.length() && end >= 0 && end <= stringArg.length() && start < end) 078 result = stringArg.substring(start, end); 079 } 080 return result; 081 } 082}