commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bjorn Bength <bjorn.ben...@curalia.se>
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
org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
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.

/bjorn



  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: " + 
xpath);
                 }
             }
             if (result instanceof NodePointer) {
                 result = ((NodePointer) result).getValuePointer();
                 if (!lenient && !((NodePointer) result).isActual()) {
                     // We need to differentiate between pointers 
representing
                     // a non-existing property and ones representing a 
property
                     // whose value is null.  In the latter case, the 
pointer
                     // is going to have isActual == false, but its parent,
                     // which is a non-node pointer identifying the bean 
property,
                     // will return isActual() == true.
                     NodePointer parent = ((NodePointer) 
result).getParent();
                     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());
                 }
             }
         }
     }



Mime
View raw message