You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Jyotsna <jy...@spearheadit.com> on 2007/02/21 08:46:10 UTC

RE: Exception while setting values for Collection /ArrayList using JXpath

Matt 
 
This works fine for String ArrayList .What if my ArrayList contains
objects of a different type .How do I go about setting that.
 
Which means I have the pojo OrganisationType 
 
*************************************************
public class OrganisationType implements Serializable {
 
    Long id;
    String code;
    String  description;
    int version;
    Collection arr =new ArrayList();
    HashMap hsh =new HashMap();
   
*******************************
 
In this the Collection arr has a list of Objects of type Address 
 
public class Address implements Serializable {
 
     Long id;
     String line1;
 
****************************************************
 
How do I populate the Address object in the ArrayList of
OrganisationType object.
 
Best Regards
Jyotsna
 
 
 
-----Original Message-----
From: Matt Benson [mailto:gudnabrsam@yahoo.com] 
Sent: Tuesday, February 20, 2007 9:54 PM
To: Jakarta Commons Users List; jyotsna.bharadwaj@spearheadit.com
Subject: Re: Exception while setting values for Collection /ArrayList
using JXpath
 
Jyotsna,
 
JXPath is designed such that nonexistent parts of the
graph cannot be set directly.  If you know that arr[0]
might not exist yet, you must check for that condition
explicitly and install a factory to create that part
of the graph.  My test code looks like this:
 
OrganisationType t = new OrganisationType();
JXPathContext ctx = JXPathContext.newContext(t);
ctx.setLenient(true);
Pointer ptr = ctx.getPointer("arr[1]");
if (ptr == null || ptr instanceof NodePointer &&
!((NodePointer) ptr).isActual()) {
  ctx.setFactory(new AbstractFactory() {
    public boolean createObject(JXPathContext context,
Pointer pointer, Object parent, String name, int
index) {
      if (parent instanceof OrganisationType &&
"arr".equals(name)) {
        ArrayList l = (ArrayList) ((OrganisationType)
parent).arr;
        l.addAll(Arrays.asList(new Object[index -
l.size() + 1]));
        return true;
      }
      return super.createObject(context, pointer,
parent, name, index);
    }
  });
  ptr = ctx.createPath("arr[1]");
}
ptr.setValue("bar");
 
HTH,
Matt
 
--- Jyotsna <jy...@spearheadit.com> wrote:
 
> Hi 
>  
> I have a java bean with the following attributes and
> their getters and
> setters.
>  
>
************************************************************
>  
> public class OrganisationType implements
> Serializable {
>  
>     Long id;
>     String code;
>     String  description;
>     int version;
>     Collection arr =new ArrayList();
>     HashMap hsh =new HashMap();
>    
> Getter and setters follow.......................
>  
>
************************************************************
>  
> I am using the following code to populate the array
> list 
>  
>    OrganisationType orgna = new OrganisationType();
>    orgna.setCode("12121");
>    JXPathContext contexts =
> JXPathContext.newContext(orgna);
>    contexts.setValue("arr[1]","12");
>  
>  
> ****************And the Exception I get is
> ***************************
> org.apache.commons.jxpath.JXPathException: Exception
> trying to set value
> with xpath arr[1]; Index: 0, Size: 0
>       at
>
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.setValue(JXPathC
> ontextReferenceImpl.java:421)
>       at
>
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.setValue(JXPathC
> ontextReferenceImpl.java:412)
>       at TestJxPath.main(TestJxPath.java:36)
> 
 
 
 
 
________________________________________________________________________
____________
We won't tell. Get more on shows you hate to love 
(and love to hate): Yahoo! TV's Guilty Pleasures list.
http://tv.yahoo.com/collections/265 
 

RE: Exception while setting values for Collection /ArrayList using JXpath

Posted by Matt Benson <gu...@yahoo.com>.
--- Jyotsna <jy...@spearheadit.com> wrote:

> Matt 
>  
> This works fine for String ArrayList .What if my
> ArrayList contains
> objects of a different type .How do I go about
> setting that.

uh, use a different value parameter in the call to
setValue() ?

-Matt

>  
> Which means I have the pojo OrganisationType 
>  
> *************************************************
> public class OrganisationType implements
> Serializable {
>  
>     Long id;
>     String code;
>     String  description;
>     int version;
>     Collection arr =new ArrayList();
>     HashMap hsh =new HashMap();
>    
> *******************************
>  
> In this the Collection arr has a list of Objects of
> type Address 
>  
> public class Address implements Serializable {
>  
>      Long id;
>      String line1;
>  
> ****************************************************
>  
> How do I populate the Address object in the
> ArrayList of
> OrganisationType object.
>  
> Best Regards
> Jyotsna
>  
>  
>  
> -----Original Message-----
> From: Matt Benson [mailto:gudnabrsam@yahoo.com] 
> Sent: Tuesday, February 20, 2007 9:54 PM
> To: Jakarta Commons Users List;
> jyotsna.bharadwaj@spearheadit.com
> Subject: Re: Exception while setting values for
> Collection /ArrayList
> using JXpath
>  
> Jyotsna,
>  
> JXPath is designed such that nonexistent parts of
> the
> graph cannot be set directly.  If you know that
> arr[0]
> might not exist yet, you must check for that
> condition
> explicitly and install a factory to create that part
> of the graph.  My test code looks like this:
>  
> OrganisationType t = new OrganisationType();
> JXPathContext ctx = JXPathContext.newContext(t);
> ctx.setLenient(true);
> Pointer ptr = ctx.getPointer("arr[1]");
> if (ptr == null || ptr instanceof NodePointer &&
> !((NodePointer) ptr).isActual()) {
>   ctx.setFactory(new AbstractFactory() {
>     public boolean createObject(JXPathContext
> context,
> Pointer pointer, Object parent, String name, int
> index) {
>       if (parent instanceof OrganisationType &&
> "arr".equals(name)) {
>         ArrayList l = (ArrayList)
> ((OrganisationType)
> parent).arr;
>         l.addAll(Arrays.asList(new Object[index -
> l.size() + 1]));
>         return true;
>       }
>       return super.createObject(context, pointer,
> parent, name, index);
>     }
>   });
>   ptr = ctx.createPath("arr[1]");
> }
> ptr.setValue("bar");
>  
> HTH,
> Matt
>  
> --- Jyotsna <jy...@spearheadit.com>
> wrote:
>  
> > Hi 
> >  
> > I have a java bean with the following attributes
> and
> > their getters and
> > setters.
> >  
> >
>
************************************************************
> >  
> > public class OrganisationType implements
> > Serializable {
> >  
> >     Long id;
> >     String code;
> >     String  description;
> >     int version;
> >     Collection arr =new ArrayList();
> >     HashMap hsh =new HashMap();
> >    
> > Getter and setters follow.......................
> >  
> >
>
************************************************************
> >  
> > I am using the following code to populate the
> array
> > list 
> >  
> >    OrganisationType orgna = new
> OrganisationType();
> >    orgna.setCode("12121");
> >    JXPathContext contexts =
> > JXPathContext.newContext(orgna);
> >    contexts.setValue("arr[1]","12");
> >  
> >  
> > ****************And the Exception I get is
> > ***************************
> > org.apache.commons.jxpath.JXPathException:
> Exception
> > trying to set value
> > with xpath arr[1]; Index: 0, Size: 0
> >       at
> >
>
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.setValue(JXPathC
> > ontextReferenceImpl.java:421)
> >       at
> >
>
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.setValue(JXPathC
> > ontextReferenceImpl.java:412)
> >       at TestJxPath.main(TestJxPath.java:36)
> > 
>  
>  
>  
>  
>
________________________________________________________________________
> ____________
> We won't tell. Get more on shows you hate to love 
> (and love to hate): Yahoo! TV's Guilty Pleasures
> list.
> http://tv.yahoo.com/collections/265 
>  
> 



 
____________________________________________________________________________________
It's here! Your new message!  
Get new email alerts with the free Yahoo! Toolbar.
http://tools.search.yahoo.com/toolbar/features/mail/

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org