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 java.util.regex.*; 022 023import org.apache.juneau.svl.*; 024 025/** 026 * A transformational variable resolver that returns character count or list count (using given delimiter). 027 * 028 * <p> 029 * The format for this var is one of the following: 030 * <ul> 031 * <li><js>"$LN{arg}"</js> 032 * <li><js>"$LN{arg,delimiter}"</js> 033 * </ul> 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(LenVar.<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>"Parts = $LN{$P{os.version},.} Chars = $LN{$P{os.version}}"</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 LenVar extends MultipartVar { 053 054 /** The name of this variable. */ 055 public static final String NAME = "LN"; 056 057 /** 058 * Constructor. 059 */ 060 public LenVar() { 061 super(NAME); 062 } 063 064 @Override /* Overridden from MultipartVar */ 065 public String resolve(VarResolverSession session, String[] args) { 066 assertArg(args.length <= 2, "Invalid number of arguments passed to $LN var. Must have 1 or 2 arguments."); 067 068 int len = 0; 069 String stringArg = args[0]; 070 if (args.length == 1) 071 len = stringArg.length(); 072 else if (args.length == 2) { 073 //delimiter is given 074 String delimiter = Pattern.quote(args[1]); 075 len = stringArg.trim().split(delimiter).length; 076 } 077 return String.valueOf(len); 078 } 079}