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 public Number modulus(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { return Integer.valueOf(first.intValue() % second.intValue()); } case LONG: { return Long.valueOf(first.longValue() % second.longValue()); } case FLOAT: { return Float.valueOf(first.floatValue() % second.floatValue()); } case DOUBLE: { return Double.valueOf(first.doubleValue() % second.doubleValue()); } case BIGINTEGER: { BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.mod(n2); } case BIGDECIMAL: { throw new _MiscTemplateException("Can't calculate remainder on BigDecimals"); } } // Make the compiler happy. getCommonClassCode() is guaranteed to // return only above codes, or throw an exception. throw new BugException(); }
public Class resolve(String className, Environment env, Template template) throws TemplateException { String templateName = safeGetTemplateName(template); if (templateName != null && (trustedTemplateNames.contains(templateName) || hasMatchingPrefix(templateName))) { return TemplateClassResolver.SAFER_RESOLVER.resolve(className, env, template); } else { if (!allowedClasses.contains(className)) { throw new _MiscTemplateException(env, "Instantiating ", className, " is not allowed in the template for security reasons. (If you " + "run into this problem when using ?new in a template, you may want to check the \"", Configurable.NEW_BUILTIN_CLASS_RESOLVER_KEY, "\" setting in the FreeMarker configuration.)"); } else { try { return ClassUtil.forName(className); } catch (ClassNotFoundException e) { throw new _MiscTemplateException(e, env); } } } }
@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), "."); } }
} catch (IndexOutOfBoundsException e) { if (index < 0) { throw new _MiscTemplateException("Negative index not allowed: ", Integer.valueOf(index)); throw new _MiscTemplateException( "String index out of range: The index was ", Integer.valueOf(index), " (0-based), but the length of the string is only ", Integer.valueOf(s.length()) , ".");
String formatBoolean(boolean value, boolean fallbackToTrueFalse) throws TemplateException { if (value) { String s = getTrueStringValue(); if (s == null) { if (fallbackToTrueFalse) { return MiscUtil.C_TRUE; } else { throw new _MiscTemplateException(getNullBooleanFormatErrorDescription()); } } else { return s; } } else { String s = getFalseStringValue(); if (s == null) { if (fallbackToTrueFalse) { return MiscUtil.C_FALSE; } else { throw new _MiscTemplateException(getNullBooleanFormatErrorDescription()); } } else { return s; } } }
@Override TemplateModel _eval(Environment env) throws TemplateException { TemplateModel targetValue = target.evalToNonMissing(env); List parameters = this.parameters; int paramCnt = parameters.size(); for (int i = 0; i + 1 < paramCnt; i += 2) { Expression caseExp = (Expression) parameters.get(i); TemplateModel caseValue = caseExp.evalToNonMissing(env); if (EvalUtil.compare( targetValue, target, EvalUtil.CMP_OP_EQUALS, "==", caseValue, caseExp, this, true, false, false, false, env)) { return ((Expression) parameters.get(i + 1)).evalToNonMissing(env); } } if (paramCnt % 2 == 0) { throw new _MiscTemplateException(target, "The value before ?", key, "(case1, value1, case2, value2, ...) didn't match any of the " + "case parameters, and there was no default value parameter (an additional last parameter) " + "eithter. "); } return ((Expression) parameters.get(paramCnt - 1)).evalToNonMissing(env); }
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; }
private TemplateModel getNodeProcessor(final String nodeName, final String nsURI, int startIndex) throws TemplateException { TemplateModel result = null; int i; int size = nodeNamespaces.size(); for (i = startIndex; i < size; i++) { Namespace ns = null; try { ns = (Namespace) nodeNamespaces.get(i); } catch (ClassCastException cce) { throw new _MiscTemplateException(this, "A \"using\" clause should contain a sequence of namespaces or strings that indicate the " + "location of importable macro libraries."); } result = getNodeProcessor(ns, nodeName, nsURI); if (result != null) break; } if (result != null) { this.nodeNamespaceIndex = i + 1; this.currentNodeName = nodeName; this.currentNodeNS = nsURI; } return result; }