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 Thu, 23 Jan 2003 00:40:34 GMT
Bjorn,

I don't really want to have a catch-all in getValue, even in the
lenient mode. 

When we allowed exceptions during XPath evaluation, the purpose was not
to conceal all problems, but to ignore <i>irrelevant</i> problems. 

Here's my logic:

1. When you call getValue("/foo/bar"), the implication is: "I know the
exact path and I want the value that's sitting at the tip of that path.
If the value is null or some parts of the path are missing, that's ok,
that's why I switched to the lenient mode. However, if an exception is
thrown while evaluating the path, something went wrong. I should have a
way of tracing the problem in order to fix it.  This is why I want a
descriptive exception thrown."

2. When you call iterate("foo//bar"), the implication is: "I am not
sure there is anything for foo//bar, but I want to perform the search
for it and get all matches.  As I search for it I will be calling many
methods that have nothing to do with either foo or bar. I don't care if
during the search some of those methods, e.g. getBadProperty() or
getMadAndExplode() throw exceptions, because those properties are
irrelevant for my search."

This is why in the current cut of JXPath, getValue() throws an
exception, while iterate() does not.

Does this logic make sense?

- Dmitri

--- Bjorn Bength <bjorn.bength@curalia.se> wrote:
> Dmitri,
> 
> I will send you fresh stacktraces later,
> but what is the main issue with a completely fail-safe, fool-proof 
> approach as my example that catches everything?
> Well, okay, besides having to create a an exception context each and 
> every access...  but as it is now, I have to do that outside of 
> getValue() anyhow.
> 
> We're using jxpath in a cocoon environment together with OJB
> and OJB uses introspection with reflection and lazy db queries
> to travers the object graphs.
> 
> btw, thanks for an otherwise excellent package :)
> 
> Björn
> 
> 
> 
> 
> Dmitri Plotnikov wrote:
> > 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
> > 
> > --
> > To unsubscribe, e-mail:  
> <mailto:commons-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> <mailto:commons-user-help@jakarta.apache.org>
> 
> 
> 
> --
> 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