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