users@jaxb.java.net

RE: JAXB 2.0 default values

From: Scott Allan <sallan1000_at_aol.com>
Date: Wed, 7 Sep 2005 23:17:41 -0500

I'm also confused about how to obtain the default value I set in the
schema.



<xs:element name="test" type="xs:string" default="testvalue"/>



I see the field is annotated like this so the default is getting
recognized:

    @XmlElement(defaultValue = "testvalue")



I thought I could get it from CPropertyInfo.defaultValue and then set
the field using JFieldVar.init() since it seems to take JExpression as a
parameter but I got no result:

             for( CPropertyInfo p : co.target.getProperties() ) {

                String fieldName2 = p.getName(false);

                  for (JFieldVar field : co.implClass.fields()) {

                    if (field.name().equals(fieldName2)){

                      field.init(p.defaultValue);

                    }

                  }

              }



There doesn't seem to be a way to display a JExpression once it has been
set so I can't really check what is in there.



field.init(JExpr.ref("testvalue")) adds a default value of "testvalue"
to the fields and field.init(JExpr.lit(0)) adds a value of zero so I'm
pretty sure I need to invoke this method somehow but not sure where to
find the default value from the schema.





I see no way of accessing the schema org.w3c.dom Nodes for a class to
check the default attribute except for when examining the plugin
customizations. I don't want to annotate every element though.

          for (CPluginCustomization c : cList) {

          c.markAsAcknowledged();

          Element e = c.element;

            if (e.getLocalName().equals("annotation")){

                if (e.hasAttribute("default"))

...



> -----Original Message-----

> From: Scott Allan [mailto:sallan1000_at_aol.com]

> Sent: Wednesday, September 07, 2005 9:32 PM

> To: users_at_jaxb.dev.java.net

> Subject: RE: JAXB 2.0 default values

>

>

> I'm trying to build a plugin to add the default values from

> the schema to my bean. I can't seem to find an easy way to

> obtain the field that corresponds to the property without

> checking all of them. There is no

> getField() method that I can find. The field() methods all

> seem to be for creating new fields.

>

>

>

> for( CPropertyInfo p : co.target.getProperties() ) {

>

> String fieldName = p.getName(true);

>

> String fieldName2 = p.getName(false);

>

> // would like to use something like

> co.implClass.getField(...) here.

>

> // instead need to check all fields

>

> for (JFieldVar field : co.implClass.fields())

>

> {

>

> if (field.name().equals(fieldName2))

>

> System.out.println("Set field.init value

> here for "+field.name());

>

> }

>

> }

>

>

>




  _____

<< ella for Spam Control >> has removed 26809 Spam messages and set
aside 2 Newsletters for me
You can use it too - and it's FREE! www.ellaforspam.com