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