Search in sources :

Example 31 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class JavascriptValidatorTag method createDynamicJavascript.

/**
     * Generates the dynamic JavaScript for the form.
     *
     * @param config
     * @param resources
     * @param locale
     * @param form
     */
private String createDynamicJavascript(ModuleConfig config, ValidatorResources resources, Locale locale, Form form) throws JspException {
    StringBuffer results = new StringBuffer();
    MessageResources messages = TagUtils.getInstance().retrieveMessageResources(pageContext, bundle, true);
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    ServletContext application = pageContext.getServletContext();
    List actions = this.createActionList(resources, form);
    final String methods = this.createMethods(actions, this.stopOnError(config));
    String formName = form.getName();
    jsFormName = formName;
    if (jsFormName.charAt(0) == '/') {
        String mappingName = TagUtils.getInstance().getActionMappingName(jsFormName);
        ActionMapping mapping = (ActionMapping) config.findActionConfig(mappingName);
        if (mapping == null) {
            JspException e = new JspException(messages.getMessage("formTag.mapping", mappingName));
            pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
            throw e;
        }
        jsFormName = mapping.getAttribute();
    }
    results.append(this.getJavascriptBegin(methods));
    for (Iterator i = actions.iterator(); i.hasNext(); ) {
        ValidatorAction va = (ValidatorAction) i.next();
        int jscriptVar = 0;
        String functionName = null;
        if ((va.getJsFunctionName() != null) && (va.getJsFunctionName().length() > 0)) {
            functionName = va.getJsFunctionName();
        } else {
            functionName = va.getName();
        }
        results.append("    function " + jsFormName + "_" + functionName + " () { \n");
        for (Iterator x = form.getFields().iterator(); x.hasNext(); ) {
            Field field = (Field) x.next();
            // retrieve from scope?))
            if (field.isIndexed() || (field.getPage() != page) || !field.isDependency(va.getName())) {
                continue;
            }
            String message = Resources.getMessage(application, request, messages, locale, va, field);
            message = (message != null) ? message : "";
            // prefix variable with 'a' to make it a legal identifier
            results.append("     this.a" + jscriptVar++ + " = new Array(\"" + field.getKey() + "\", \"" + escapeQuotes(message) + "\", ");
            results.append("new Function (\"varName\", \"");
            Map vars = field.getVars();
            // Loop through the field's variables.
            Iterator varsIterator = vars.keySet().iterator();
            while (varsIterator.hasNext()) {
                String varName = (String) varsIterator.next();
                Var var = (Var) vars.get(varName);
                String varValue = Resources.getVarValue(var, application, request, false);
                String jsType = var.getJsType();
                // fieldValue
                if (varName.startsWith("field")) {
                    continue;
                }
                String varValueEscaped = escapeJavascript(varValue);
                if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=" + varValueEscaped + "; ");
                } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                // So everyone using the latest format doesn't need to
                // change their xml files immediately.
                } else if ("mask".equalsIgnoreCase(varName)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                }
            }
            results.append(" return this[varName];\"));\n");
        }
        results.append("    } \n\n");
    }
    return results.toString();
}
Also used : ValidatorAction(org.apache.commons.validator.ValidatorAction) MessageResources(org.apache.struts.util.MessageResources) Var(org.apache.commons.validator.Var) HttpServletRequest(javax.servlet.http.HttpServletRequest) JspException(javax.servlet.jsp.JspException) Field(org.apache.commons.validator.Field) ActionMapping(org.apache.struts.action.ActionMapping) Iterator(java.util.Iterator) ServletContext(javax.servlet.ServletContext) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 32 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class MessagesTag method doStartTag.

/**
     * Construct an iterator for the specified collection, and begin looping
     * through the body once per element.
     *
     * @throws JspException if a JSP exception has occurred
     */
public int doStartTag() throws JspException {
    // Initialize for a new request.
    processed = false;
    // Were any messages specified?
    ActionMessages messages = null;
    // Make a local copy of the name attribute that we can modify.
    String name = this.name;
    if ((message != null) && "true".equalsIgnoreCase(message)) {
        name = Globals.MESSAGE_KEY;
    }
    try {
        messages = TagUtils.getInstance().getActionMessages(pageContext, name);
    } catch (JspException e) {
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    // Acquire the collection we are going to iterate over
    this.iterator = (property == null) ? messages.get() : messages.get(property);
    // Store the first value and evaluate, or skip the body if none
    if (!this.iterator.hasNext()) {
        return SKIP_BODY;
    }
    // process the first message
    processMessage((ActionMessage) iterator.next());
    if ((header != null) && (header.length() > 0)) {
        String headerMessage = TagUtils.getInstance().message(pageContext, bundle, locale, header);
        if (headerMessage != null) {
            TagUtils.getInstance().write(pageContext, headerMessage);
        }
    }
    // Set the processed variable to true so the
    // doEndTag() knows processing took place
    processed = true;
    return (EVAL_BODY_TAG);
}
Also used : JspException(javax.servlet.jsp.JspException) ActionMessages(org.apache.struts.action.ActionMessages)

Example 33 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class MultiboxTag method prepareValue.

/**
     * Render the value element
     *
     * @param results The StringBuffer that output will be appended to.
     */
protected String prepareValue(StringBuffer results) throws JspException {
    String value = (this.value == null) ? this.constant : this.value;
    if (value == null) {
        JspException e = new JspException(messages.getMessage("multiboxTag.value"));
        pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
        throw e;
    }
    prepareAttribute(results, "value", TagUtils.getInstance().filter(value));
    return value;
}
Also used : JspException(javax.servlet.jsp.JspException)

Example 34 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class BaseHandlerTag method message.

// ------------------------------------------------------ Protected Methods
/**
     * Return the text specified by the literal value or the message resources
     * key, if any; otherwise return <code>null</code>.
     *
     * @param literal Literal text value or <code>null</code>
     * @param key     Message resources key or <code>null</code>
     * @throws JspException if both arguments are non-null
     */
protected String message(String literal, String key) throws JspException {
    if (literal != null) {
        if (key != null) {
            JspException e = new JspException(messages.getMessage("common.both"));
            TagUtils.getInstance().saveException(pageContext, e);
            throw e;
        } else {
            return (literal);
        }
    } else {
        if (key != null) {
            return TagUtils.getInstance().message(pageContext, getBundle(), getLocale(), key);
        } else {
            return null;
        }
    }
}
Also used : JspException(javax.servlet.jsp.JspException)

Example 35 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class ErrorsTag method doStartTag.

// ------------------------------------------------------- Public Methods
/**
     * Render the specified error messages if there are any.
     *
     * @throws JspException if a JSP exception has occurred
     */
public int doStartTag() throws JspException {
    // Were any error messages specified?
    ActionMessages errors = null;
    try {
        errors = TagUtils.getInstance().getActionMessages(pageContext, name);
    } catch (JspException e) {
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    if ((errors == null) || errors.isEmpty()) {
        return (EVAL_BODY_INCLUDE);
    }
    boolean headerPresent = TagUtils.getInstance().present(pageContext, bundle, locale, getHeader());
    boolean footerPresent = TagUtils.getInstance().present(pageContext, bundle, locale, getFooter());
    boolean prefixPresent = TagUtils.getInstance().present(pageContext, bundle, locale, getPrefix());
    boolean suffixPresent = TagUtils.getInstance().present(pageContext, bundle, locale, getSuffix());
    // Render the error messages appropriately
    StringBuffer results = new StringBuffer();
    boolean headerDone = false;
    String message = null;
    Iterator reports = (property == null) ? errors.get() : errors.get(property);
    while (reports.hasNext()) {
        ActionMessage report = (ActionMessage) reports.next();
        if (!headerDone) {
            if (headerPresent) {
                message = TagUtils.getInstance().message(pageContext, bundle, locale, getHeader());
                results.append(message);
            }
            headerDone = true;
        }
        if (prefixPresent) {
            message = TagUtils.getInstance().message(pageContext, bundle, locale, getPrefix());
            results.append(message);
        }
        if (report.isResource()) {
            message = TagUtils.getInstance().message(pageContext, bundle, locale, report.getKey(), report.getValues());
        } else {
            message = report.getKey();
        }
        if (message != null) {
            results.append(message);
        }
        if (suffixPresent) {
            message = TagUtils.getInstance().message(pageContext, bundle, locale, getSuffix());
            results.append(message);
        }
    }
    if (headerDone && footerPresent) {
        message = TagUtils.getInstance().message(pageContext, bundle, locale, getFooter());
        results.append(message);
    }
    TagUtils.getInstance().write(pageContext, results.toString());
    return (EVAL_BODY_INCLUDE);
}
Also used : JspException(javax.servlet.jsp.JspException) ActionMessages(org.apache.struts.action.ActionMessages) Iterator(java.util.Iterator) ActionMessage(org.apache.struts.action.ActionMessage)

Aggregations

JspException (javax.servlet.jsp.JspException)158 IOException (java.io.IOException)34 Map (java.util.Map)30 HashMap (java.util.HashMap)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)21 JspWriter (javax.servlet.jsp.JspWriter)21 Iterator (java.util.Iterator)20 ActionMessages (org.apache.struts.action.ActionMessages)15 MockFormBean (org.apache.struts.mock.MockFormBean)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)11 ActionMessage (org.apache.struts.action.ActionMessage)11 ArrayList (java.util.ArrayList)7 Collection (java.util.Collection)7 List (java.util.List)7 Locale (java.util.Locale)6 PageExpiredException (org.mifos.framework.exceptions.PageExpiredException)6 MalformedURLException (java.net.MalformedURLException)5 ModuleConfig (org.apache.struts.config.ModuleConfig)5 UserContext (org.mifos.security.util.UserContext)5 SimpleDateFormat (java.text.SimpleDateFormat)4