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 * <h5 class='section'>See Also:</h5><ul>
029 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.SimpleVariableLanguage">Simple Variable Language</a>
030
031 * </ul>
032 */
033public abstract class DefaultingVar extends SimpleVar {
034
035   /**
036    * Constructor.
037    *
038    * @param name The name of this variable.
039    */
040   public DefaultingVar(String name) {
041      super(name);
042   }
043
044   @Override /* Var*/
045   public String doResolve(VarResolverSession session, String s) throws Exception {
046      int i = s.indexOf(',');
047      if (i == -1)
048         return resolve(session, s.trim());
049      String s1 = s.substring(0, i);
050      String s2 = s.length() == i ? null : s.substring(i+1);
051      String v = resolve(session, s1);
052      if (v == null)
053         v = s2;
054      return v;
055   }
056}