@Override protected String dump(boolean canonical) { StringBuilder sb = new StringBuilder(); if (canonical) sb.append('<'); sb.append(getNodeTypeSymbol()); sb.append(' '); sb.append(transformExpression); if (namedArgs != null) { for (Iterator it = getSortedNamedArgs().iterator(); it.hasNext(); ) { Map.Entry entry = (Map.Entry) it.next(); sb.append(' '); sb.append(entry.getKey()); sb.append('='); _MessageUtil.appendExpressionAsUntearable(sb, (Expression) entry.getValue()); } } if (canonical) { sb.append(">"); sb.append(getChildrenCanonicalForm()); sb.append("</").append(getNodeTypeSymbol()).append('>'); } return sb.toString(); }
private static String formatLocation(String preposition, String templateSourceName, int line, int column) { return formatLocation( preposition, templateSourceName, null, false, line, column); }
static void appendInstructionStackItem(TemplateElement stackEl, StringBuilder sb) { sb.append(_MessageUtil.shorten(stackEl.getDescription(), 40)); sb.append(" ["); Macro enclosingMacro = getEnclosingMacro(stackEl); if (enclosingMacro != null) { sb.append(_MessageUtil.formatLocationForEvaluationError( enclosingMacro, stackEl.beginLine, stackEl.beginColumn)); } else { sb.append(_MessageUtil.formatLocationForEvaluationError( stackEl.getTemplate(), stackEl.beginLine, stackEl.beginColumn)); } sb.append("]"); }
public String getAsString() throws TemplateModelException { if (cachedValue == null) { if (defaultFormat == null) { if (dateModel.getDateType() == TemplateDateModel.UNKNOWN) { throw _MessageUtil.newCantFormatUnknownTypeDateException(target, null); } else { throw new BugException(); } } try { cachedValue = EvalUtil.assertFormatResultNotNull(defaultFormat.formatToPlainText(dateModel)); } catch (TemplateValueFormatException e) { try { throw _MessageUtil.newCantFormatDateException(defaultFormat, target, e, true); } catch (TemplateException e2) { // `e` should always be a TemplateModelException here, but to be sure: throw _CoreAPI.ensureIsTemplateModelException("Failed to format date/time/datetime", e2); } } } return cachedValue; }
public Object exec(List args) throws TemplateModelException { final int argCnt = args.size(); if (argCnt < 1 || argCnt > 2) { throw _MessageUtil.newArgCntError(methodName, argCnt, 1, 2); throw _MessageUtil.newMethodArgMustBeStringException(methodName, 0, arg); TemplateModel arg = (TemplateModel) args.get(1); if (!(arg instanceof TemplateHashModelEx)) { throw _MessageUtil.newMethodArgMustBeExtendedHashException(methodName, 1, arg); throw _MessageUtil.newMethodArgInvalidValueException(methodName, 1, "All keys in the options hash must be strings, but found ", new _DelayedAOrAn(new _DelayedFTLTypeDescription(optNameTM))); parse = getBooleanOption(OPTION_PARSE, optValue); } else { throw _MessageUtil.newMethodArgInvalidValueException(methodName, 1, "Unsupported option ", new _DelayedJQuote(optName), "; valid names are: ", new _DelayedJQuote(OPTION_ENCODING), ", ", new _DelayedJQuote(OPTION_PARSE), ".");
return assertFormatResultNotNull(format.format(tnm)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, false); return assertFormatResultNotNull(format.format(tdm)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatDateException(format, exp, e, false);
protected final void checkMethodArgCount(int argCnt, int expectedCnt) throws TemplateModelException { if (argCnt != expectedCnt) { throw _MessageUtil.newArgCntError("?" + key, argCnt, expectedCnt); } }
/** * Returns a string that indicates * where in the template source, this object is. */ public String getStartLocation() { return _MessageUtil.formatLocationForEvaluationError(template, beginLine, beginColumn); }
/** * @param tdmSourceExpr * The blamed expression if an error occurs; only used for error messages. */ String formatDateToPlainText(TemplateDateModel tdm, Expression tdmSourceExpr, boolean useTempModelExc) throws TemplateException { TemplateDateFormat format = getTemplateDateFormat(tdm, tdmSourceExpr, useTempModelExc); try { return EvalUtil.assertFormatResultNotNull(format.formatToPlainText(tdm)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatDateException(format, tdmSourceExpr, e, useTempModelExc); } }
private TokenMgrError newNameConventionMismatchException(Token tok) { return new TokenMgrError( "Naming convention mismatch. " + "Identifiers that are part of the template language (not the user specified ones) " + (initialNamingConvention == Configuration.AUTO_DETECT_NAMING_CONVENTION ? "must consistently use the same naming convention within the same template. This template uses " : "must use the configured naming convention, which is the ") + (namingConvention == Configuration.CAMEL_CASE_NAMING_CONVENTION ? "camel case naming convention (like: exampleName) " : (namingConvention == Configuration.LEGACY_NAMING_CONVENTION ? "legacy naming convention (directive (tag) names are like examplename, " + "everything else is like example_name) " : "??? (internal error)" )) + (namingConventionEstabilisher != null ? "estabilished by auto-detection at " + _MessageUtil.formatPosition( namingConventionEstabilisher.beginLine, namingConventionEstabilisher.beginColumn) + " by token " + StringUtil.jQuote(namingConventionEstabilisher.image.trim()) : "") + ", but the problematic token, " + StringUtil.jQuote(tok.image.trim()) + ", uses a different convention.", TokenMgrError.LEXICAL_ERROR, tok.beginLine, tok.beginColumn, tok.endLine, tok.endColumn); }
/** * Same as {@link #getTemplateDateFormat(String, int, Class)}, but translates the exceptions to * {@link TemplateException}-s. */ TemplateDateFormat getTemplateDateFormat( String formatString, int dateType, Class<? extends Date> dateClass, Expression blamedDateSourceExp, Expression blamedFormatterExp, boolean useTempModelExc) throws TemplateException { try { return getTemplateDateFormat(formatString, dateType, dateClass); } catch (UnknownDateTypeFormattingUnsupportedException e) { throw _MessageUtil.newCantFormatUnknownTypeDateException(blamedDateSourceExp, e); } catch (TemplateValueFormatException e) { _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( "Can't create date/time/datetime format based on format string ", new _DelayedJQuote(formatString), ". Reason given: ", e.getMessage()) .blame(blamedFormatterExp); throw useTempModelExc ? new _TemplateModelException(e, desc) : new _MiscTemplateException(e, desc); } }
@Override protected String doConversion(Object obj) { String s = obj.toString(); return _MessageUtil.getAOrAn(s) + " " + s; }
/** * 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( TemplateNumberModel number, TemplateNumberFormat format, Expression exp, boolean useTempModelExc) throws TemplateException { try { return EvalUtil.assertFormatResultNotNull(format.formatToPlainText(number)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, useTempModelExc); } }
private void renderMessageAndDescription() { String desc = getOrRenderDescription(); String prefix; if (!isInJBossToolsMode()) { prefix = "Syntax error " + _MessageUtil.formatLocationForSimpleParsingError(templateName, lineNumber, columnNumber) + ":\n"; } else { prefix = "[col. " + columnNumber + "] "; } String msg = prefix + desc; desc = msg.substring(prefix.length()); // so we reuse the backing char[] synchronized (this) { message = msg; description = desc; messageAndDescriptionRendered = true; } }
public Object exec(List args) throws TemplateModelException { final int argCnt = args.size(); if (argCnt < 1 || argCnt > 2) { throw _MessageUtil.newArgCntError(methodName, argCnt, 1, 2); throw _MessageUtil.newMethodArgMustBeStringException(methodName, 0, arg); TemplateModel arg = (TemplateModel) args.get(1); if (!(arg instanceof TemplateHashModelEx)) { throw _MessageUtil.newMethodArgMustBeExtendedHashException(methodName, 1, arg); throw _MessageUtil.newMethodArgInvalidValueException(methodName, 1, "All keys in the options hash must be strings, but found ", new _DelayedAOrAn(new _DelayedFTLTypeDescription(optNameTM))); parse = getBooleanOption(OPTION_PARSE, optValue); } else { throw _MessageUtil.newMethodArgInvalidValueException(methodName, 1, "Unsupported option ", new _DelayedJQuote(optName), "; valid names are: ", new _DelayedJQuote(OPTION_ENCODING), ", ", new _DelayedJQuote(OPTION_PARSE), ".");
return ensureFormatResultString(format.format(tnm), exp, env); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, false); return ensureFormatResultString(format.format(tdm), exp, env); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatDateException(format, exp, e, false);
protected final void checkMethodArgCount(int argCnt, int minCnt, int maxCnt) throws TemplateModelException { if (argCnt < minCnt || argCnt > maxCnt) { throw _MessageUtil.newArgCntError("?" + key, argCnt, minCnt, maxCnt); } }
public String getAsString() throws TemplateModelException { if (cachedValue == null) { if (defaultFormat == null) { if (dateModel.getDateType() == TemplateDateModel.UNKNOWN) { throw _MessageUtil.newCantFormatUnknownTypeDateException(target, null); } else { throw new BugException(); } } try { cachedValue = EvalUtil.assertFormatResultNotNull(defaultFormat.formatToPlainText(dateModel)); } catch (TemplateValueFormatException e) { try { throw _MessageUtil.newCantFormatDateException(defaultFormat, target, e, true); } catch (TemplateException e2) { // `e` should always be a TemplateModelException here, but to be sure: throw _CoreAPI.ensureIsTemplateModelException("Failed to format date/time/datetime", e2); } } } return cachedValue; }
public String getEndLocation() { return _MessageUtil.formatLocationForEvaluationError(template, endLine, endColumn); }
/** * @param blamedDateSourceExp * The blamed expression if an error occurs; only used for error messages. * @param blamedFormatterExp * The blamed expression if an error occurs; only used for error messages. */ String formatDateToPlainText(TemplateDateModel tdm, String formatString, Expression blamedDateSourceExp, Expression blamedFormatterExp, boolean useTempModelExc) throws TemplateException { Date date = EvalUtil.modelToDate(tdm, blamedDateSourceExp); TemplateDateFormat format = getTemplateDateFormat( formatString, tdm.getDateType(), date.getClass(), blamedDateSourceExp, blamedFormatterExp, useTempModelExc); try { return EvalUtil.assertFormatResultNotNull(format.formatToPlainText(tdm)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatDateException(format, blamedDateSourceExp, e, useTempModelExc); } }