Return-Path: Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: (qmail 60352 invoked from network); 17 Feb 2011 16:56:50 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 17 Feb 2011 16:56:50 -0000 Received: (qmail 2833 invoked by uid 500); 17 Feb 2011 16:56:50 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 2364 invoked by uid 500); 17 Feb 2011 16:56:47 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 2336 invoked by uid 99); 17 Feb 2011 16:56:46 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 17 Feb 2011 16:56:46 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.116] (HELO hel.zones.apache.org) (140.211.11.116) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 17 Feb 2011 16:56:44 +0000 Received: from hel.zones.apache.org (hel.zones.apache.org [140.211.11.116]) by hel.zones.apache.org (Postfix) with ESMTP id 5952D1AA7D7 for ; Thu, 17 Feb 2011 16:56:24 +0000 (UTC) Date: Thu, 17 Feb 2011 16:56:24 +0000 (UTC) From: "Aleksandr Bissekerskij (JIRA)" To: issues@commons.apache.org Message-ID: <657566414.3352.1297961784361.JavaMail.tomcat@hel.zones.apache.org> In-Reply-To: <1780601007.3162.1297959744917.JavaMail.tomcat@hel.zones.apache.org> Subject: [jira] Updated: (JXPATH-150) JXPathContext: using AbstractFactory with Map in XPath MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/JXPATH-150?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Aleksandr Bissekerskij updated JXPATH-150: ------------------------------------------ Attachment: Parent.java JXPathMap.java Child.java Original files representing issue that is described. > JXPathContext: using AbstractFactory with Map in XPath > ------------------------------------------------------ > > Key: JXPATH-150 > URL: https://issues.apache.org/jira/browse/JXPATH-150 > Project: Commons JXPath > Issue Type: Improvement > Affects Versions: 1.3 > Reporter: Aleksandr Bissekerskij > Priority: Minor > Attachments: Child.java, JXPathMap.java, Parent.java > > > Hello, > I have a problem with JXPath which I suppose is something that is not supported yet, though it's looks like a bug. > I'm using AbstractFactory implementation to create missing objects in XPath when setting properties. (see http://commons.apache.org/jxpath/users-guide.html#Creating_Objects). Everything works fine as long as missing objects are Bean-like objects, or any Collections. But if missing object is one of objects in Map problems begin. Here is my issue (please find complete code of used classes and working example attached): > {code:title=Parent.java} > package tests.xpath; > import java.util.Map; > public class Parent { > private String name; > private Map children; > Parent() {} > Parent(String name) { setName(name); } > public Map getChildren() { return children; } > public void setChildren(Map children) { this.children = children; } > public String getName() { return name; } > public void setName(String name) { this.name = name; } > @Override > public String toString() { > return this.getClass().getSimpleName() + "[" + > "name=" + getName() + ", " + > "children=" + getChildren() + > "]"; > } > } > {code} > {code:title=Child.java} > package tests.xpath; > public class Child { > private String sex; > private int age; > Child() {} > Child(String sex, int age) { setSex(sex); setAge(age); } > public int getAge() { return age; } > public void setAge(int age) { this.age = age; } > public String getSex() { return sex; } > public void setSex(String sex) { this.sex = sex; } > @Override > public String toString() { > return this.getClass().getSimpleName() + "[" + > "sex=" + getSex() + ", " + > "age=" + getAge() + > "]"; > } > } > {code} > {code:title=JXPathMap.java} > package tests.xpath; > import java.util.LinkedHashMap; > import org.apache.commons.jxpath.AbstractFactory; > import org.apache.commons.jxpath.JXPathContext; > import org.apache.commons.jxpath.Pointer; > public class JXPathMap { > public static void main (String[] args) { > Parent p = new Parent("Dad"); > p.setChildren(new LinkedHashMap() {{ > put("Bobby", new Child("Male", 9)); > put("Steven", null); > put("Sandra", new Child("Female", 12)); > }}); > JXPathContext context = JXPathContext.newContext(p); > context.setFactory(new AbstractFactory() { > @Override > public boolean createObject(JXPathContext context, Pointer pointer, Object parent, String name, int index) { > System.out.println("parent: " + parent); > System.out.println("name: " + name); > System.out.println("index: " + index); > return super.createObject(context, pointer, parent, name, index); > } > }); > context.createPathAndSetValue("children/Steven/age", 4); > } > } > {code} > this code results as following: > {noformat} > parent: {Bobby=Child[sex=Male, age=9], Steven=null, Sandra=Child[sex=Female, age=12]} > name: Steven > index: 0 > Exception in thread "main" org.apache.commons.jxpath.JXPathException: Exception trying to create xpath children/Steven/age; Factory could not create an object for path: /children[@name='Steven'] > at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.createPathAndSetValue(JXPathContextReferenceImpl.java:549) > at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.createPathAndSetValue(JXPathContextReferenceImpl.java:533) > at tests.xpath.JXPathMap.main(JXPathMap.java:29) > Caused by: org.apache.commons.jxpath.JXPathAbstractFactoryException: Factory could not create an object for path: /children[@name='Steven'] > at org.apache.commons.jxpath.ri.model.dynamic.DynamicPropertyPointer.createPath(DynamicPropertyPointer.java:230) > at org.apache.commons.jxpath.ri.model.beans.NullPointer.createPath(NullPointer.java:100) > at org.apache.commons.jxpath.ri.model.beans.NullPropertyPointer.createPath(NullPropertyPointer.java:138) > at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.setValue(JXPathContextReferenceImpl.java:584) > at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.createPathAndSetValue(JXPathContextReferenceImpl.java:546) > ... 2 more > {noformat} > I must mention that context.createPathAndSetValue("children/Bobby/age", 4) works fine - sets Bobby's age to 4. In case of missing object is element of collection - everything works fine, I would get Parent as parent object, children as property that has missing object and index so i know where to put my newly created object. > In case of Map I don't get enough information to instantiate missing object. "Steven" as property name is most surprising. Exception is OK, cause nothing is created by factory. > I suppose that AbstractFactory should have separate method in case of Map element is missed. JXPath easily detects that it is Map because setting "children/Bobby/age" works as expected. > Regards, > Aleksandr -- This message is automatically generated by JIRA. - For more information on JIRA, see: http://www.atlassian.com/software/jira