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