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.ThrowableUtils.*;
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>
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 VarResolverBuilder#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>
041 *    <li class='jac'>{@link SimpleVar}
042 *    <li class='jac'>{@link StreamedVar}
043 * </ul>
044 *
045 * <h5 class='section'>See Also:</h5>
046 * <ul>
047 *    <li class='link'>{@doc juneau-svl.SvlVariables}
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      this.name = name;
066      this.streamed = streamed;
067
068      if (name == null)
069         illegalArg("Invalid var name.  Must consist of only uppercase and lowercase ASCII letters.");
070      else for (int i = 0; i < name.length(); i++) {
071      // Need to make sure only ASCII characters are used.
072         char c = name.charAt(i);
073         if (c < 'A' || c > 'z' || (c > 'Z' && c < 'a'))
074            illegalArg("Invalid var name.  Must consist of only uppercase and lowercase ASCII letters.");
075      }
076   }
077
078   /**
079    * Return the name of this variable.
080    *
081    * <p>
082    * For example, the system property variable returns <js>"S"</js> since the format of the variable is
083    * <js>"$S{system.property}"</js>.
084    *
085    * @return The name of this variable.
086    */
087   protected String getName() {
088      return name;
089   }
090
091   /**
092    * Returns whether nested variables are supported by this variable.
093    *
094    * <p>
095    * For example, in <js>"$X{$Y{xxx}}"</js>, $Y is a nested variable that will be resolved if this method returns
096    * <jk>true</jk>.
097    *
098    * <p>
099    * The default implementation of this method always returns <jk>true</jk>.
100    * Subclasses can override this method to override the default behavior.
101    *
102    * @return <jk>true</jk> if nested variables are supported by this variable.
103    */
104   protected boolean allowNested() {
105      return true;
106   }
107
108   /**
109    * Returns whether variables in the resolved contents of this variable should also be resolved.
110    *
111    * <p>
112    * For example, if <js>"$X{xxx}"</js> resolves to <js>"$Y{xxx}"</js>, then the $Y variable will be recursively
113    * resolved if this method returns <jk>true</jk>.
114    *
115    * <p>
116    * The default implementation of this method always returns <jk>true</jk>.
117    * <br>Subclasses can override this method to override the default behavior.
118    *
119    * <h5 class='topic'>Important Note</h5>
120    * <p>
121    * As a general rule, variables that resolve user-entered data should not be recursively resolved as this may
122    * cause a security hole.
123    *
124    * @return <jk>true</jk> if resolved variables should be recursively resolved.
125    */
126   protected boolean allowRecurse() {
127      return true;
128   }
129
130   /**
131    * The method called from {@link VarResolver}.
132    *
133    * <p>
134    * Can be overridden to intercept the request and do special handling.
135    * <br>Default implementation simply calls resolve(String).
136    *
137    * @param session The session object used for a single instance of a string resolution.
138    * @param arg The inside argument of the variable.
139    * @return The resolved value.
140    * @throws Exception Any exception can be thrown.
141    */
142   protected String doResolve(VarResolverSession session, String arg) throws Exception {
143      return resolve(session, arg);
144   }
145
146   /**
147    * The interface that needs to be implemented for subclasses of {@link SimpleVar}.
148    *
149    * @param session The session object used for a single instance of a var 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   public abstract String resolve(VarResolverSession session, String arg) throws Exception;
155
156   /**
157    * The interface that needs to be implemented for subclasses of {@link StreamedVar}.
158    *
159    * @param session The session object used for a single instance of a var resolution.
160    * @param w The writer to send the resolved value to.
161    * @param arg The inside argument of the variable.
162    * @throws Exception Any exception can be thrown.
163    */
164   public abstract void resolveTo(VarResolverSession session, Writer w, String arg) throws Exception;
165}