You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Henri Biestro (JIRA)" <ji...@apache.org> on 2015/07/11 12:32:04 UTC

[jira] [Commented] (JEXL-162) empty() function throws an exception : unsupported type

    [ https://issues.apache.org/jira/browse/JEXL-162?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14623341#comment-14623341 ] 

Henri Biestro commented on JEXL-162:
------------------------------------

The documented behavior is not what you expect.
It actually states : Returns *true* if the expression following is either...
The rationale is that 'emptiness' must have a meaning for the object considered, a callable method implementation.
I agree though that a _lenient_ arithmetic should return 'false' (and 'size' should return 0) rather than generate an error.

You can implement your behavior by subclassing JexlArithmetic and overload the 'empty' method as in the following where an 'empty' node is a node without attributes nor children.
{code}

    public static class XmlArithmetic extends JexlArithmetic {
        public XmlArithmetic(boolean lenient) {
            super(lenient);
        }
        public boolean empty(org.w3c.dom.Element elt) {
            return !elt.hasAttributes() && !elt.hasChildNodes();
        }
    }

    @Test
    public void test162() throws Exception {
        JexlEngine jexl = new JexlBuilder().arithmetic(new XmlArithmetic(false)).create();
        JexlScript s0 = jexl.createScript("empty(x)", "x");
        Document xml;
        Node x;
        Boolean r;
        xml = getDocument("<node info='123'/>");
        x = xml.getLastChild();
        r = (Boolean) s0.execute(null, x);
        Assert.assertFalse(r);
        xml = getDocument("<node>some content</node>");
        x = xml.getLastChild();
        r = (Boolean) s0.execute(null, x);
        Assert.assertFalse(r);
        xml = getDocument("<node/>");
        x = xml.getLastChild();
        r = (Boolean) s0.execute(null, x);
        Assert.assertTrue(r);
    }
        
    private static Document getDocument(String xml) throws IOException, SAXException, ParserConfigurationException {
        DocumentBuilder xmlBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputStream stringInputStream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
        return xmlBuilder.parse(stringInputStream);
    }
{code}

> empty() function throws an exception : unsupported type
> -------------------------------------------------------
>
>                 Key: JEXL-162
>                 URL: https://issues.apache.org/jira/browse/JEXL-162
>             Project: Commons JEXL
>          Issue Type: Bug
>    Affects Versions: 3.0
>            Reporter: Dmitri Blinov
>
> empty() : unsupported type : class com.sun.org.apache.xerces.internal.dom.ElementImpl.
> Exepected behaviour - just return false if neither:
>     Object is null
>     Object is an empty string
>     Object is an array of length zero
>     Object is a collection of size zero
>     Object is an empty map
> .....



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)