Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 66458 invoked from network); 7 Feb 2003 10:14:04 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 7 Feb 2003 10:14:04 -0000 Received: (qmail 28456 invoked by uid 97); 7 Feb 2003 10:15:44 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 28449 invoked from network); 7 Feb 2003 10:15:44 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 7 Feb 2003 10:15:44 -0000 Received: (qmail 66200 invoked by uid 500); 7 Feb 2003 10:14:01 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 66176 invoked from network); 7 Feb 2003 10:14:01 -0000 Received: from multicom.co.uk (HELO charon.multicom.co.uk) (195.188.198.195) by daedalus.apache.org with SMTP; 7 Feb 2003 10:14:01 -0000 Received: from zultan (janus.multicom.co.uk [195.188.198.200]) by charon.multicom.co.uk with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2650.21) id YV9KVGPG; Fri, 7 Feb 2003 10:15:23 -0000 Message-ID: <006601c2ce91$dad90270$1b9dbdc0@zultan> From: "Adam J Chesney" To: "Jakarta Commons Developers List" , References: <20030206214743.8225.qmail@web11703.mail.yahoo.com> Subject: Re: [JXPath] Problem adding objects to an ArrayList Variable Date: Fri, 7 Feb 2003 10:15:35 -0000 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0063_01C2CE91.DACAAA90" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4807.1700 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N ------=_NextPart_000_0063_01C2CE91.DACAAA90 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hi Dimitri, Hmmm. This seems a bit strange to me. It works for Maps (using put instead of add). If I change the context definition to this: Object o = new Object (); JXPathContext context = JXPathContext.newContext( o ); context.getVariables().declareVariable("myList", new ArrayList() ); context.getVariables().declareVariable("myMap", new HashMap() ); FunctionLibrary library = new FunctionLibrary (); library.addFunctions( new ClassFunctions ( List.class, "list" ) ); library.addFunctions( new ClassFunctions ( Map.class, "map" ) ); context.setFunctions( library ); Then I can produce the following results: getValue("list:size($myList)") returns: 0 getValue("list:add($myList, 'hello')") returns: true getValue("list:size($myList)") returns: 0 getValue("$myList") returns: [] getValue("map:size($myMap)") returns: 0 getValue("map:put($myMap, 'key', 'value')") returns: null getValue("map:size($myMap)") returns: 1 getValue("$myMap") returns: {key=value} So you can see that the Map is mutable. I'm not sure how difficult it is to allow for mutable Lists, but if I can't do things like: getValue("list:add($myList, 'hello')") returns: true I think it might be a show stopper for me. I am almost certainly trying to use JXPath in a way that was not intended. I am trying to build a 2 way data binding mapping structure which I think JXPath is excellent for, but I would also like to add some easy micro-scripting, which is maybe not possible with JXPath. One other thing, can you explain to me why the returned collections should be immutable? Is it necessary to enforce that behaviour? Thanks for your help, Adam. ----- Original Message ----- From: "Dmitri Plotnikov" To: "Jakarta Commons Developers List" Sent: Thursday, February 06, 2003 9:47 PM Subject: Re: [JXPath] Problem adding objects to an ArrayList Variable > Adam, > > JXPath is in fact incapable of doing what you want it to do. Before a > collection is passed to an extension function, it undergoes some > conversions, which make it unmodifiable. JXPath should of course throw > an exception in this case. I have added that exception and will check > that new code in tonight. > > The rationale for this behavior is that from JXPath's perspective an > extension function cannot take a collection by reference. Arguments of > an extension function are either primitive values or "node sets", which > are really results of search. JXPath finds all nodes matching the > path, loads them into a list and then passes the list to the function. > Consider for example this: > > context.getValue("size(//foo[bar = 3])") > > From this example it should be clear that the argument of the function > is in fact an unmodifiable collection - results of search. > > We could of course do something special for variables, but then the > extension function's code would have to be aware of the difference > between variables and general node sets. I think we should avoid this > implicit difference. > > What you could do to add a node to the collection is call > createPathAndSetValue() with a path like "$myList[24]". > > Thanks for bringing this issue up - I should document it. > > - Dmitri > > > --- Adam J Chesney wrote: > > Hi, > > > > Attached is a small test program that simply defines a Context like > > this: > > > > Object o = new Object (); > > JXPathContext context = JXPathContext.newContext( o ); > > context.getVariables().declareVariable("myList", new ArrayList() ); > > > > FunctionLibrary library = new FunctionLibrary (); > > library.addFunctions( new ClassFunctions ( List.class, "list" ) ); > > context.setFunctions( library ); > > > > The output from executing several XPath expressions on it is thus: > > > > getValue("list:size($myList)") returns: 0 > > > > getValue("list:add($myList, 'hello')") returns: true > > > > getValue("list:size($myList)") returns: 0 > > > > getValue("$myList") returns: [] > > > > I would expect the String 'hello' to be added to the list, and > > therefore the second call to size should return 1. > > > > Am I doing something wrong here? > > > > Cheers, > > > > Adam > > > > ===================================================== > > > > Tel: (+44) 117 908 1254 (Direct) > > Mobile (+44) 7780 962 961 > > email: adam@multicom.co.uk > > > > This communication is intended solely for the addressee and is > > confidential. > > If you are not the intended recipient, any disclosure, copying, > > distribution > > or any action taken or omitted to be taken in reliance on it, is > > prohibited > > and may be unlawful. > > > > Although this e-mail and any attachments are believed to be free of > > any > > virus, or any other defect which might affect any computer or IT > > system into > > which they are received and opened, it is the responsibility of the > > recipient to ensure that they are virus free and no responsibility is > > accepted by Multicom Products Limited for any loss or damage arising > > in any > > way from receipt or use thereof. > > > package test; > > > > import org.apache.commons.jxpath.*; > > import java.util.*; > > > > /** > > *

Title:

> > *

Description:

> > *

Copyright: Copyright (c) 2003

> > *

Company:

> > * @author unascribed > > * @version 1.0 > > */ > > > > public class Test2 > > { > > > > public static void main(String[] args) > > { > > try > > { > > Object o = new Object (); > > JXPathContext context = JXPathContext.newContext( o ); > > context.getVariables().declareVariable("myList", new ArrayList() ); > > > > FunctionLibrary library = new FunctionLibrary (); > > library.addFunctions( new ClassFunctions ( List.class, "list" ) ); > > context.setFunctions( library ); > > > > getValue (context, "list:size($myList)"); > > getValue (context, "list:add($myList, 'hello')"); > > getValue (context, "list:size($myList)"); > > getValue (context, "$myList"); > > } > > catch (Exception e1) > > { > > e1.printStackTrace(); > > } > > } > > > > public static Object getValue ( JXPathContext context, String xpath > > ) > > { > > Object obj = context.getValue( xpath ); > > System.out.println("getValue(\"" + xpath + "\") returns: " + obj > > ); > > return obj; > > } > > > > } > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org > > For additional commands, e-mail: commons-dev-help@jakarta.apache.org > > > __________________________________________________ > Do you Yahoo!? > Yahoo! Mail Plus - Powerful. Affordable. Sign up now. > http://mailplus.yahoo.com > > --------------------------------------------------------------------- > To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org > For additional commands, e-mail: commons-dev-help@jakarta.apache.org ------=_NextPart_000_0063_01C2CE91.DACAAA90 Content-Type: text/x-java; name="Test2.java" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="Test2.java" package test;=0A= =0A= import org.apache.commons.jxpath.*;=0A= import java.util.*;=0A= =0A= /**=0A= *

Title:

=0A= *

Description:

=0A= *

Copyright: Copyright (c) 2003

=0A= *

Company:

=0A= * @author unascribed=0A= * @version 1.0=0A= */=0A= =0A= public class Test2=0A= {=0A= =0A= public static void main(String[] args)=0A= {=0A= try=0A= {=0A= Object o =3D new Object ();=0A= JXPathContext context =3D JXPathContext.newContext( o );=0A= context.getVariables().declareVariable("myList", new ArrayList() );=0A= context.getVariables().declareVariable("myMap", new HashMap() );=0A= =0A= FunctionLibrary library =3D new FunctionLibrary ();=0A= library.addFunctions( new ClassFunctions ( List.class, "list" ) );=0A= library.addFunctions( new ClassFunctions ( Map.class, "map" ) );=0A= context.setFunctions( library );=0A= =0A= getValue (context, "list:size($myList)");=0A= getValue (context, "list:add($myList, 'hello')");=0A= getValue (context, "list:size($myList)");=0A= getValue (context, "$myList");=0A= =0A= getValue (context, "map:size($myMap)");=0A= getValue (context, "map:put($myMap, 'key', 'value')");=0A= getValue (context, "map:size($myMap)");=0A= getValue (context, "$myMap");=0A= =0A= }=0A= catch (Exception e1)=0A= {=0A= e1.printStackTrace();=0A= }=0A= }=0A= =0A= public static Object getValue ( JXPathContext context, String xpath )=0A= {=0A= Object obj =3D context.getValue( xpath );=0A= System.out.println("getValue(\"" + xpath + "\") returns: " + obj );=0A= return obj;=0A= }=0A= =0A= } ------=_NextPart_000_0063_01C2CE91.DACAAA90 Content-Type: text/plain; charset=us-ascii --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org ------=_NextPart_000_0063_01C2CE91.DACAAA90--