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 PatternReplaceVar} 075 * <li>{@link PatternExtractVar} 076 * <li>{@link UpperCaseVar} 077 * <li>{@link LowerCaseVar} 078 * <li>{@link NotEmptyVar} 079 * <li>{@link LenVar} 080 * <li>{@link SubstringVar} 081 * </ul> 082 * 083 * @return This object (for method chaining). 084 */ 085 public VarResolverBuilder defaultVars() { 086 return vars( 087 SystemPropertiesVar.class, 088 EnvVariablesVar.class, 089 ManifestFileVar.class, 090 ArgsVar.class, 091 SwitchVar.class, 092 IfVar.class, 093 CoalesceVar.class, 094 PatternMatchVar.class, 095 PatternReplaceVar.class, 096 PatternExtractVar.class, 097 UpperCaseVar.class, 098 LowerCaseVar.class, 099 NotEmptyVar.class, 100 LenVar.class, 101 SubstringVar.class); 102 } 103 104 /** 105 * Associates a context object with this resolver. 106 * 107 * <p> 108 * A context object is essentially some environmental object that doesn't change but is used by vars to customize 109 * output. 110 * 111 * @param name The name of the context object. 112 * @param object The context object. 113 * @return This object (for method chaining). 114 */ 115 public VarResolverBuilder contextObject(String name, Object object) { 116 contextObjects.put(name, object); 117 return this; 118 } 119 120 /** 121 * Associates multiple context objects with this resolver. 122 * 123 * <p> 124 * A context object is essentially some environmental object that doesn't change but is used by vars to customize 125 * output. 126 * 127 * @param map A map of context objects keyed by their name. 128 * @return This object (for method chaining). 129 */ 130 public VarResolverBuilder contextObjects(Map<String,Object> map) { 131 contextObjects.putAll(map); 132 return this; 133 } 134}