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