Search in sources :

Example 6 with JspException

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

the class OptionsTag method doEndTag.

/**
     * Process the end of this tag.
     *
     * @throws JspException if a JSP exception has occurred
     */
public int doEndTag() throws JspException {
    // Acquire the select tag we are associated with
    SelectTag selectTag = (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);
    if (selectTag == null) {
        throw new JspException(messages.getMessage("optionsTag.select"));
    }
    StringBuffer sb = new StringBuffer();
    // If a collection was specified, use that mode to render options
    if (collection != null) {
        Iterator collIterator = getIterator(collection, null);
        while (collIterator.hasNext()) {
            Object bean = collIterator.next();
            Object value = null;
            Object label = null;
            try {
                value = PropertyUtils.getProperty(bean, property);
                if (value == null) {
                    value = "";
                }
            } catch (IllegalAccessException e) {
                throw new JspException(messages.getMessage("getter.access", property, collection));
            } catch (InvocationTargetException e) {
                Throwable t = e.getTargetException();
                throw new JspException(messages.getMessage("getter.result", property, t.toString()));
            } catch (NoSuchMethodException e) {
                throw new JspException(messages.getMessage("getter.method", property, collection));
            }
            try {
                if (labelProperty != null) {
                    label = PropertyUtils.getProperty(bean, labelProperty);
                } else {
                    label = value;
                }
                if (label == null) {
                    label = "";
                }
            } catch (IllegalAccessException e) {
                throw new JspException(messages.getMessage("getter.access", labelProperty, collection));
            } catch (InvocationTargetException e) {
                Throwable t = e.getTargetException();
                throw new JspException(messages.getMessage("getter.result", labelProperty, t.toString()));
            } catch (NoSuchMethodException e) {
                throw new JspException(messages.getMessage("getter.method", labelProperty, collection));
            }
            String stringValue = value.toString();
            addOption(sb, stringValue, label.toString(), selectTag.isMatched(stringValue));
        }
    } else // Otherwise, use the separate iterators mode to render options
    {
        // Construct iterators for the values and labels collections
        Iterator valuesIterator = getIterator(name, property);
        Iterator labelsIterator = null;
        if ((labelName != null) || (labelProperty != null)) {
            labelsIterator = getIterator(labelName, labelProperty);
        }
        // Render the options tags for each element of the values coll.
        while (valuesIterator.hasNext()) {
            Object valueObject = valuesIterator.next();
            if (valueObject == null) {
                valueObject = "";
            }
            String value = valueObject.toString();
            String label = value;
            if ((labelsIterator != null) && labelsIterator.hasNext()) {
                Object labelObject = labelsIterator.next();
                if (labelObject == null) {
                    labelObject = "";
                }
                label = labelObject.toString();
            }
            addOption(sb, value, label, selectTag.isMatched(value));
        }
    }
    TagUtils.getInstance().write(pageContext, sb.toString());
    return EVAL_PAGE;
}
Also used : JspException(javax.servlet.jsp.JspException) Iterator(java.util.Iterator) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with JspException

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

the class OptionsTag method getIterator.

/**
     * Return an iterator for the option labels or values, based on our
     * configured properties.
     *
     * @param name     Name of the bean attribute (if any)
     * @param property Name of the bean property (if any)
     * @throws JspException if an error occurs
     */
protected Iterator getIterator(String name, String property) throws JspException {
    // Identify the bean containing our collection
    String beanName = name;
    if (beanName == null) {
        beanName = Constants.BEAN_KEY;
    }
    Object bean = TagUtils.getInstance().lookup(pageContext, beanName, null);
    if (bean == null) {
        throw new JspException(messages.getMessage("getter.bean", beanName));
    }
    // Identify the collection itself
    Object collection = bean;
    if (property != null) {
        try {
            collection = PropertyUtils.getProperty(bean, property);
            if (collection == null) {
                throw new JspException(messages.getMessage("getter.property", property));
            }
        } catch (IllegalAccessException e) {
            throw new JspException(messages.getMessage("getter.access", property, name));
        } catch (InvocationTargetException e) {
            Throwable t = e.getTargetException();
            throw new JspException(messages.getMessage("getter.result", property, t.toString()));
        } catch (NoSuchMethodException e) {
            throw new JspException(messages.getMessage("getter.method", property, name));
        }
    }
    // Construct and return an appropriate iterator
    if (collection.getClass().isArray()) {
        collection = Arrays.asList((Object[]) collection);
    }
    if (collection instanceof Collection) {
        return (((Collection) collection).iterator());
    } else if (collection instanceof Iterator) {
        return ((Iterator) collection);
    } else if (collection instanceof Map) {
        return (((Map) collection).entrySet().iterator());
    } else if (collection instanceof Enumeration) {
        return new IteratorAdapter((Enumeration) collection);
    } else {
        throw new JspException(messages.getMessage("optionsTag.iterator", collection.toString()));
    }
}
Also used : JspException(javax.servlet.jsp.JspException) Enumeration(java.util.Enumeration) IteratorAdapter(org.apache.struts.util.IteratorAdapter) Iterator(java.util.Iterator) Collection(java.util.Collection) Map(java.util.Map) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 8 with JspException

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

the class SelectTag method calculateMatchValues.

/**
     * Calculate the match values we will actually be using.
     *
     * @throws JspException
     */
private void calculateMatchValues() throws JspException {
    if (this.value != null) {
        this.match = new String[1];
        this.match[0] = this.value;
    } else {
        Object bean = TagUtils.getInstance().lookup(pageContext, name, null);
        if (bean == null) {
            JspException e = new JspException(messages.getMessage("getter.bean", name));
            TagUtils.getInstance().saveException(pageContext, e);
            throw e;
        }
        try {
            this.match = BeanUtils.getArrayProperty(bean, property);
            if (this.match == null) {
                this.match = new String[0];
            }
        } catch (IllegalAccessException e) {
            TagUtils.getInstance().saveException(pageContext, e);
            throw new JspException(messages.getMessage("getter.access", property, name));
        } catch (InvocationTargetException e) {
            Throwable t = e.getTargetException();
            TagUtils.getInstance().saveException(pageContext, t);
            throw new JspException(messages.getMessage("getter.result", property, t.toString()));
        } catch (NoSuchMethodException e) {
            TagUtils.getInstance().saveException(pageContext, e);
            throw new JspException(messages.getMessage("getter.method", property, name));
        }
    }
}
Also used : JspException(javax.servlet.jsp.JspException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 9 with JspException

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

the class CompareTagBase method condition.

/**
     * Evaluate the condition that is being tested by this particular tag, and
     * return <code>true</code> if the nested body content of this tag should
     * be evaluated, or <code>false</code> if it should be skipped. This
     * method must be implemented by concrete subclasses.
     *
     * @param desired1 First desired value for a true result (-1, 0, +1)
     * @param desired2 Second desired value for a true result (-1, 0, +1)
     * @throws JspException if a JSP exception occurs
     */
protected boolean condition(int desired1, int desired2) throws JspException {
    // Acquire the value and determine the test type
    int type = -1;
    double doubleValue = 0.0;
    long longValue = 0;
    if ((type < 0) && (value.length() > 0)) {
        try {
            doubleValue = Double.parseDouble(value);
            type = DOUBLE_COMPARE;
        } catch (NumberFormatException e) {
            ;
        }
    }
    if ((type < 0) && (value.length() > 0)) {
        try {
            longValue = Long.parseLong(value);
            type = LONG_COMPARE;
        } catch (NumberFormatException e) {
            ;
        }
    }
    if (type < 0) {
        type = STRING_COMPARE;
    }
    // Acquire the unconverted variable value
    Object variable = null;
    if (cookie != null) {
        Cookie[] cookies = ((HttpServletRequest) pageContext.getRequest()).getCookies();
        if (cookies == null) {
            cookies = new Cookie[0];
        }
        for (int i = 0; i < cookies.length; i++) {
            if (cookie.equals(cookies[i].getName())) {
                variable = cookies[i].getValue();
                break;
            }
        }
    } else if (header != null) {
        variable = ((HttpServletRequest) pageContext.getRequest()).getHeader(header);
    } else if (name != null) {
        Object bean = TagUtils.getInstance().lookup(pageContext, name, scope);
        if (property != null) {
            if (bean == null) {
                JspException e = new JspException(messages.getMessage("logic.bean", name));
                TagUtils.getInstance().saveException(pageContext, e);
                throw e;
            }
            try {
                variable = PropertyUtils.getProperty(bean, property);
            } catch (InvocationTargetException e) {
                Throwable t = e.getTargetException();
                if (t == null) {
                    t = e;
                }
                TagUtils.getInstance().saveException(pageContext, t);
                throw new JspException(messages.getMessage("logic.property", name, property, t.toString()));
            } catch (Throwable t) {
                TagUtils.getInstance().saveException(pageContext, t);
                throw new JspException(messages.getMessage("logic.property", name, property, t.toString()));
            }
        } else {
            variable = bean;
        }
    } else if (parameter != null) {
        variable = pageContext.getRequest().getParameter(parameter);
    } else {
        JspException e = new JspException(messages.getMessage("logic.selector"));
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    if (variable == null) {
        // Coerce null to a zero-length String
        variable = "";
    }
    // Perform the appropriate comparison
    int result = 0;
    if (type == DOUBLE_COMPARE) {
        try {
            double doubleVariable = Double.parseDouble(variable.toString());
            if (doubleVariable < doubleValue) {
                result = -1;
            } else if (doubleVariable > doubleValue) {
                result = +1;
            }
        } catch (NumberFormatException e) {
            result = variable.toString().compareTo(value);
        }
    } else if (type == LONG_COMPARE) {
        try {
            long longVariable = Long.parseLong(variable.toString());
            if (longVariable < longValue) {
                result = -1;
            } else if (longVariable > longValue) {
                result = +1;
            }
        } catch (NumberFormatException e) {
            result = variable.toString().compareTo(value);
        }
    } else {
        result = variable.toString().compareTo(value);
    }
    // Normalize the result
    if (result < 0) {
        result = -1;
    } else if (result > 0) {
        result = +1;
    }
    // Return true if the result matches either desired value
    return ((result == desired1) || (result == desired2));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) JspException(javax.servlet.jsp.JspException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with JspException

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

the class EmptyTag method condition.

/**
     * Evaluate the condition that is being tested by this particular tag, and
     * return <code>true</code> if the nested body content of this tag should
     * be evaluated, or <code>false</code> if it should be skipped. This
     * method must be implemented by concrete subclasses.
     *
     * @param desired Desired outcome for a true result
     * @throws JspException if a JSP exception occurs
     */
protected boolean condition(boolean desired) throws JspException {
    if (this.name == null) {
        JspException e = new JspException(messages.getMessage("empty.noNameAttribute"));
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    Object value = null;
    if (this.property == null) {
        value = TagUtils.getInstance().lookup(pageContext, name, scope);
    } else {
        value = TagUtils.getInstance().lookup(pageContext, name, property, scope);
    }
    boolean empty = true;
    if (value == null) {
        empty = true;
    } else if (value instanceof String) {
        String strValue = (String) value;
        empty = (strValue.length() < 1);
    } else if (value instanceof Collection) {
        Collection collValue = (Collection) value;
        empty = collValue.isEmpty();
    } else if (value instanceof Map) {
        Map mapValue = (Map) value;
        empty = mapValue.isEmpty();
    } else if (value.getClass().isArray()) {
        empty = Array.getLength(value) == 0;
    } else {
        empty = false;
    }
    return (empty == desired);
}
Also used : JspException(javax.servlet.jsp.JspException) Collection(java.util.Collection) Map(java.util.Map)

Aggregations

JspException (javax.servlet.jsp.JspException)151 IOException (java.io.IOException)34 Map (java.util.Map)30 HashMap (java.util.HashMap)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)21 Iterator (java.util.Iterator)20 JspWriter (javax.servlet.jsp.JspWriter)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 PageExpiredException (org.mifos.framework.exceptions.PageExpiredException)6 MalformedURLException (java.net.MalformedURLException)5 Locale (java.util.Locale)5 ModuleConfig (org.apache.struts.config.ModuleConfig)5 UserContext (org.mifos.security.util.UserContext)5 JspTag (javax.servlet.jsp.tagext.JspTag)4