Thanks for the tip Robert. Upgraded to the latest from CVS did solve
the class cast exception. However I now have a new problem. Any
pointers would be appreciated.
I downloaded the latest source from CVS and compiled it using maven. I
then copied the resulting betwixt jar (0.6.1) as well as the other
jars that were downloaded to my maven lib directory (beanutil,
digester, ect) and copied them to a clean directory. I wrote a quick
ant script to run my Betwixt test class that creates a PersonBean
(from the regression tests) and fills it with data, uses BeanWriter to
write the xml data to a StringWriter, then pipe that data into
BeanReader. The result is reader.parse(xmlreader) returns null every
time. For some reason it is not matching the <PersonBean> attribute
with the PersonBean class/rule (yes I registered the PersonBean
class). If the log4j debug output would be helpful let me know and I
will supply it. See below for failing code:
public class TestBetwixt {
public static final void main(String args[]) throws Exception{
StringWriter out = new StringWriter();
out.write("<?xml version='1.0'?>");
BeanWriter writer = new BeanWriter(out);
writer.setWriteEmptyElements( true );
PersonBean personBean = new PersonBean();
personBean.setName("ryan c");
personBean.setAge(24);
writer.write(personBean);
System.out.println("out: " + out);
StringReader xmlReader = new StringReader(out.toString());
BeanReader reader = new BeanReader();
reader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
reader.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
reader.registerBeanClass(PersonBean.class);
PersonBean person = (PersonBean) reader.parse(xmlReader);
// At this point person is null
System.out.println(person);
}
}
On Mon, 13 Dec 2004 22:52:06 +0000, robert burrell donkin
<[EMAIL PROTECTED]> wrote:
> hi ryan
>
> this sounds very much like a bug that's been fixed in CVS HEAD (we
> really need to cut a 1.6.1 release very soon). please upgrade to a
> recent nightly or roll your own from CVS HEAD. if this doesn't fix the
> issue, please post a reply...
>
> - robert
>
> On 13 Dec 2004, at 06:30, Ryan Crumley wrote:
>
> > I am trying to get the betwixt "PersonBean" example working in my
> > local sandbox however I am getting a ClassCastException. Does anyone
> > have a suggestion as to what I am doing wrong? Below is my code
> > followed by the log4j output and stack trace.
> >
> > Thanks,
> > ryan
> >
> > public class TestBetwixt {
> >
> > public static final void main(String args[]) throws Exception{
> >
> > // First construct the xml which will be read in
> > // For this example, read in from a hard coded string
> > StringReader xmlReader = new StringReader(
> > "<?xml version='1.0'
> > ?><person><age>25</age><name>James Smith</name></person>");
> >
> > // Now convert this to a bean using betwixt
> > // Create BeanReader
> > BeanReader beanReader = new BeanReader();
> >
> > // Configure the reader
> > // If you're round-tripping, make sure that the configurations
> > are compatible!
> >
> > beanReader.getXMLIntrospector().setAttributesForPrimitives(false);
> > beanReader.setMatchIDs(false);
> >
> > // Register beans so that betwixt knows what the xml is to be
> > converted to
> > // Since the element mapped to a PersonBean isn't called the
> > same,
> > // need to register the path as well
> > beanReader.registerBeanClass("person", PersonBean.class);
> >
> > // Now we parse the xml
> > PersonBean person = (PersonBean) beanReader.parse(xmlReader);
> >
> > // send bean to system out
> > System.out.println(person);
> > }
> >
> >
> > public static class PersonBean {
> >
> > private String name;
> > private int age;
> >
> > /** Need to allow bean to be created via reflection */
> > public PersonBean() {}
> >
> > public PersonBean(String name, int age) {
> > this.name = name;
> > this.age = age;
> > }
> >
> > public String getName() {
> > return name;
> > }
> >
> > public void setName(String name) {
> > this.name = name;
> > }
> >
> > public int getAge() {
> > return age;
> > }
> >
> > public void setAge(int age) {
> > this.age = age;
> > }
> >
> > public String toString() {
> > return "PersonBean[name='" + name + "',age='" + age + "']";
> > }
> > }
> >
> > }
> >
> > 00:27:47,671 DEBUG XMLIntrospector:462 - Attempting to lookup an XML
> > descriptor for class: class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean
> > 00:27:47,671 DEBUG XMLIntrospector:956 - Could not find betwixt file
> > TestBetwixt$PersonBean.betwixt
> > 00:27:47,722 DEBUG XMLIntrospector:518 -
> > Populating:Bean[name=TestBetwixt$PersonBean, type=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean
> > 00:27:47,732 DEBUG XMLIntrospector:538 - Bean is standard type
> > 00:27:47,742 DEBUG XMLIntrospector:1006 - 3 properties to be added
> > 00:27:47,742 DEBUG XMLIntrospector:150 - Creating descriptor for
> > property: name=age type=int
> > 00:27:47,742 DEBUG XMLIntrospector:167 - Property
> > expression=MethodExpression [method=public int
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean.getAge()]
> > 00:27:47,742 DEBUG XMLIntrospector:378 - Primitive type: age
> > 00:27:47,742 DEBUG XMLIntrospector:392 - Adding property as element:
> > age
> > 00:27:47,742 DEBUG XMLIntrospector:220 - Created descriptor:
> > 00:27:47,742 DEBUG XMLIntrospector:221 -
> > ElementDescriptor[qname=age,pname=age,class=int,singular=int,updater=Me
> > thodUpdater
> > [method=public void
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean.setAge(int)],wra
> > p=true]
> > 00:27:47,742 DEBUG XMLIntrospector:150 - Creating descriptor for
> > property: name=class type=class java.lang.Class
> > 00:27:47,742 DEBUG XMLIntrospector:167 - Property
> > expression=MethodExpression [method=public final native
> > java.lang.Class java.lang.Object.getClass()]
> > 00:27:47,742 DEBUG XMLIntrospector:174 - Ignoring class property
> > 00:27:47,752 DEBUG XMLIntrospector:150 - Creating descriptor for
> > property: name=name type=class java.lang.String
> > 00:27:47,752 DEBUG XMLIntrospector:167 - Property
> > expression=MethodExpression [method=public java.lang.String
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean.getName()]
> > 00:27:47,752 DEBUG XMLIntrospector:378 - Primitive type: name
> > 00:27:47,752 DEBUG XMLIntrospector:392 - Adding property as element:
> > name
> > 00:27:47,752 DEBUG XMLIntrospector:220 - Created descriptor:
> > 00:27:47,752 DEBUG XMLIntrospector:221 -
> > ElementDescriptor[qname=name,pname=name,class=class
> > java.lang.String,singular=class java.lang.String,updater=MethodUpdater
> > [method=public void
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean.setName(java.lan
> > g.String)],wrap=true]
> > 00:27:47,752 DEBUG XMLIntrospector:1013 - After properties have been
> > added (elements, attributes, contents):
> > 00:27:47,752 DEBUG XMLIntrospector:1014 -
> > [ElementDescriptor[qname=age,pname=age,class=int,singular=int,updater=M
> > ethodUpdater
> > [method=public void
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean.setAge(int)],wra
> > p=true],
> > ElementDescriptor[qname=name,pname=name,class=class
> > java.lang.String,singular=class java.lang.String,updater=MethodUpdater
> > [method=public void
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean.setName(java.lan
> > g.String)],wrap=true]]
> > 00:27:47,752 DEBUG XMLIntrospector:1015 - []
> > 00:27:47,752 DEBUG XMLIntrospector:1016 - []
> > 00:27:47,752 DEBUG XMLIntrospector:573 - Populated descriptor:
> > 00:27:47,752 DEBUG XMLIntrospector:574 -
> > ElementDescriptor[qname=TestBetwixtPersonBean,pname=null,class=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,singular=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,updater=null,wra
> > p=true]
> > 00:27:47,772 DEBUG XMLIntrospector:479 - XMLBeanInfo [class=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,
> > descriptor=ElementDescriptor[qname=TestBetwixtPersonBean,pname=null,cla
> > ss=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,singular=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,updater=null,wra
> > p=true]]
> > 00:27:47,772 DEBUG BeanReader:335 - Adding BeanRuleSet for class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean
> > 00:27:47,772 DEBUG Digester:1695 - addRuleSet() with no namespace URI
> > 00:27:47,772 DEBUG BeanRuleSet:212 - Adding rules
> > to:[EMAIL PROTECTED]
> > 00:27:48,002 DEBUG sax:1180 -
> > setDocumentLocator(org.apache.xerces.parsers.AbstractSAXParser$LocatorP
> > [EMAIL PROTECTED])
> > 00:27:48,002 DEBUG sax:1214 - startDocument()
> > 00:27:48,052 DEBUG sax:1316 -
> > startPrefixMapping(xml,http://www.w3.org/XML/1998/namespace)
> > 00:27:48,052 DEBUG sax:1316 -
> > startPrefixMapping(xmlns,http://www.w3.org/2000/xmlns/)
> > 00:27:48,052 DEBUG sax:1243 - startElement(,,person)
> > 00:27:48,052 DEBUG Digester:1250 - Pushing body text ''
> > 00:27:48,052 DEBUG Digester:1269 - New match='person'
> > 00:27:48,052 DEBUG Digester:1284 - Fire begin() for
> > [EMAIL PROTECTED]
> > 00:27:48,052 DEBUG XMLIntrospector:475 - Used cached XMLBeanInfo.
> > 00:27:48,052 DEBUG XMLIntrospector:479 - XMLBeanInfo [class=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,
> > descriptor=ElementDescriptor[qname=TestBetwixtPersonBean,pname=null,cla
> > ss=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,singular=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,updater=null,wra
> > p=true]]
> > 00:27:48,052 DEBUG XMLIntrospector:475 - Used cached XMLBeanInfo.
> > 00:27:48,052 DEBUG XMLIntrospector:479 - XMLBeanInfo [class=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,
> > descriptor=ElementDescriptor[qname=TestBetwixtPersonBean,pname=null,cla
> > ss=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,singular=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,updater=null,wra
> > p=true]]
> > 00:27:48,062 DEBUG BeanReader:54 - Element Pushed: person
> > 00:27:48,062 DEBUG BeanReader:98 - Creating instance of class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean for element
> > person
> > 00:27:48,062 DEBUG XMLIntrospector:475 - Used cached XMLBeanInfo.
> > 00:27:48,062 DEBUG XMLIntrospector:479 - XMLBeanInfo [class=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,
> > descriptor=ElementDescriptor[qname=TestBetwixtPersonBean,pname=null,cla
> > ss=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,singular=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,updater=null,wra
> > p=true]]
> > 00:27:48,062 DEBUG BeanReader:84 - Marked: class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean
> > 00:27:48,062 DEBUG BeanReader:232 - Filling descriptor for: class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean
> > 00:27:48,062 DEBUG XMLIntrospector:475 - Used cached XMLBeanInfo.
> > 00:27:48,062 DEBUG XMLIntrospector:479 - XMLBeanInfo [class=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,
> > descriptor=ElementDescriptor[qname=TestBetwixtPersonBean,pname=null,cla
> > ss=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,singular=class
> > com.lantern.platform.tools.data.TestBetwixt$PersonBean,updater=null,wra
> > p=true]]
> > 00:27:48,062 DEBUG BeanReader:103 - Created bean
> > PersonBean[name='null',age='0']
> > 00:27:48,062 DEBUG sax:1316 -
> > startPrefixMapping(xml,http://www.w3.org/XML/1998/namespace)
> > 00:27:48,082 DEBUG sax:1316 -
> > startPrefixMapping(xmlns,http://www.w3.org/2000/xmlns/)
> > 00:27:48,082 DEBUG sax:1243 - startElement(,,age)
> > 00:27:48,082 DEBUG Digester:1250 - Pushing body text ''
> > 00:27:48,082 DEBUG Digester:1269 - New match='person/age'
> > 00:27:48,082 DEBUG Digester:1284 - Fire begin() for
> > [EMAIL PROTECTED]
> > 00:27:48,082 DEBUG sax:941 - characters(25)
> > 00:27:48,082 DEBUG sax:1009 - endElement(,,age)
> > 00:27:48,082 DEBUG Digester:1012 - match='person/age'
> > 00:27:48,082 DEBUG Digester:1013 - bodyText='25'
> > 00:27:48,082 DEBUG Digester:1035 - Fire body() for
> > [EMAIL PROTECTED]
> > 00:27:48,092 DEBUG BeanRuleSet:300 - [BRS] Body with text 25
> > 00:27:48,092 DEBUG MethodUpdater:73 - Converting primitive to int
> > 00:27:48,142 DEBUG ConvertUtils:418 - Convert string '25' to class
> > 'int'
> > 00:27:48,142 DEBUG ConvertUtils:426 - Using converter
> > [EMAIL PROTECTED]
> > 00:27:48,152 DEBUG MethodUpdater:106 - Calling setter method: setAge
> > on bean: PersonBean[name='null',age='0'] with new value: 25
> > 00:27:48,152 DEBUG Digester:1055 - Popping body text ''
> > 00:27:48,152 DEBUG Digester:1065 - Fire end() for
> > [EMAIL PROTECTED]
> > 00:27:48,152 DEBUG sax:1099 - endPrefixMapping(xml)
> > 00:27:48,152 DEBUG sax:1099 - endPrefixMapping(xmlns)
> > 00:27:48,152 DEBUG sax:1316 -
> > startPrefixMapping(xml,http://www.w3.org/XML/1998/namespace)
> > 00:27:48,152 DEBUG sax:1316 -
> > startPrefixMapping(xmlns,http://www.w3.org/2000/xmlns/)
> > 00:27:48,152 DEBUG sax:1243 - startElement(,,name)
> > 00:27:48,152 DEBUG Digester:1250 - Pushing body text ''
> > 00:27:48,152 DEBUG Digester:1269 - New match='person/name'
> > 00:27:48,152 DEBUG Digester:1284 - Fire begin() for
> > [EMAIL PROTECTED]
> > 00:27:48,152 DEBUG sax:941 - characters(James Smith)
> > 00:27:48,152 DEBUG sax:1009 - endElement(,,name)
> > 00:27:48,162 DEBUG Digester:1012 - match='person/name'
> > 00:27:48,162 DEBUG Digester:1013 - bodyText='James Smith'
> > 00:27:48,162 DEBUG Digester:1035 - Fire body() for
> > [EMAIL PROTECTED]
> > 00:27:48,162 DEBUG BeanRuleSet:300 - [BRS] Body with text James Smith
> > 00:27:48,162 DEBUG MethodUpdater:73 - Converting primitive to class
> > java.lang.String
> > 00:27:48,162 DEBUG ConvertUtils:418 - Convert string 'James Smith' to
> > class 'java.lang.String'
> > 00:27:48,162 DEBUG ConvertUtils:426 - Using converter
> > [EMAIL PROTECTED]
> > 00:27:48,162 DEBUG MethodUpdater:106 - Calling setter method: setName
> > on bean: PersonBean[name='null',age='25'] with new value: James Smith
> > 00:27:48,162 DEBUG Digester:1055 - Popping body text ''
> > 00:27:48,162 DEBUG Digester:1065 - Fire end() for
> > [EMAIL PROTECTED]
> > 00:27:48,162 DEBUG sax:1099 - endPrefixMapping(xml)
> > 00:27:48,162 DEBUG sax:1099 - endPrefixMapping(xmlns)
> > 00:27:48,162 DEBUG sax:1009 - endElement(,,person)
> > 00:27:48,162 DEBUG Digester:1012 - match='person'
> > 00:27:48,162 DEBUG Digester:1013 - bodyText=''
> > 00:27:48,162 DEBUG Digester:1035 - Fire body() for
> > [EMAIL PROTECTED]
> > 00:27:48,162 DEBUG BeanRuleSet:300 - [BRS] Body with text
> > 00:27:48,162 DEBUG Digester:1055 - Popping body text ''
> > 00:27:48,162 DEBUG Digester:1065 - Fire end() for
> > [EMAIL PROTECTED]
> > 00:27:48,172 ERROR Digester:1069 - End event threw exception
> > java.lang.ClassCastException
> > at
> > org.apache.commons.betwixt.io.read.ReadContext.getCurrentElement(ReadCo
> > ntext.java:224)
> > at
> > org.apache.commons.betwixt.io.read.BeanBindAction.update(BeanBindAction
> > .java:164)
> > at
> > org.apache.commons.betwixt.io.read.BeanBindAction.end(BeanBindAction.ja
> > va:154)
> > at
> > org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule.end(BeanRul
> > eSet.java:316)
> > at org.apache.commons.digester.Digester.endElement(Digester.java:1067)
> > at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown
> > Source)
> > at
> > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Un
> > known
> > Source)
> > at
> > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDi
> > spatcher.dispatch(Unknown
> > Source)
> > at
> > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unkn
> > own
> > Source)
> > at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
> > at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
> > at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
> > at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
> > at org.apache.commons.digester.Digester.parse(Digester.java:1610)
> > at
> > com.lantern.platform.tools.data.TestBetwixt.main(TestBetwixt.java:42)
> > java.lang.ClassCastException
> > at
> > org.apache.commons.digester.Digester.createSAXException(Digester.java:
> > 2792)
> > at
> > org.apache.commons.digester.Digester.createSAXException(Digester.java:
> > 2818)
> > at org.apache.commons.digester.Digester.endElement(Digester.java:1070)
> > at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown
> > Source)
> > at
> > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Un
> > known
> > Source)
> > at
> > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDi
> > spatcher.dispatch(Unknown
> > Source)
> > at
> > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unkn
> > own
> > Source)
> > at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
> > at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
> > at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
> > at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
> > at org.apache.commons.digester.Digester.parse(Digester.java:1610)
> > at
> > com.lantern.platform.tools.data.TestBetwixt.main(TestBetwixt.java:42)
> > Exception in thread "main"
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]