/** * @see MessageTag#doStartTagInternal() * @should evaluate specified message resolvable * @should resolve message by code * @should resolve message in locale that different from default * @should return code if no message resolved * @should use body content as fallback if no message resolved * @should use text attribute as fallback if no message resolved * @should use body content in prior to text attribute as fallback if no message resolved * @should ignore fallbacks if tag locale differs from context locale */ @Override protected int doEndTagInternal() 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 = this.var; if (resolvedVar != null) { pageContext.setAttribute(resolvedVar, msg, TagUtils.getScope(this.scope)); } else { writeMessage(msg); } return EVAL_PAGE; } catch (NoSuchMessageException ex) { throw new JspTagException(getNoSuchMessageExceptionDescription(ex)); } }
@Override protected final int doStartTagInternal() throws JspException { if (this.value != 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(this.value); result = editor.getAsText(); } else { // Else, just do a toString. result = this.value.toString(); } result = htmlEscape(result); if (this.var != null) { pageContext.setAttribute(this.var, result, TagUtils.getScope(this.scope)); } else { try { // Else, just print it out. pageContext.getOut().print(result); } catch (IOException ex) { throw new JspException(ex); } } } return SKIP_BODY; }
@Test @Verifies(value = "resolve a Tag with var not set to null", method = "doEndTag()") public void doEndTag_shouldEvaluateVarIfIsNotNull() throws Exception { final String varName = "Mary"; final String expectedOutput = "had a little lamb"; openmrsMessageTag.setVar(varName); DefaultMessageSourceResolvable message = new DefaultMessageSourceResolvable(new String[] { "test" }, expectedOutput); openmrsMessageTag.setMessage(message); openmrsMessageTag.setScope(TagUtils.SCOPE_PAGE); checkDoEndTagEvaluationOfVar(varName, PageContext.PAGE_SCOPE, expectedOutput); }
/** * 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)); } }
private SelectTag getSelectTag() { TagUtils.assertHasAncestorOfType(this, SelectTag.class, "options", "select"); return (SelectTag) findAncestorWithClass(this, SelectTag.class); }
private void assertUnderSelectTag() { TagUtils.assertHasAncestorOfType(this, SelectTag.class, "option", "select"); }
@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 the scope to export the URL variable to. This attribute has no * meaning unless var is also defined. */ public void setScope(String scope) { this.scope = TagUtils.getScope(scope); }
/** * Set the scope to export the evaluation result to. * This attribute has no meaning unless var is also defined. */ public void setScope(String scope) { this.scope = TagUtils.getScope(scope); }