Search in sources :

Example 1 with ArrayErrorListener

use of cfml.parsing.reporting.ArrayErrorListener in project CFLint by cflint.

the class CFLint method unpackTagExpressions.

private Map<String, CFExpression> unpackTagExpressions(final Element elem) {
    // Use LinkedHashMap to preserve the order
    final Map<String, CFExpression> expressions = new LinkedHashMap<>();
    if (!elem.getName().toLowerCase().startsWith("cf") || elem.getAttributes() == null) {
        return expressions;
    }
    // variable attribute
    for (final Attribute attr : elem.getAttributes()) {
        if (detectScript(attr)) {
            // Try wrapping the expression in single or double quotes for
            // parsing.
            final List<String> literalChar = attr.getValue().contains("'") ? Arrays.asList("\"", "'") : Arrays.asList("'", "\"");
            try {
                final List<String> errors = new ArrayList<>();
                final ANTLRErrorListener errorReporter = new ArrayErrorListener(errors);
                final CFExpression exp = cfmlParser.parseCFMLExpression(literalChar.get(0) + attr.getValue() + literalChar.get(0), errorReporter);
                if (errors.isEmpty()) {
                    expressions.put(attr.getName().toLowerCase(), exp);
                    continue;
                }
            } catch (final Exception e) {
            }
            // Try other quotes before reporting a failure
            try {
                final CFExpression exp = cfmlParser.parseCFMLExpression(literalChar.get(1) + attr.getValue() + literalChar.get(1), this);
                expressions.put(attr.getName().toLowerCase(), exp);
            } catch (final Exception e2) {
                System.err.println("Error in parsing : " + attr.getValue() + " on tag " + elem.getName());
            }
        } else if (tagInfo.isExpressionAttribute(elem, attr.getName())) {
            try {
                final CFExpression exp = cfmlParser.parseCFMLExpression(attr.getValue(), this);
                expressions.put(attr.getName().toLowerCase(), exp);
            } catch (final Exception e2) {
                System.err.println("Error in parsing : " + attr.getValue() + " on tag " + elem.getName());
            }
        }
    }
    return expressions;
}
Also used : Attribute(net.htmlparser.jericho.Attribute) ArrayList(java.util.ArrayList) ArrayErrorListener(cfml.parsing.reporting.ArrayErrorListener) ANTLRErrorListener(org.antlr.v4.runtime.ANTLRErrorListener) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException) CFLintScanException(com.cflint.exception.CFLintScanException) CFExpression(cfml.parsing.cfscript.CFExpression) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

CFExpression (cfml.parsing.cfscript.CFExpression)1 ArrayErrorListener (cfml.parsing.reporting.ArrayErrorListener)1 ParseException (cfml.parsing.reporting.ParseException)1 CFLintScanException (com.cflint.exception.CFLintScanException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Attribute (net.htmlparser.jericho.Attribute)1 ANTLRErrorListener (org.antlr.v4.runtime.ANTLRErrorListener)1