commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Neil Benn" <neil.b...@gmail.com>
Subject Re: Calling a method with JXPath
Date Mon, 05 Mar 2007 19:21:43 GMT
Hello,

         Thanks for the reply, I tested that as well with the following code
(sorry about the largish amount of code):
---
*

package* com.ziath.eventregistration.email.test;

*

import* java.util.ArrayList;

*import* java.util.List;

*

import* org.apache.commons.jxpath.JXPathContext;

*

import* junit.framework.TestCase;

*

public* *class* JXPathTestCase *extends* *TestCase* {

 *private *List<Person> objTestList = *null*;

*public* *void* setUp(){

objTestList = *new* ArrayList<Person>();

Person objPerson1 = *new* Person("Neil", "Dave", "Benn");

Person objPerson2 = *new* Person("John", "Bob", "Smith");

objTestList.add(objPerson1);

objTestList.add(objPerson2);

}

*public* *void* *testJXPathDirectCall*(){

JXPathContext objContext = JXPathContext.*newContext*(objTestList);

System.*out*.println(objContext.getValue(".[1]/getName(.[1], 1)"));

}

*public* *void* testJXPathRelativeMethodCall(){

JXPathContext objContext = JXPathContext.*newContext*(objTestList);

System.*out*.println(objContext.getRelativeContext(

objContext.getPointer(".[1]"))

.getValue("getName(., 1)"));

}

*private* *static* *class* Person{

*private* List<String> lstNames = *null*;

/**

* *@param* strFirstName

* *@param* *strMiddleName*

* *@param* *strLastName*

*/

*public* Person(String strFirstName, String strMiddleName, String
strLastName) {

*super*();

lstNames = *new* ArrayList<String>();

lstNames.add(strFirstName);

lstNames.add(strMiddleName);

*lstNames*.add(strLastName);

}

*public* *synchronized* *final* String getName(*int* piPosition){

*return* lstNames.get(piPosition);

}

*public* String toString(){

*return* lstNames.get(0) + " " +

lstNames.get(1) + " " +

lstNames.get(2) + " ";

}

}

}
---
However that givesme a traceback of :


org.apache.commons.jxpath.JXPathException: Cannot invoke null;
java.lang.NullPointerException
 at org.apache.commons.jxpath.functions.MethodFunction.invoke(
MethodFunction.java:94)
 at org.apache.commons.jxpath.ri.compiler.ExtensionFunction.computeValue(
ExtensionFunction.java:92)
 at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(
JXPathContextReferenceImpl.java:314)
 at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(
JXPathContextReferenceImpl.java:280)
 at
com.ziath.eventregistration.email.test.JXPathTestCase.testJXPathDirectCall(
JXPathTestCase.java:24)

The thing is that I'm searching and searching for an example of how to do
this but not getting anywhere.  Standard XPath (from W3C) notation (as far
as I can find)  doesn't have this notion, the JXPath docs have an example
usaing a varibale but no example of how this variable is declared.  The
other thing I've tried (as you can see above) is to get a pointer and use
this to make a relative context  and call 'getPerson(., 1)' but that doesn't
work either.

  Errm, I gonna try the nightly build to see if that has anything in it.
Any suggestions would be greatly appreciated!

Cheers,



Neil



On 3/5/07, Dmitri Plotnikov <dmitri@apache.org> wrote:
>
> Neil,
>
> In any case, please make sure that the syntax is right.  You cannot use a
> method as a step in the path.  It must be called as a function:
>
> "getQuestion(.[1], 'lastname')"
>
> Regards,
>
> - Dmitri
>
>
> ----- Original Message -----
> From: "Scott Heaberlin" <heabdogg@gmail.com>
> To: "Jakarta Commons Users List" <commons-user@jakarta.apache.org>
> Sent: Sunday, March 04, 2007 9:05 PM
> Subject: Re: Calling a method with JXPath
>
>
> Odd.  I know I've used instance methods in JXPath before.  But I can't
> say I tried to do so where the instance method was on a member item of
> a collection.
>
> One thing I can say is that I've had difficulties in the past with the
> 1.2 release where my object graphs contain a mixture of maps,
> collections, and pojos.
>
> Suggestion - try your test case with one of the JXPath nightly builds.
> I had to do that because some fixes for traversing collections of
> collections which contain maps, etc.  The 1.2 release is quite old.
>
> -Scott Heaberlin
>
> On 3/4/07, Neil Benn <neil.benn@ziath.com> wrote:
> > Hello,
> >
> >       Thanks for the tip - I tried that but unfortunatly it does not
> work,
> > I
> > still get the same problem.  The stack trace is now:
> >
> > ---
> >
> > org.apache.commons.jxpath.JXPathException: Invalid XPath:
> > '.[1]/getQuestion(.[1], \'lastname\')'. Syntax error after: '.[1]/g'
> >
> > org.apache.commons.jxpath.ri.Parser.parseExpression(Parser.java:60)
> >
> org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.compileExpression(
> JXPathContextReferenceImpl.java:218)
> > org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(
> JXPathContextReferenceImpl.java:247)
> >
> > ---
> >
> >   It is the same problem.  Interestingly I did try putting a list into
> the
> > JXContext and calling size as follows:
> >
> > List objValues = new ArrayList<String>();
> > objValues.add("test1");
> > objValues.add("test2");
> > JXPathContext objContext = JXPathContext.newContext(objValues);
> > System.out.println(objContext.getValue("size(.)"));
> > assertEquals("test1", objContext.getValue(".[1]"));
> >
> >   This works, it prints out 2 for the size of the list.  I wonder if it
> is
> > because the method I am calling has parameters?  In the meantime, I'm
> > gonna
> > trawl trhough the testcases in the source to see if I can find an
> example.
> >
> >   Thanks for your assistance.
> >
> > Cheers,
> >
> > Neil
> >
> > "Scott Heaberlin" <heabdogg@gmail.com> wrote:
> >
> > > Try passing the object on which you want to call the method as the
> > > first arg in the xpath function (the object's method).
> > >
> > > .[1]/getQuestion(.[1], 'lastname')
> > >
> > > Object methods take the syntax above as part of JXPath's standard
> > > extension functions.
> > >
> > >
> >
> http://jakarta.apache.org/commons/jxpath/users-guide.html#Standard_Extension_Functions
> > >
> > > Hope this helps,
> > >
> > > -Scott Heaberlin
> > >
> > > On 3/3/07, Neil Benn <neil.benn@ziath.com> wrote:
> > > > Hello,
> > > >
> > > >
> > > >
> > > >             I have a a strane problem when I'm trying to call a
> method
> > with
> > > > JXPath.  The object graph has a user which has a method called
> > > > getQuestion(String pstrQuestionID).  The JXPath I have written for
> > > > this
> > is:
> > > >
> > > >
> > > >
> > > > .[1]/getQuestion(\'lastname\')
> > > >
> > > >
> > > >             The dot at the front is notation to allow me to access
> the
> > first
> > > > element, the root of the context is a list.  However when I attempt
> to
> > > > run
> > > > this I get the following stack trace :
> > > >
> > > >
> > > >
> > > > org.apache.commons.jxpath.JXPathException: Invalid XPath:
> > > > '.[1]/getQuestion(\'lastname\')'. Syntax error after: '.[1]/g'
> > > > org.apache.commons.jxpath.ri.Parser.parseExpression(Parser.java:60)
> > > >
> > >
> >
> org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.compileExpression(
> JXPathContextReferenceImpl.java:218)
> > > >
> > >
> > org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(
> JXPathContextReferenceImpl.java:247)
> > > >
> > > >
> > > >             However as far as I can see, the XPath notation is valid
> –
> > > > I
> > > would
> > > > be most grateful is someone could let me know what I've done wrong.
> > > >
> > > >
> > > >
> > > >             Thanks, in advance for your help.
> > > >
> > > >
> > > >
> > > > Cheers,
> > > >
> > > >
> > > >
> > > > Neil
> > > >
> > > >
> > > >
> > > > ---
> > > >
> > > > Neil Benn Msc
> > > >
> > > > CEO Ziath Ltd
> > > >
> > > > Website - http://www.ziath.com
> > > >
> > > > Blog - http://labauto-lounge.com
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > > > For additional commands, e-mail:
> commons-user-help@jakarta.apache.org
> > > >
> > > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> > >
> > >
> > ---
> >
> > Neil Benn Msc
> > CEO Ziath Ltd
> > Website - http://www.ziath.com
> > Blog - http://labauto-lounge.com
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message