You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Matt Benson <gu...@yahoo.com> on 2008/02/07 21:55:38 UTC
RE: [JXPath] and Collections
--- Jeffrey Puro <jp...@sterlingtesting.com> wrote:
> If I change this to a List, how do I set an element
> on it or add one to
> it?
>
> context.setValue("myList/", "New object on list");
>
> Do I have to specify a particular index or can
> setValue just add on its
> own?
You have to specify the index. And unfortunately it
seems JXPath isn't smart enough to be able to take an
expression as an index. Here's what I had to do here:
//given MyBean { List getList(){...} void
setList(List)}:
MyBean myBean = new MyBean();
myBean.getList().add("foo");
JXPathContext context =
JXPathContext.newContext(myBean);
context.setFactory(new AbstractFactory() {
public boolean createObject(JXPathContext
context, Pointer pointer,
Object parent, String name, int
index) {
if (pointer instanceof
PropertyPointer) {
Object baseValue =
((PropertyPointer) pointer).getBaseValue();
if (baseValue instanceof List) {
List list = (List) baseValue;
while (index >= list.size()) {
list.add(null);
}
return true;
}
}
return false;
}
});
int listSize = ((Integer)
context.getValue("size(/list)")).intValue();
context.createPathAndSetValue("/list[" +
(listSize + 1) + "]", "bar");
HTH,
Matt
>
> Thanks,
>
> Jeff
>
> -----Original Message-----
> From: Matt Benson [mailto:gudnabrsam@yahoo.com]
> Sent: Wednesday, February 06, 2008 3:55 PM
> To: Jakarta Commons Users List
> Subject: Re: JXPath and Collections
>
>
> --- Jeffrey Puro <jp...@sterlingtesting.com> wrote:
>
> > I have a pojo that has the following properties:
> >
>
> Looks like a set is unsupported. :( Could you
> possibly use some kind of wrapper object, possibly
> returning asList() from a commons-collections
> ListOrderedSet from a getSetAsList() method or
> something similar? JXPath can add an element to a
> list.
>
> -Matt
>
> >
> >
> > Class MyPojo {
> >
> >
> >
> > private Set mySet;
> >
> >
> >
> > }
> >
> >
> >
> > Using JXPath, I am trying to add a new object to
> my
> > Set object called
> > mySet by using an expression as follows:
> >
> >
> >
> > context.setValue("mySet/", "New object on set");
> >
> >
> >
> > However, this does not work when attempting to add
> > elements to a
> > collection (as it's trying to replace the Set
> object
> > with my new
> > String). I'm not sure how one would do this with
> > the setValue method,
> > but any help would be appreciated.
> >
> >
> >
> > Regards,
> >
> >
> >
> > Jeff
> >
> >
> > This email (and any attachments) is intended only
> > for the use of the individual or entity named
> above
> > and may contain information that is privileged and
> > confidential. If you are not the intended
> recipient,
> > or have unauthorized access, you are hereby
> notified
> > that copying, disseminating, distributing or
> taking
> > any action in reliance on this email is strictly
> > prohibited<br />
> >
> > Opinions, conclusions and other information in
> this
> > message that do not relate to the official
> business
> > of our firm shall be understood as neither given
> nor
> > endorsed by it.
> >
>
>
>
>
>
________________________________________________________________________
> ____________
> Never miss a thing. Make Yahoo your home page.
> http://www.yahoo.com/r/hs
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@commons.apache.org
> For additional commands, e-mail:
> user-help@commons.apache.org
>
>
> This email (and any attachments) is intended only
> for the use of the individual or entity named above
> and may contain information that is privileged and
> confidential. If you are not the intended recipient,
> or have unauthorized access, you are hereby notified
> that copying, disseminating, distributing or taking
> any action in reliance on this email is strictly
> prohibited<br />
>
> Opinions, conclusions and other information in this
> message that do not relate to the official business
> of our firm shall be understood as neither given nor
> endorsed by it.
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@commons.apache.org
> For additional commands, e-mail:
> user-help@commons.apache.org
>
>
____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org