commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bjorn Bength <>
Subject [JXPath] Lenient getValue() in JXPathContextReferenceImpl
Date Tue, 21 Jan 2003 16:43:45 GMT
We have a problem with getValue() and the lenient setting.
in our app we cant allow any exceptions thrown by this method,
hence we used setLenient(true);
However, our bean objects can throw many different kinds of exceptions
while traversing getters (attributes), such as nullpointer and 
reflection exceptions.
If we patch the getValue() method in file
to catch all exceptions like so (see below), it all works okay for us.
Probably we would want to patch the other getValue() method
and iterate() methods, too.

Note, i only tested this on a nightly build from about 2-3 weeks ago.
maybe there already is a better solution to this, but I really wish the 
lenient setting be respected.


  public Object getValue(String xpath, Expression expr){
         try {
             Object result = expr.computeValue(getRootContext());
             if (result instanceof EvalContext) {
                 EvalContext ctx = (EvalContext) result;
                 result = ctx.getSingleNodePointer();
                 if (!lenient && result == null) {
                     throw new JXPathException("No value for xpath: " + 
             if (result instanceof NodePointer) {
                 result = ((NodePointer) result).getValuePointer();
                 if (!lenient && !((NodePointer) result).isActual()) {
                     // We need to differentiate between pointers 
                     // a non-existing property and ones representing a 
                     // whose value is null.  In the latter case, the 
                     // is going to have isActual == false, but its parent,
                     // which is a non-node pointer identifying the bean 
                     // will return isActual() == true.
                     NodePointer parent = ((NodePointer) 
                     if (parent == null
                         || !parent.isContainer()
                         || !parent.isActual()) {
                         throw new JXPathException("No value for xpath: 
" + xpath);
                 result = ((NodePointer) result).getValue();
             return result;

         } catch (Throwable t) {
             if (lenient) {
                 return null;
             } else {
                 if (t instanceof JXPathException) {
                     throw (JXPathException)t;
                 } else {
                     throw new JXPathException("Error getting value for 
xpath: " + xpath +
                                               ": " + t.toString());

View raw message