You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Nathalie Doremieux <na...@newsoftwaremarketing.com> on 2008/04/07 11:40:46 UTC

Betwixt: BeanReader setup question

Hi,

Currently the reader sends an exception when there is a mismatch between
the bean and the xml. is it possible to setup the reader to only send a
warning instead of an exception so that we can continue processing in
case there is an extra field and just ignore it?

thanks!
Nathalie D.



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


Re: Betwixt: BeanReader setup question

Posted by le...@email.arizona.edu.
Nathalie,

With betwixt configuration (either in a global, app-level config or on a per
class with <class>.betwixt), you can ask Betwixt to ignore properties when
writing out a bean and reading in a bean.

ExampleNode.betwixt:
<?xml version='1.0' encoding='UTF-8' ?>
<info primitiveTypes='element'>
   <hide property='position'/>
   <element name='exampleNode'>
     <addDefaults/>
   </element>
</info>

I'm by no means an expert on betwixt or BeanReader.  But I got things to work
with .betwixt files, so that's what I've used in the cases where I'm 
leveraging
Betwixt.

You'll find some more information about .betwixt configuration in the "Getting
Started" page on the Betwixt page:

   http://commons.apache.org/betwixt/guide/start.html

I looked on the site for into about the global config approach and 
couldn't find
it.  I might have pulled it from the Jakarta Commons O'Reilly book. I have
access to Safari Online through my ACM membership, so I used that book a bit
when I started out.

I hope that helps,

Andy

Quoting Nathalie Doremieux <na...@newsoftwaremarketing.com>:
> Currently the reader sends an exception when there is a mismatch between
> the bean and the xml. is it possible to setup the reader to only send a
> warning instead of an exception so that we can continue processing in
> case there is an extra field and just ignore it?




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


Re: Betwixt: BeanReader setup question

Posted by le...@email.arizona.edu.
Nathalie,

So I have an example working....

Given the input XML file (me.xml):
----------------------------------
<?xml version='1.0' encoding='UTF-8' ?>
<me id="he3432h11-032">
    <a name="rock hard" />
    <b name="monkey" />
    <c name="scam tactics" />
</me>

MeElement.java
----------------------------------
public class MeElement {
	private String id;
	private String nameA;
	private String nameC;
  /* I have getters/setters defined for omitting them here */
}

MeElement.betwixt
----------------------------------
<?xml version='1.0' encoding='UTF-8' ?>
<info primitiveTypes="element">
   <element name="me">
     <attribute name="id" property="id" />
     <element name="a">
       <attribute name="name" property="nameA" />
     </element>
     <element name="c">
       <attribute name="name" property="nameC" />
     </element>
     <addDefaults/>
   </element>
</info>

Remember that the .betwixt file needs to be in the same directory that your
.java file is.  Betwixt will pick up both theses files since they're on the
classpath together.

I'll assume that you have JUnit setup (because we all know that testing is
important...even former .NET people like me).  So I have a subclass of 
TestCase
with the following method defined:

public void testMeDocumentLoad() {
   try { // this assumes that 'me.xml' is sitting on the classpath
     InputStream xtnodes = getClass().getResourceAsStream("./me.xml");
     BeanReader beanReader = new BeanReader();
     beanReader.registerBeanClass(MeElement.class);
     MeElement root = (MeElement)beanReader.parse(xtnodes);
     assertNotNull(root);
     // add further testing to ensure that the bean/pojo has been loaded as
expected
     System.out.println(root);
   } catch (Exception e) {
     e.printStackTrace();
     System.out.println("exception occured... lame");
   }
}

Betwixt does have log4j capabilities (apparently).  I tried to 
configure it for
my unit test but I'm not getting any INFO output.  I'm not really too familiar
with log4j - so I can't really offer any help.  I just was able to 
confirm that
you should be able to get DEBUG/INFO/WARN information about Betwixt via log4j.

I hope that helps,

Andy

/****************************************
  * @author:  Andrew Lenards
  * @email:   lenards@email.arizona.edu
  * @title:   Systems Programmer, Principal
  * @project: Tree of Life Web Project
  * @website: http://tolweb.org/
  * @dept:    Dept. of Entomology
  * @school:  University of Arizona
  * @geo-loc: Tucson, AZ 85721
  ***************************************/


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


Re: Betwixt: BeanReader setup question

Posted by le...@email.arizona.edu.
Nathalie,

I've been very frustrated with the fact that I can't get adequate 
feedback from
Betwixt.  I haven't looked into if there are logging settings that can be
tweaked to get more feedback, if anyone else knows - that would be a nice bit
of information. I come from a .NET background, so I'm not overly familiar with
things that most developers that use Commons take for granted.

I realized now that it would be helpful to have an idea about your 
pojo/bean(s).

Let's work up a better example than what we have here:

Your Example 1
>
> <me>
>     <a/>
>     <b/>
>     <c/>
> </me>

...because I don't see the values that would be store in the bean really. An
instance of 'me' with a class called 'a' and one called 'b' and another 
'c'.  I
mean, it's not very to me what is being modeled nor is what is just structure
and what is data.  I appreciate a simple, concise example when asking 
questions
- but this is so terse that the meaning and intent is not clear to me.

It would be nice to flush something out like:

<me id="he3432h11-032">
    <a name="rock hard" />
    <b name="monkey" />
    <c name="scam tactics" />
</me>

Now we've got some data.  We can play with this a bit more now. You 
mention that
your beans don't have anything in them - but in that first, concise example
(Your Example 1) there really isn't any data to be stored in a bean.

For this - below - I would expect an exception to be thrown by the underlying
XML parser.  Remember, a valid, well-formed XML document must have only one
root.  This violates that rule.

> <me>
>     <a/>
>     <b/>
>     <c/>
> </me>
> <me>
>     <a/>
>     <b/>
>     <c/>
> </me>

So let me know if my take on your example is okay - or point out a better
example.  And then would you provide the pojo/bean(s) involved along 
with their
mapping.

Andy

/****************************************
  * @author:  Andrew Lenards
  * @email:   lenards@email.arizona.edu
  * @title:   Systems Programmer, Principal
  * @project: Tree of Life Web Project
  * @website: http://tolweb.org/
  * @dept:    Dept. of Entomology
  * @school:  University of Arizona
  * @geo-loc: Tucson, AZ 85721
  ***************************************/


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


Re: Betwixt: BeanReader setup question

Posted by Nathalie Doremieux <na...@newsoftwaremarketing.com>.
Andy,


thanks, the wiki is back up. maybe I need to change something in the 
digester (but I don't know what and how) but it's a problem that betwixt 
does not send any error/exception/warning in the following example:

I have an XML that looks like this:

<me>
    <a/>
    <b/>
    <c/>
</me>

and the bean mapped to this guy does not know about b, then I have no 
error, no message, my bean is just screwed up (I only have a value for 
a), which is a pb because I am not notified that there was a pb and that 
c could not be mapped.
it is even worst when I have 2 elements <me> like this

<me>
    <a/>
    <b/>
    <c/>
</me>
<me>
    <a/>
    <b/>
    <c/>
</me>

in that case, I have absolutely nothing in the beans.  what I would like 
to do is handle it in my code, but if I don't get notified that 
something went wrong I can't do anything. it seems to me that it's a 
problem in betwixt to not report a problem by throwing an exception for 
example. betwixt should be able to tell me that something went wrong. 
don't you think?

thanks for your help!

Nathalie


lenards@email.arizona.edu wrote:
> Nathalie,
>
> Okay - so you have some input XML like:
>
> <root>
>   <element a="A" b="B" c="C" />
> </root>
>
> or
>
> <root>
>   <A/>
>   <B/>
>   <C/>
> </root>
>
> You're reading in this input xml file and you're getting an error 
> because your
> class has no property mapping for the attribute 'b' or element 'B'.
>
> Is that correct?
>
> One sort of mindless approach (a hack, kludge, etc) would be to 
> subclass you
> mapping class and the property for B - just to get this to read in.  
> This class
> serves the role of the superclass - and that'll filtering out B while 
> getting
> your the information you need.  That's a quick fix - which I'd likely be
> attacked for suggesting if more people read the Betwixt questions (not 
> say they
> don't - just not too many seem to answer them if they are reading).
>
> Question: will you have to output your mapped beans in this XML format?
>
> If there answer is 'No', then you would likely want to look at what 
> Betwixt is
> built on: Digester.  If that's a 'Yes' - then you'd like to take in this
> information to output later.  I'm assuming since you're looking to 
> filter out
> the input XML - the answer is 'No.'
>
> So - Betwixt is built on Digester.  Digester uses a set of rules to 
> specify the
> mapping from xml elements and attributes into beans.  Betwixt is 
> generating
> these rules for you in a custom ruleset (BeanRuleSet).  I believe that 
> what you
> want to do would be look at how you define Rules in Digester to ignore 
> this.
>
> And that makes sense because you originally asked the question about both
> Digester and Betwixt.
>
> I'd take a stab at what you need to do in Digester but their Wiki is 
> down right
> now =( The Advanced "Reading Beans" section for Betwixt mentions 
> "Adding Custom
> Digestion Rules" : 
> http://commons.apache.org/betwixt/guide/reading.html (but the
> section just sort of points you to the Digester page, which is rather 
> thin - it
> looks like the meatier content is on the wiki, again, which is down).  
> I'd look
> at that.  I haven't delved into Digester so I can't offer much help
>
> Eventually - the wiki should be back up at some point:
>
> http://wiki.apache.org/commons/Digester
>
> Sorry I can't offer a full solution.
>
> Andy
>
> Quoting Nathalie Doremieux <na...@newsoftwaremarketing.com>:
>
>> Andy,
>>
>> thanks for your answer but I  need to be able to ignore fields on the
>> XML side, not on the bean side.
>> I think I can better explain with an example
>>
>> My bean has 2 elements
>> A
>> C
>>
>> my XML has 3 elements
>> A
>> B
>> C
>>
>> with that configuration I would like to be able to read A and C into my
>> bean, but right now I only get A. I basically would like to be able to
>> tell betwixt to ignore B in the XML as it does not map to anything in
>> the bean. Is that possible? I know I can also write an XML validator and
>> make sure my XML is the right format, but if there is a solution within
>> betwixt it would be nice to know!
>>
>> thanks for you help!
>>
>> Nathalie
>
>
> /****************************************
>  * @author:  Andrew Lenards
>  * @email:   lenards@email.arizona.edu
>  * @title:   Systems Programmer, Principal
>  * @project: Tree of Life Web Project
>  * @website: http://tolweb.org/
>  * @dept:    Dept. of Entomology
>  * @school:  University of Arizona
>  * @geo-loc: Tucson, AZ 85721
>  ***************************************/
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>


-- 
Nathalie Doremieux
New Software Marketing

www.newsoftwaremarketing.com



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


Re: Betwixt: BeanReader setup question

Posted by le...@email.arizona.edu.
Nathalie,

Okay - so you have some input XML like:

<root>
   <element a="A" b="B" c="C" />
</root>

or

<root>
   <A/>
   <B/>
   <C/>
</root>

You're reading in this input xml file and you're getting an error because your
class has no property mapping for the attribute 'b' or element 'B'.

Is that correct?

One sort of mindless approach (a hack, kludge, etc) would be to subclass you
mapping class and the property for B - just to get this to read in.  
This class
serves the role of the superclass - and that'll filtering out B while getting
your the information you need.  That's a quick fix - which I'd likely be
attacked for suggesting if more people read the Betwixt questions (not 
say they
don't - just not too many seem to answer them if they are reading).

Question: will you have to output your mapped beans in this XML format?

If there answer is 'No', then you would likely want to look at what Betwixt is
built on: Digester.  If that's a 'Yes' - then you'd like to take in this
information to output later.  I'm assuming since you're looking to filter out
the input XML - the answer is 'No.'

So - Betwixt is built on Digester.  Digester uses a set of rules to 
specify the
mapping from xml elements and attributes into beans.  Betwixt is generating
these rules for you in a custom ruleset (BeanRuleSet).  I believe that 
what you
want to do would be look at how you define Rules in Digester to ignore this.

And that makes sense because you originally asked the question about both
Digester and Betwixt.

I'd take a stab at what you need to do in Digester but their Wiki is 
down right
now =( The Advanced "Reading Beans" section for Betwixt mentions 
"Adding Custom
Digestion Rules" : http://commons.apache.org/betwixt/guide/reading.html 
(but the
section just sort of points you to the Digester page, which is rather 
thin - it
looks like the meatier content is on the wiki, again, which is down).  
I'd look
at that.  I haven't delved into Digester so I can't offer much help

Eventually - the wiki should be back up at some point:

http://wiki.apache.org/commons/Digester

Sorry I can't offer a full solution.

Andy

Quoting Nathalie Doremieux <na...@newsoftwaremarketing.com>:

> Andy,
>
> thanks for your answer but I  need to be able to ignore fields on the
> XML side, not on the bean side.
> I think I can better explain with an example
>
> My bean has 2 elements
> A
> C
>
> my XML has 3 elements
> A
> B
> C
>
> with that configuration I would like to be able to read A and C into my
> bean, but right now I only get A. I basically would like to be able to
> tell betwixt to ignore B in the XML as it does not map to anything in
> the bean. Is that possible? I know I can also write an XML validator and
> make sure my XML is the right format, but if there is a solution within
> betwixt it would be nice to know!
>
> thanks for you help!
>
> Nathalie


/****************************************
  * @author:  Andrew Lenards
  * @email:   lenards@email.arizona.edu
  * @title:   Systems Programmer, Principal
  * @project: Tree of Life Web Project
  * @website: http://tolweb.org/
  * @dept:    Dept. of Entomology
  * @school:  University of Arizona
  * @geo-loc: Tucson, AZ 85721
  ***************************************/


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


Re: Betwixt: BeanReader setup question

Posted by Nathalie Doremieux <na...@newsoftwaremarketing.com>.
Andy,

thanks for your answer but I  need to be able to ignore fields on the 
XML side, not on the bean side.
I think I can better explain with an example

My bean has 2 elements
A
C

my XML has 3 elements
A
B
C

with that configuration I would like to be able to read A and C into my 
bean, but right now I only get A. I basically would like to be able to 
tell betwixt to ignore B in the XML as it does not map to anything in 
the bean. Is that possible? I know I can also write an XML validator and 
make sure my XML is the right format, but if there is a solution within 
betwixt it would be nice to know!

thanks for you help!

Nathalie

lenards@email.arizona.edu wrote:
> Nathalie,
>
> I was reading some examples and stumbled onto something.
>
> If you want to course-grain ignoring of properties, you can provide a
> suppression strategy to BeanReader that will cause all properties with 
> that
> name to be skipped.
>
> http://commons.apache.org/betwixt/guide/binding.html
>
> search for Ignoring Properties
>
> Andy
>
>
> /****************************************
>  * @author:  Andrew Lenards
>  * @email:   lenards@email.arizona.edu
>  * @title:   Systems Programmer, Principal
>  * @project: Tree of Life Web Project
>  * @website: http://tolweb.org/
>  * @dept:    Dept. of Entomology
>  * @school:  University of Arizona
>  * @geo-loc: Tucson, AZ 85721
>  ***************************************/
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>


-- 
Nathalie Doremieux
New Software Marketing

www.newsoftwaremarketing.com



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


Re: Betwixt: BeanReader setup question

Posted by le...@email.arizona.edu.
Nathalie,

I was reading some examples and stumbled onto something.

If you want to course-grain ignoring of properties, you can provide a
suppression strategy to BeanReader that will cause all properties with that
name to be skipped.

http://commons.apache.org/betwixt/guide/binding.html

search for Ignoring Properties

Andy


/****************************************
  * @author:  Andrew Lenards
  * @email:   lenards@email.arizona.edu
  * @title:   Systems Programmer, Principal
  * @project: Tree of Life Web Project
  * @website: http://tolweb.org/
  * @dept:    Dept. of Entomology
  * @school:  University of Arizona
  * @geo-loc: Tucson, AZ 85721
  ***************************************/


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