/** * Returns the message translation for a code, in the {@link Locale} of the * current request. * * @param pageContext of the current request. * @param code to get the message * @return the translated message * @throws JspException if there is an error getting the message */ public String resolveMessage(PageContext pageContext, String code) throws JspException { RequestContext requestContext = getRequestContext(pageContext); if (requestContext == null) { throw new JspTagException("No corresponding RequestContext found"); } MessageSource messageSource = requestContext.getMessageSource(); if (messageSource == null) { throw new JspTagException("No corresponding MessageSource found"); } String resolvedCode = ExpressionEvaluationUtils.evaluateString("code", code, pageContext); if (resolvedCode != null) { // We have no fallback text to consider. try { return messageSource.getMessage(resolvedCode, null, requestContext.getLocale()); } catch (NoSuchMessageException e) { LOG.warn("Unable to get message with code " + resolvedCode, e); } } return resolvedCode; }
@Override protected final int doStartTagInternal() throws Exception { String resolvedPath = ExpressionEvaluationUtils.evaluateString("path", getPath(), pageContext); if (!isIgnoreNestedPath()) { String nestedPath = (String) pageContext.getAttribute( NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE); // only prepend if not already an absolute path if (nestedPath != null && !resolvedPath.startsWith(nestedPath) && !resolvedPath.equals(nestedPath.substring(0, nestedPath.length() - 1))) { resolvedPath = nestedPath + resolvedPath; } } try { this.status = new BindStatus(getRequestContext(), resolvedPath, isHtmlEscape()); } catch (IllegalStateException ex) { throw new JspTagException(ex.getMessage()); } // Save previous status values, for re-exposure at the end of this tag. this.previousPageStatus = pageContext.getAttribute(STATUS_VARIABLE_NAME, PageContext.PAGE_SCOPE); this.previousRequestStatus = pageContext.getAttribute(STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); // Expose this tag's status object as PageContext attribute, // making it available for JSP EL. pageContext.removeAttribute(STATUS_VARIABLE_NAME, PageContext.PAGE_SCOPE); pageContext.setAttribute(STATUS_VARIABLE_NAME, this.status, PageContext.REQUEST_SCOPE); return EVAL_BODY_INCLUDE; }
@Override public int doStartTag() throws JspException { String resolvedPath = ExpressionEvaluationUtils.evaluateString("path", getPath(), pageContext); // Save previous nestedPath value, build and expose current nestedPath value. // Use request scope to expose nestedPath to included pages too. this.previousNestedPath = (String) pageContext.getAttribute(NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE); String nestedPath = (this.previousNestedPath != null ? this.previousNestedPath + resolvedPath : resolvedPath); pageContext.setAttribute(NESTED_PATH_VARIABLE_NAME, nestedPath, PageContext.REQUEST_SCOPE); return EVAL_BODY_INCLUDE; }
@Override protected int doStartTagInternal() throws JspException { boolean resolvedDefaultHtmlEscape = ExpressionEvaluationUtils.evaluateBoolean("defaultHtmlEscape", this.defaultHtmlEscape, pageContext); getRequestContext().setDefaultHtmlEscape(resolvedDefaultHtmlEscape); return EVAL_BODY_INCLUDE; }
/** * Evaluate the supplied value for the supplied attribute name. If the supplied value * is {@code null} then {@code null} is returned, otherwise evaluation is * handled using {@link ExpressionEvaluationUtils#evaluate(String, String, javax.servlet.jsp.PageContext)}. */ protected Object evaluate(String attributeName, Object value) throws JspException { if (value instanceof String) { return ExpressionEvaluationUtils.evaluate(attributeName, (String) value, this.pageContext); } else { return value; } }
/** * Resolves the message, escapes it if demanded, * and writes it to the page (or exposes it as variable). * @see #resolveMessage() * @see org.springframework.web.util.HtmlUtils#htmlEscape(String) * @see org.springframework.web.util.JavaScriptUtils#javaScriptEscape(String) * @see #writeMessage(String) */ @Override protected final int doStartTagInternal() throws JspException, IOException { try { // Resolve the unescaped message. String msg = resolveMessage(); // HTML and/or JavaScript escape, if demanded. msg = isHtmlEscape() ? HtmlUtils.htmlEscape(msg) : msg; msg = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(msg) : msg; // Expose as variable, if demanded, else write to the page. String resolvedVar = ExpressionEvaluationUtils.evaluateString("var", this.var, pageContext); if (resolvedVar != null) { String resolvedScope = ExpressionEvaluationUtils.evaluateString("scope", this.scope, pageContext); pageContext.setAttribute(resolvedVar, msg, TagUtils.getScope(resolvedScope)); } else { writeMessage(msg); } return EVAL_BODY_INCLUDE; } catch (NoSuchMessageException ex) { throw new JspTagException(getNoSuchMessageExceptionDescription(ex)); } }
/** * Resolve the given arguments Object into an arguments array. * @param arguments the specified arguments Object * @return the resolved arguments as array * @throws JspException if argument conversion failed * @see #setArguments */ protected Object[] resolveArguments(Object arguments) throws JspException { if (arguments instanceof String) { String[] stringArray = StringUtils.delimitedListToStringArray((String) arguments, this.argumentSeparator); if (stringArray.length == 1) { Object argument = ExpressionEvaluationUtils.evaluate("argument", stringArray[0], pageContext); if (argument != null && argument.getClass().isArray()) { return ObjectUtils.toObjectArray(argument); } else { return new Object[] {argument}; } } else { Object[] argumentsArray = new Object[stringArray.length]; for (int i = 0; i < stringArray.length; i++) { argumentsArray[i] = ExpressionEvaluationUtils.evaluate("argument[" + i + "]", stringArray[i], pageContext); } return argumentsArray; } } else if (arguments instanceof Object[]) { return (Object[]) arguments; } else if (arguments instanceof Collection) { return ((Collection) arguments).toArray(); } else if (arguments != null) { // Assume a single argument object. return new Object[] {arguments}; } else { return null; } }
@Override protected final int doStartTagInternal() throws ServletException, JspException { String resolvedName = ExpressionEvaluationUtils.evaluateString("name", this.name, pageContext); this.errors = getRequestContext().getErrors(resolvedName, isHtmlEscape()); if (this.errors != null && this.errors.hasErrors()) { this.pageContext.setAttribute(ERRORS_VARIABLE_NAME, this.errors, PageContext.REQUEST_SCOPE); return EVAL_BODY_INCLUDE; } else { return SKIP_BODY; } }
/** * Set JavaScript escaping for this tag, as boolean value. Default is * "false". */ public void setJavaScriptEscape(String javaScriptEscape) throws JspException { this.javaScriptEscape = ExpressionEvaluationUtils.evaluateBoolean( "javaScriptEscape", javaScriptEscape, pageContext); }
/** * Set JavaScript escaping for this tag, as boolean value. * Default is "false". */ public void setJavaScriptEscape(String javaScriptEscape) throws JspException { this.javaScriptEscape = ExpressionEvaluationUtils.evaluateBoolean("javaScriptEscape", javaScriptEscape, pageContext); }
/** * Set JavaScript escaping for this tag, as boolean value. * Default is "false". */ public void setJavaScriptEscape(String javaScriptEscape) throws JspException { this.javaScriptEscape = ExpressionEvaluationUtils.evaluateBoolean("javaScriptEscape", javaScriptEscape, this.pageContext); }
@Override protected final int doStartTagInternal() throws JspException { Object resolvedValue = this.value; if (this.value instanceof String) { String strValue = (String) this.value; resolvedValue = ExpressionEvaluationUtils.evaluate("value", strValue, pageContext); } if (resolvedValue != null) { // Find the containing EditorAwareTag (e.g. BindTag), if applicable. EditorAwareTag tag = (EditorAwareTag) TagSupport.findAncestorWithClass(this, EditorAwareTag.class); if (tag == null) { throw new JspException("TransformTag can only be used within EditorAwareTag (e.g. BindTag)"); } // OK, let's obtain the editor... String result = null; PropertyEditor editor = tag.getEditor(); if (editor != null) { // If an editor was found, edit the value. editor.setValue(resolvedValue); result = editor.getAsText(); } else { // Else, just do a toString. result = resolvedValue.toString(); } result = isHtmlEscape() ? HtmlUtils.htmlEscape(result) : result; String resolvedVar = ExpressionEvaluationUtils.evaluateString("var", this.var, pageContext); if (resolvedVar != null) { String resolvedScope = ExpressionEvaluationUtils.evaluateString("scope", this.scope, pageContext); pageContext.setAttribute(resolvedVar, result, TagUtils.getScope(resolvedScope)); } else { try { // Else, just print it out. pageContext.getOut().print(result); } catch (IOException ex) { throw new JspException(ex); } } } return SKIP_BODY; }
/** * Set HTML/XML escaping for this tag, as boolean value. Overrides the * default HTML escaping setting for the current page. * * @see HtmlEscapeTag#setDefaultHtmlEscape */ public void setHtmlEscape(String htmlEscape) throws JspException { this.htmlEscape = ExpressionEvaluationUtils.evaluateBoolean( "htmlEscape", htmlEscape, pageContext); }
/** * Evaluate the supplied value for the supplied attribute name. If the supplied value * is {@code null} then {@code false} is returned, otherwise evaluation is * handled using {@link ExpressionEvaluationUtils#evaluate(String, String, javax.servlet.jsp.PageContext)}, * with subsequent matching against {@code Boolean.TRUE} and {@code Boolean.valueOf}. */ protected boolean evaluateBoolean(String attributeName, String value) throws JspException { Object evaluated = ExpressionEvaluationUtils.evaluate(attributeName, value, this.pageContext); return (Boolean.TRUE.equals(evaluated) || (evaluated instanceof String && Boolean.valueOf((String) evaluated))); }
/** * Set HTML escaping for this tag, as boolean value. * Overrides the default HTML escaping setting for the current page. * @see HtmlEscapeTag#setDefaultHtmlEscape */ public void setHtmlEscape(String htmlEscape) throws JspException { this.htmlEscape = ExpressionEvaluationUtils.evaluateBoolean("htmlEscape", htmlEscape, pageContext); }