commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jyotsna" <jyotsna.bharad...@spearheadit.com>
Subject RE: Exception while setting values for Collection /ArrayList using JXpath
Date Wed, 21 Feb 2007 07:46:10 GMT
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 <jyotsna.bharadwaj@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 
 

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