001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.svl; 018 019import java.io.*; 020 021import org.apache.juneau.common.utils.*; 022 023/** 024 * Abstract superclass of all Simple Var Language variables. 025 * 026 * <p> 027 * Vars are used to convert simple variables of the form <js>"$varName{varKey}"</js> into something else by the 028 * {@link VarResolver} class. 029 * 030 * <p> 031 * Subclasses must implement one of the following two methods: 032 * <ul class='javatree'> 033 * <li class='jm'>{@link #resolve(VarResolverSession,String)} - For simple vars. 034 * <li class='jm'>{@link #resolveTo(VarResolverSession,Writer,String)} - For streamed vars. 035 * </ul> 036 * 037 * <p> 038 * Subclasses MUST implement a no-arg constructor so that class names can be passed to the 039 * {@link VarResolver.Builder#vars(Class...)} method. 040 * <br><b>They must also be thread safe!</b> 041 * 042 * <p> 043 * Two direct abstract subclasses are provided to differentiated between simple and streamed vars: 044 * <ul class='javatree'> 045 * <li class='jac'>{@link SimpleVar} 046 * <li class='jac'>{@link StreamedVar} 047 * </ul> 048 * 049 * <h5 class='section'>See Also:</h5><ul> 050 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SimpleVariableLanguageBasics">Simple Variable Language Basics</a> 051 052 * </ul> 053 */ 054public abstract class Var { 055 056 private final String name; 057 final boolean streamed; 058 059 /** 060 * Constructor. 061 * 062 * @param name The name of this variable. 063 * @param streamed 064 * Whether this variable is 'streamed', meaning the {@link #resolveTo(VarResolverSession, Writer, String)} method 065 * is implemented. 066 * If <jk>false</jk>, then the {@link #resolve(VarResolverSession, String)} method is implemented. 067 */ 068 public Var(String name, boolean streamed) { 069 Utils.assertArgNotNull("name", name); 070 this.name = name; 071 this.streamed = streamed; 072 073 for (int i = 0; i < name.length(); i++) { 074 // Need to make sure only ASCII characters are used. 075 char c = name.charAt(i); 076 if (c < 'A' || c > 'z' || (c > 'Z' && c < 'a')) 077 throw new IllegalArgumentException("Invalid var name. Must consist of only uppercase and lowercase ASCII letters."); 078 } 079 } 080 081 /** 082 * Return the name of this variable. 083 * 084 * <p> 085 * For example, the system property variable returns <js>"S"</js> since the format of the variable is 086 * <js>"$S{system.property}"</js>. 087 * 088 * @return The name of this variable. 089 */ 090 protected String getName() { 091 return name; 092 } 093 094 /** 095 * Returns whether nested variables are supported by this variable. 096 * 097 * <p> 098 * For example, in <js>"$X{$Y{xxx}}"</js>, $Y is a nested variable that will be resolved if this method returns 099 * <jk>true</jk>. 100 * 101 * <p> 102 * The default implementation of this method always returns <jk>true</jk>. 103 * Subclasses can override this method to override the default behavior. 104 * 105 * @return <jk>true</jk> if nested variables are supported by this variable. 106 */ 107 protected boolean allowNested() { 108 return true; 109 } 110 111 /** 112 * Returns whether variables in the resolved contents of this variable should also be resolved. 113 * 114 * <p> 115 * For example, if <js>"$X{xxx}"</js> resolves to <js>"$Y{xxx}"</js>, then the $Y variable will be recursively 116 * resolved if this method returns <jk>true</jk>. 117 * 118 * <p> 119 * The default implementation of this method always returns <jk>true</jk>. 120 * <br>Subclasses can override this method to override the default behavior. 121 * 122 * <div class='warn'> 123 * As a general rule, variables that resolve user-entered data should not be recursively resolved as this may 124 * cause a security hole. 125 * </div> 126 * 127 * @return <jk>true</jk> if resolved variables should be recursively resolved. 128 */ 129 protected boolean allowRecurse() { 130 return true; 131 } 132 133 /** 134 * Returns <jk>true</jk> if this variable can be resolved in the specified session. 135 * 136 * <p> 137 * For example, some variable cannot resolve unless specific context or session objects are available. 138 * 139 * @param session The current session. 140 * @return <jk>true</jk> if this variable can be resolved in the specified session. 141 */ 142 protected boolean canResolve(VarResolverSession session) { 143 return true; 144 } 145 146 /** 147 * The method called from {@link VarResolver}. 148 * 149 * <p> 150 * Can be overridden to intercept the request and do special handling. 151 * <br>Default implementation simply calls resolve(String). 152 * 153 * @param session The session object used for a single instance of a string resolution. 154 * @param arg The inside argument of the variable. 155 * @return The resolved value. 156 * @throws Exception Any exception can be thrown. 157 */ 158 protected String doResolve(VarResolverSession session, String arg) throws Exception { 159 return resolve(session, arg); 160 } 161 162 /** 163 * The interface that needs to be implemented for subclasses of {@link SimpleVar}. 164 * 165 * @param session The session object used for a single instance of a var resolution. 166 * @param arg The inside argument of the variable. 167 * @return The resolved value. 168 * @throws Exception Any exception can be thrown. 169 */ 170 public abstract String resolve(VarResolverSession session, String arg) throws Exception; 171 172 /** 173 * The interface that needs to be implemented for subclasses of {@link StreamedVar}. 174 * 175 * @param session The session object used for a single instance of a var resolution. 176 * @param w The writer to send the resolved value to. 177 * @param arg The inside argument of the variable. 178 * @throws Exception Any exception can be thrown. 179 */ 180 public abstract void resolveTo(VarResolverSession session, Writer w, String arg) throws Exception; 181}