You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Ryan Crumley <cr...@gmail.com> on 2004/12/13 07:30:35 UTC

ClassCastException with betwixt 0.6

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=MethodUpdater
[method=public void
com.lantern.platform.tools.data.TestBetwixt$PersonBean.setAge(int)],wrap=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.lang.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=MethodUpdater
[method=public void
com.lantern.platform.tools.data.TestBetwixt$PersonBean.setAge(int)],wrap=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.lang.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,wrap=true]
00:27:47,772 DEBUG XMLIntrospector:479 - XMLBeanInfo [class=class
com.lantern.platform.tools.data.TestBetwixt$PersonBean,
descriptor=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,wrap=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:org.apache.commons.betwixt.io.BeanReader@152544e
00:27:48,002 DEBUG sax:1180 -
setDocumentLocator(org.apache.xerces.parsers.AbstractSAXParser$LocatorProxy@a3d4cf)
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
org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
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,class=class
com.lantern.platform.tools.data.TestBetwixt$PersonBean,singular=class
com.lantern.platform.tools.data.TestBetwixt$PersonBean,updater=null,wrap=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,class=class
com.lantern.platform.tools.data.TestBetwixt$PersonBean,singular=class
com.lantern.platform.tools.data.TestBetwixt$PersonBean,updater=null,wrap=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,class=class
com.lantern.platform.tools.data.TestBetwixt$PersonBean,singular=class
com.lantern.platform.tools.data.TestBetwixt$PersonBean,updater=null,wrap=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,class=class
com.lantern.platform.tools.data.TestBetwixt$PersonBean,singular=class
com.lantern.platform.tools.data.TestBetwixt$PersonBean,updater=null,wrap=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
org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
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
org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
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
org.apache.commons.beanutils.converters.IntegerConverter@148bd3
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
org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
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
org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
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
org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
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
org.apache.commons.beanutils.converters.StringConverter@19d0a1
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
org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
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
org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
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
org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
00:27:48,172 ERROR Digester:1069 - End event threw exception
java.lang.ClassCastException
	at org.apache.commons.betwixt.io.read.ReadContext.getCurrentElement(ReadContext.java:224)
	at org.apache.commons.betwixt.io.read.BeanBindAction.update(BeanBindAction.java:164)
	at org.apache.commons.betwixt.io.read.BeanBindAction.end(BeanBindAction.java:154)
	at org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule.end(BeanRuleSet.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(Unknown
Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
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(Unknown
Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: ClassCastException with betwixt 0.6

Posted by Ryan Crumley <cr...@gmail.com>.
Another tip users may find useful when they receive this exception:

java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
	at org.apache.commons.digester.Digester.createSAXException(Digester.java:2792)

Make sure that DEBUG is not enabled in your log4j.properties. Code
that normally completes without error causes that exception when I
have DEBUG logging enabled. I have filed a defect (# 32743 ).

ryan


On Thu, 16 Dec 2004 17:15:42 -0600, Ryan Crumley <cr...@gmail.com> wrote:
> Thanks a bunch for helping me out. I was able to get the example
> working. It seems my problem was related to classpath and not using
> the correct dependencies. I ran "ant get-deps" to create target/lib
> and copied those jars to my sandbox lib directory. After doing that
> the example code ran as expected.
> 
> Maven looks very interesting. Up until this point I had only used ant
> as a build tool.
> 
> Again thanks for the tips and patience.
> 
> ryan
> 
> On Thu, 16 Dec 2004 22:25:23 +0000, robert burrell donkin
> <ro...@blueyonder.co.uk> wrote:
> > hi ryan
> >
> > On 16 Dec 2004, at 05:19, Ryan Crumley wrote:
> >
> > > I was quite excited that the problem may have been found however I
> > > changed the code so that the configuration object is shared between
> > > the writer and reader and I am still receiving a null object back from
> > > the reader. The new code is listed below.
> > >
> > > The output when running the program is:
> > >
> > >    out: <?xml version='1.0'?><PersonBean age="24" name="ryan c"
> > > id="1"/>
> > >    null
> >
> > hmmm...
> >
> > i've run the same code against CVS HEAD in eclipse and i get:
> >
> > out: <?xml version='1.0'?><PersonBean age="24" name="ryan c" id="1"/>
> >
> > [org.apache.commons.betwixt.PersonBean: age=24 name=ryan c]
> >
> >
> > > I am still at a loss why this simple example is not working. The only
> > > thing I can think of (unless someone sees a problem with the code I am
> > > using below) is that my jar versions are not correct. However I used
> > > the same jars that maven appears to use when running regression (which
> > > passes). Any ideas on where to go next?
> >
> > this seems very strange.
> >
> > i suspect that it's a problem with the classpath: an old jar getting in
> > or something. if you have CVS HEAD checked out then an easy way to test
> > this hypothesis is to add your code as a test case into src/test.
> > provided that it's named TestXXX then maven will automatically find it
> > and run it (as a unit test). (we generally use test driven development
> > on betwixt so this is the way that all new code starts and so it's the
> > way that we're most confident about the configuration).
> >
> > FYI maven builds into target. running maven should generate a
> > commons-betwixt-0.6.1-dev.jar in there. that's the actual library code.
> > the test classes are built into test-classes.
> >
> > - robert
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >
>

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


Re: ClassCastException with betwixt 0.6

Posted by Ryan Crumley <cr...@gmail.com>.
Thanks a bunch for helping me out. I was able to get the example
working. It seems my problem was related to classpath and not using
the correct dependencies. I ran "ant get-deps" to create target/lib
and copied those jars to my sandbox lib directory. After doing that
the example code ran as expected.

Maven looks very interesting. Up until this point I had only used ant
as a build tool.

Again thanks for the tips and patience.

ryan


On Thu, 16 Dec 2004 22:25:23 +0000, robert burrell donkin
<ro...@blueyonder.co.uk> wrote:
> hi ryan
> 
> On 16 Dec 2004, at 05:19, Ryan Crumley wrote:
> 
> > I was quite excited that the problem may have been found however I
> > changed the code so that the configuration object is shared between
> > the writer and reader and I am still receiving a null object back from
> > the reader. The new code is listed below.
> >
> > The output when running the program is:
> >
> >    out: <?xml version='1.0'?><PersonBean age="24" name="ryan c"
> > id="1"/>
> >    null
> 
> hmmm...
> 
> i've run the same code against CVS HEAD in eclipse and i get:
> 
> out: <?xml version='1.0'?><PersonBean age="24" name="ryan c" id="1"/>
> 
> [org.apache.commons.betwixt.PersonBean: age=24 name=ryan c]
> 
> 
> > I am still at a loss why this simple example is not working. The only
> > thing I can think of (unless someone sees a problem with the code I am
> > using below) is that my jar versions are not correct. However I used
> > the same jars that maven appears to use when running regression (which
> > passes). Any ideas on where to go next?
> 
> this seems very strange.
> 
> i suspect that it's a problem with the classpath: an old jar getting in
> or something. if you have CVS HEAD checked out then an easy way to test
> this hypothesis is to add your code as a test case into src/test.
> provided that it's named TestXXX then maven will automatically find it
> and run it (as a unit test). (we generally use test driven development
> on betwixt so this is the way that all new code starts and so it's the
> way that we're most confident about the configuration).
> 
> FYI maven builds into target. running maven should generate a
> commons-betwixt-0.6.1-dev.jar in there. that's the actual library code.
> the test classes are built into test-classes.
> 
> - robert
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
>

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


Re: ClassCastException with betwixt 0.6

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
hi ryan

On 16 Dec 2004, at 05:19, Ryan Crumley wrote:

> I was quite excited that the problem may have been found however I
> changed the code so that the configuration object is shared between
> the writer and reader and I am still receiving a null object back from
> the reader. The new code is listed below.
>
> The output when running the program is:
>
>    out: <?xml version='1.0'?><PersonBean age="24" name="ryan c" 
> id="1"/>
>    null

hmmm...

i've run the same code against CVS HEAD in eclipse and i get:

out: <?xml version='1.0'?><PersonBean age="24" name="ryan c" id="1"/>

[org.apache.commons.betwixt.PersonBean: age=24 name=ryan c]


> I am still at a loss why this simple example is not working. The only
> thing I can think of (unless someone sees a problem with the code I am
> using below) is that my jar versions are not correct. However I used
> the same jars that maven appears to use when running regression (which
> passes). Any ideas on where to go next?

this seems very strange.

i suspect that it's a problem with the classpath: an old jar getting in 
or something. if you have CVS HEAD checked out then an easy way to test 
this hypothesis is to add your code as a test case into src/test. 
provided that it's named TestXXX then maven will automatically find it 
and run it (as a unit test). (we generally use test driven development 
on betwixt so this is the way that all new code starts and so it's the 
way that we're most confident about the configuration).

FYI maven builds into target. running maven should generate a 
commons-betwixt-0.6.1-dev.jar in there. that's the actual library code. 
the test classes are built into test-classes.

- robert


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


Re: ClassCastException with betwixt 0.6

Posted by Ryan Crumley <cr...@gmail.com>.
I was quite excited that the problem may have been found however I
changed the code so that the configuration object is shared between
the writer and reader and I am still receiving a null object back from
the reader. The new code is listed below.

The output when running the program is:

   out: <?xml version='1.0'?><PersonBean age="24" name="ryan c" id="1"/>
   null

I am still at a loss why this simple example is not working. The only
thing I can think of (unless someone sees a problem with the code I am
using below) is that my jar versions are not correct. However I used
the same jars that maven appears to use when running regression (which
passes). Any ideas on where to go next?

Thanks,
ryan

public static final void main(String args[]) throws Exception{
        StringWriter out = new StringWriter();
        out.write("<?xml version='1.0'?>");

        BeanWriter writer = new BeanWriter(out);
        
        IntrospectionConfiguration configuration =
writer.getXMLIntrospector().getConfiguration();
        configuration.setAttributesForPrimitives(true);
        configuration.setWrapCollectionsInElement(false);
        
        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().setConfiguration(configuration);
        reader.registerBeanClass(PersonBean.class);
        
        PersonBean person = (PersonBean) reader.parse(xmlReader);
        
        // send bean to system out
        System.out.println(person);
    }



On Wed, 15 Dec 2004 09:41:53 +0000, robert burrell donkin
<ro...@blueyonder.co.uk> wrote:
> hi ryan
> 
> i'm afraid you're going to kick yourself ;)
> 
> On 15 Dec 2004, at 05:45, Ryan Crumley wrote:
> 
> <snip>
> 
> > 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().setAttributesForPrimitiv
> > es(true);
>                                                  
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> this line means that the writing is configured to write primitives as
> elements but the reading configuration is expecting them to be
> attributes.
> 
> a couple of related tips:
> 
> * using the same IntrospectionConfiguration instance for the reader and
> writer will ensure that the configurations are identical;
> * XMLIntrospector caches the results of introspection (which takes
> quite a bit of time) and so as long as readers and writers share the
> same configuration, you'll get better performance if they share the
> same instance;
> 
> - robert
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
>

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


Re: ClassCastException with betwixt 0.6

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
hi ryan

i'm afraid you're going to kick yourself ;)

On 15 Dec 2004, at 05:45, Ryan Crumley wrote:

<snip>

> 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().setAttributesForPrimitiv 
> es(true);
                                                  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

this line means that the writing is configured to write primitives as  
elements but the reading configuration is expecting them to be  
attributes.

a couple of related tips:

* using the same IntrospectionConfiguration instance for the reader and  
writer will ensure that the configurations are identical;
* XMLIntrospector caches the results of introspection (which takes  
quite a bit of time) and so as long as readers and writers share the  
same configuration, you'll get better performance if they share the  
same instance;

- robert


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


Re: ClassCastException with betwixt 0.6

Posted by Ryan Crumley <cr...@gmail.com>.
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
<ro...@blueyonder.co.uk> 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:org.apache.commons.betwixt.io.BeanReader@152544e
> > 00:27:48,002 DEBUG sax:1180 -
> > setDocumentLocator(org.apache.xerces.parsers.AbstractSAXParser$LocatorP
> > roxy@a3d4cf)
> > 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
> > org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> > 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
> > org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> > 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
> > org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> > 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
> > org.apache.commons.beanutils.converters.IntegerConverter@148bd3
> > 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
> > org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> > 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
> > org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> > 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
> > org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> > 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
> > org.apache.commons.beanutils.converters.StringConverter@19d0a1
> > 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
> > org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> > 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
> > org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> > 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
> > org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> > 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: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
>

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


Re: ClassCastException with betwixt 0.6

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
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:org.apache.commons.betwixt.io.BeanReader@152544e
> 00:27:48,002 DEBUG sax:1180 -
> setDocumentLocator(org.apache.xerces.parsers.AbstractSAXParser$LocatorP 
> roxy@a3d4cf)
> 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
> org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> 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
> org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> 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
> org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> 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
> org.apache.commons.beanutils.converters.IntegerConverter@148bd3
> 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
> org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> 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
> org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> 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
> org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> 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
> org.apache.commons.beanutils.converters.StringConverter@19d0a1
> 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
> org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> 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
> org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> 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
> org.apache.commons.betwixt.io.BeanRuleSet$ActionMappingRule@1cdeff
> 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: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>


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