commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dmitri Plotnikov <dplot...@yahoo.com>
Subject Re: [JXPath] Lenient getValue() in JXPathContextReferenceImpl
Date Tue, 21 Jan 2003 20:08:34 GMT
Bjorn,

There is code in JXPath that is supposed to take care of this issue.
For some reason it is not working for you.

Could you do me a favor: upgrade to the latest build, run the test and
send me the stack trace for the exception thrown by the accessor.  I
should be able to reproduce the problem and then fix it based on the
stack trace.

Thank you,

- Dmitri


--- Bjorn Bength <bjorn.bength@curalia.se> wrote:
> 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());
>                  }
>              }
>          }
>      }
> 
> 
> 
> --
> To unsubscribe, e-mail:  
> <mailto:commons-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:commons-user-help@jakarta.apache.org>
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

Mime
View raw message