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;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017/**
018 * Interface for the resolution of vars with a default value if the <c>resolve()</c> method returns <jk>null</jk>.
019 *
020 * <p>
021 * For example, to resolve the system property <js>"myProperty"</js> but resolve to <js>"not found"</js> if the
022 * property doesn't exist: <js>"$S{myProperty,not found}"</js>
023 *
024 * <p>
025 * Subclasses must implement the following method:
026 * <ul class='javatree'>
027 *    <li class='jm'>{@link #resolve(VarResolverSession, String)}
028 * </ul>
029 *
030 * <ul class='seealso'>
031 *    <li class='link'>{@doc juneau-svl.SvlVariables}
032 * </ul>
033 */
034public abstract class DefaultingVar extends SimpleVar {
035
036   /**
037    * Constructor.
038    *
039    * @param name The name of this variable.
040    */
041   public DefaultingVar(String name) {
042      super(name);
043   }
044
045   @Override /* Var*/
046   public String doResolve(VarResolverSession session, String s) throws Exception {
047      int i = s.indexOf(',');
048      if (i == -1)
049         return resolve(session, s.trim());
050      String[] s2 = split(s);
051      String v = resolve(session, s2[0]);
052      if (v == null)
053         v = s2[1];
054      return v;
055   }
056}