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