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