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.ClassUtils.*;
016
017import java.util.*;
018
019import org.apache.juneau.svl.vars.*;
020
021/**
022 * Builder class for building instances of {@link VarResolver}.
023 *
024 * <h5 class='section'>See Also:</h5>
025 * <ul>
026 *    <li class='link'>{@doc juneau-svl.VarResolvers}
027 * </ul>
028 */
029public class VarResolverBuilder {
030
031   private final List<Class<? extends Var>> vars = new ArrayList<>();
032   private final Map<String,Object> contextObjects = new HashMap<>();
033
034   /**
035    * Create a new var resolver using the settings in this builder.
036    *
037    * @return A new var resolver.
038    */
039   public VarResolver build() {
040      return new VarResolver(vars.toArray(new Class[vars.size()]), contextObjects);
041   }
042
043   /**
044    * Register new variables with this resolver.
045    *
046    * @param vars
047    *    The variable resolver classes.
048    *    These classes must subclass from {@link Var} and have no-arg constructors.
049    * @return This object (for method chaining).
050    */
051   @SuppressWarnings("unchecked")
052   public VarResolverBuilder vars(Class<?>...vars) {
053      for (Class<?> v : vars) {
054         newInstance(Var.class, v);
055         this.vars.add((Class<? extends Var>)v);
056      }
057      return this;
058   }
059
060   /**
061    * Adds the default variables to this builder.
062    *
063    * <p>
064    * The default variables are:
065    * <ul>
066    *    <li>{@link SystemPropertiesVar}
067    *    <li>{@link EnvVariablesVar}
068    *    <li>{@link ArgsVar}
069    *    <li>{@link ManifestFileVar}
070    *    <li>{@link SwitchVar}
071    *    <li>{@link IfVar}
072    *    <li>{@link CoalesceVar}
073    *    <li>{@link PatternMatchVar}
074    *    <li>{@link UpperCaseVar}
075    *    <li>{@link LowerCaseVar}
076    *    <li>{@link NotEmptyVar}
077    * </ul>
078    *
079    * @return This object (for method chaining).
080    */
081   public VarResolverBuilder defaultVars() {
082      return vars(
083         SystemPropertiesVar.class,
084         EnvVariablesVar.class,
085         ManifestFileVar.class,
086         ArgsVar.class,
087         SwitchVar.class,
088         IfVar.class,
089         CoalesceVar.class,
090         PatternMatchVar.class,
091         UpperCaseVar.class,
092         LowerCaseVar.class,
093         NotEmptyVar.class);
094   }
095
096   /**
097    * Associates a context object with this resolver.
098    *
099    * <p>
100    * A context object is essentially some environmental object that doesn't change but is used by vars to customize
101    * output.
102    *
103    * @param name The name of the context object.
104    * @param object The context object.
105    * @return This object (for method chaining).
106    */
107   public VarResolverBuilder contextObject(String name, Object object) {
108      contextObjects.put(name, object);
109      return this;
110   }
111
112   /**
113    * Associates multiple context objects with this resolver.
114    *
115    * <p>
116    * A context object is essentially some environmental object that doesn't change but is used by vars to customize
117    * output.
118    *
119    * @param map A map of context objects keyed by their name.
120    * @return This object (for method chaining).
121    */
122   public VarResolverBuilder contextObjects(Map<String,Object> map) {
123      contextObjects.putAll(map);
124      return this;
125   }
126}