commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew Hughes" <ahhug...@gmail.com>
Subject JXPath XBeanInfo & Generic Collection Problem
Date Fri, 21 Nov 2008 03:12:15 GMT
Hi,
I'm trying to work out how I can add the following PojoNode to JXPath's
context for evaluation.

public class PojoNode extends java.util.ArrayList<PojoNode> {
public PojoNode(Pojo pojo) {
this.pojo = pojo;
}
private Pojo pojo;
public Pojo getPojo() {
return pojo;
}
}



The PojoNode tree above is (definitely) populated... then I setup and ask
JXPath to evaluate an expression on it:

JXPathIntrospector.registerDynamicClass(PojoNode.class,PojoDynamicPropertyHandler.class);
JXPathContext jxPathContext = JXPathContext.newContext(rootPojoNode);
PojoNode result = (PojoNode)jxPathContext.getValue("/"); //this always
produces an empty result


What concerns me is not the code I have written but the code I have not.
I've got log statements in the PojoXInfoBean and PojoDynamicPropertyHandler
(see below) but none every fire (in particular  "I have been asked to
look.." and "...w00t").  Consequently the PojoNode tree is not being
traversed correctly and thus fails. I suspect that because PojoNode
implements a Collection it's picked up buy another DynamicPropertyHandler.

I feel like I have exhausted the javadocs and any help would be very very
much appreciated.

Thank You.


ps if you wanted to see more code it is below....

public class PojoNodeDynamicPropertyHandler implements
DynamicPropertyHandler {

private final static Logger log =
Logger.getLogger(PojoNodeDynamicPropertyHandler.class);

public Object getProperty(Object PojoNode, String name) {
log.debug("I have been asked to look for '"+name+"'");
Collection<PojoNode> hits = new ArrayList<PojoNode>();
for(PojoNode childPojoNode : (PojoNode) PojoNode){
if (childPojoNode.getPojo().getName().equals(name)){
hits.add(childPojoNode);
}
}

log.debug("I have found '"+hits.size()+"' instances of '"+name+"'");
return hits;
}

public String[] getPropertyNames(Object PojoNode) {
log.debug("JXPath has asked for this, w00t!");
HashSet<String> nameSet = new HashSet<String>();
nameSet.addAll(collectChildNames(PojoNode));
return (String[]) nameSet.toArray(new String[nameSet.size()]);
}

public void setProperty(Object arg0, String arg1, Object arg2) {
throw new RuntimeException(
"Manipulation through JXPath is not allowed.... (yet)");
}

private HashSet<String> collectChildNames(Object PojoNode) {
HashSet<String> nameSet = new HashSet<String>();
for (PojoNode childPojo : (PojoNode) PojoNode) {
nameSet.add(childPojo.getPojo().getName());
}
return nameSet;
}

}
........................................................................................

public class PojoNodeXBeanInfo implements JXPathBeanInfo{

 private static final Logger log =
Logger.getLogger(PojoNodeXBeanInfo.class);


public Class<PojoNodeDynamicPropertyHandler>
getDynamicPropertyHandlerClass() {

log.debug("JXPath has asked for this, w00t!");

return PojoNodeDynamicPropertyHandler.class;

}


public PropertyDescriptor getPropertyDescriptor(String arg0) {

log.debug("JXPath has asked for this, w00t!");

return null;

}


public PropertyDescriptor[] getPropertyDescriptors() {

log.debug("JXPath has asked for this, w00t!");

return null;

}


public boolean isAtomic() {

log.debug("JXPath has asked for this, w00t!");

return false;

}


public boolean isDynamic() {

log.debug("JXPath has asked for this, w00t!");

return true;

}

}

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