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 <code>resolve()</code> 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>
027 *    <li class='jm'>{@link #resolve(VarResolverSession, String)}
028 * </ul>
029 *
030 * <h5 class='section'>See Also:</h5>
031 * <ul>
032 *    <li class='link'>{@doc juneau-svl.SvlVariables}
033 * </ul>
034 */
035public abstract class DefaultingVar extends SimpleVar {
036
037   /**
038    * Constructor.
039    *
040    * @param name The name of this variable.
041    */
042   public DefaultingVar(String name) {
043      super(name);
044   }
045
046   @Override /* Var*/
047   public String doResolve(VarResolverSession session, String s) throws Exception {
048      int i = s.indexOf(',');
049      if (i == -1)
050         return resolve(session, s.trim());
051      String[] s2 = split(s);
052      String v = resolve(session, s2[0]);
053      if (v == null)
054         v = s2[1];
055      return v;
056   }
057}