public static TemplateException newInstantiatingClassNotAllowedException(String className, Environment env) { return new _MiscTemplateException(env, "Instantiating ", className, " is not allowed in the template for security reasons."); }
private static int getClassCode(Number num) throws TemplateException { try { return ((Integer) classCodes.get(num.getClass())).intValue(); } catch (NullPointerException e) { if (num == null) { throw new _MiscTemplateException("The Number object was null."); } else { throw new _MiscTemplateException("Unknown number type ", num.getClass().getName()); } } }
String getCurrentRecoveredErrorMessage() throws TemplateException { if (recoveredErrorStack.isEmpty()) { throw new _MiscTemplateException(this, ".error is not available outside of a #recover block"); } return ((Throwable) recoveredErrorStack.get(recoveredErrorStack.size() - 1)).getMessage(); }
public Class resolve(String className, Environment env, Template template) throws TemplateException { try { return ClassUtil.forName(className); } catch (ClassNotFoundException e) { throw new _MiscTemplateException(e, env); } }
public ConstructorFunction(String classname, Environment env, Template template) throws TemplateException { this.env = env; cl = env.getNewBuiltinClassResolver().resolve(classname, env, template); if (!TemplateModel.class.isAssignableFrom(cl)) { throw new _MiscTemplateException(NewBI.this, env, "Class ", cl.getName(), " does not implement freemarker.template.TemplateModel"); } if (BeanModel.class.isAssignableFrom(cl)) { throw new _MiscTemplateException(NewBI.this, env, "Bean Models cannot be instantiated using the ?", key, " built-in"); } if (JYTHON_MODEL_CLASS != null && JYTHON_MODEL_CLASS.isAssignableFrom(cl)) { throw new _MiscTemplateException(NewBI.this, env, "Jython Models cannot be instantiated using the ?", key, " built-in"); } }
@Override TemplateModel _eval(Environment env) throws TemplateException { try { return env.getVariable(name); } catch (NullPointerException e) { if (env == null) { throw new _MiscTemplateException( "Variables are not available (certainly you are in a parse-time executed directive). " + "The name of the variable you tried to read: ", name); } else { throw e; } } }
public Class resolve(String className, Environment env, Template template) throws TemplateException { if (className.equals(ObjectConstructor.class.getName()) || className.equals(Execute.class.getName()) || className.equals("freemarker.template.utility.JythonRuntime")) { throw _MessageUtil.newInstantiatingClassNotAllowedException(className, env); } try { return ClassUtil.forName(className); } catch (ClassNotFoundException e) { throw new _MiscTemplateException(e, env); } }
void loopForItemsElement(Environment env, TemplateElement[] childBuffer, String loopVarName, String loopVar2Name) throws NonSequenceOrCollectionException, TemplateModelException, InvalidReferenceException, TemplateException, IOException { try { if (alreadyEntered) { throw new _MiscTemplateException(env, "The #items directive was already entered earlier for this listing."); } alreadyEntered = true; this.loopVarName = loopVarName; this.loopVar2Name = loopVar2Name; executeNestedContent(env, childBuffer); } finally { this.loopVarName = null; this.loopVar2Name = null; } }
@Override TemplateModel calculateResult(String s, Environment env) throws TemplateException { final boolean b; if (s.equals("true")) { b = true; } else if (s.equals("false")) { b = false; } else if (s.equals(env.getTrueStringValue())) { b = true; } else if (s.equals(env.getFalseStringValue())) { b = false; } else { throw new _MiscTemplateException(this, env, "Can't convert this string to boolean: ", new _DelayedJQuote(s)); } return b ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; } }
private boolean getYesNo(Expression exp, String s) throws TemplateException { try { return StringUtil.getYesNo(s); } catch (IllegalArgumentException iae) { throw new _MiscTemplateException(exp, "Value must be boolean (or one of these strings: " + "\"n\", \"no\", \"f\", \"false\", \"y\", \"yes\", \"t\", \"true\"), but it was ", new _DelayedJQuote(s), "."); } }
final TemplateModel eval(Environment env) throws TemplateException { try { return constantValue != null ? constantValue : _eval(env); } catch (FlowControlException e) { throw e; } catch (TemplateException e) { throw e; } catch (Exception e) { if (env != null && EvalUtil.shouldWrapUncheckedException(e, env)) { throw new _MiscTemplateException( this, e, env, "Expression has thrown an unchecked exception; see the cause exception."); } else if (e instanceof RuntimeException) { throw (RuntimeException) e; } else { throw new UndeclaredThrowableException(e); } } }
protected void checkDateTypeNotUnknown(int dateType) throws TemplateException { if (dateType == TemplateDateModel.UNKNOWN) { throw new _MiscTemplateException(new _ErrorDescriptionBuilder( "The value of the following has unknown date type, but ?", key, " needs a value where it's known if it's a date (no time part), time, or date-time value:" ).blame(target).tip(_MessageUtil.UNKNOWN_DATE_TYPE_ERROR_TIP)); } }
@Override TemplateModel _eval(Environment env) throws TemplateException { IterationContext iterCtx = IteratorBlock.findEnclosingIterationContext(env, loopVarName); if (iterCtx == null) { // The parser should prevent this situation throw new _MiscTemplateException( this, env, "There's no iteration in context that uses loop variable ", new _DelayedJQuote(loopVarName), "."); } return calculateResult(iterCtx, env); }
@Override TemplateElement[] accept(Environment env) throws TemplateException, IOException { final IterationContext iterCtx = IteratorBlock.findEnclosingIterationContext(env, null); if (iterCtx == null) { // The parser should prevent this situation throw new _MiscTemplateException(env, getNodeTypeSymbol(), " without iteration in context"); } iterCtx.loopForItemsElement(env, getChildBuffer(), loopVarName, loopVar2Name); return null; }
@Override TemplateElement[] accept(Environment env) throws TemplateException, IOException { final IterationContext iterCtx = IteratorBlock.findEnclosingIterationContext(env, null); if (iterCtx == null) { // The parser should prevent this situation throw new _MiscTemplateException(env, getNodeTypeSymbol(), " without iteration in context"); } if (iterCtx.hasNext()) { return getChildBuffer(); } return null; }
protected TemplateException invalidSettingValueException(String name, String value) { return new _MiscTemplateException(getEnvironment(), "Invalid value for setting ", new _DelayedJQuote(name), ": ", new _DelayedJQuote(value)); }
@Override TemplateModel _eval(Environment env) throws TemplateException { if (!env.isAPIBuiltinEnabled()) { throw new _MiscTemplateException(this, "Can't use ?api, because the \"", Configurable.API_BUILTIN_ENABLED_KEY, "\" configuration setting is false. Think twice before you set it to true though. Especially, " + "it shouldn't abused for modifying Map-s and Collection-s."); } final TemplateModel tm = target.eval(env); if (!(tm instanceof TemplateModelWithAPISupport)) { target.assertNonNull(tm, env); throw new APINotSupportedTemplateException(env, target, tm); } return ((TemplateModelWithAPISupport) tm).getAPI(); } }
/** * Format number with the number format specified as the parameter, with the current locale. * * @param exp * The blamed expression if an error occurs; it's only needed for better error messages */ String formatNumberToPlainText(Number number, BackwardCompatibleTemplateNumberFormat format, Expression exp) throws TemplateModelException, _MiscTemplateException { try { return format.format(number); } catch (UnformattableValueException e) { throw new _MiscTemplateException(exp, e, this, "Failed to format number with ", new _DelayedJQuote(format.getDescription()), ": ", e.getMessage()); } }
public static TemplateException newCantFormatDateException(TemplateDateFormat format, Expression dataSrcExp, TemplateValueFormatException e, boolean useTempModelExc) { _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( "Failed to format date/time/datetime with format ", new _DelayedJQuote(format.getDescription()), ": ", e.getMessage()) .blame(dataSrcExp); return useTempModelExc ? new _TemplateModelException(e, (Environment) null, desc) : new _MiscTemplateException(e, (Environment) null, desc); }
public static TemplateException newCantFormatNumberException(TemplateNumberFormat format, Expression dataSrcExp, TemplateValueFormatException e, boolean useTempModelExc) { _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( "Failed to format number with format ", new _DelayedJQuote(format.getDescription()), ": ", e.getMessage()) .blame(dataSrcExp); return useTempModelExc ? new _TemplateModelException(e, (Environment) null, desc) : new _MiscTemplateException(e, (Environment) null, desc); }