How to support javax.xml.datatype.XMLGregorianCalendar ?

2,789 views
Skip to first unread message

lmu

unread,
Mar 25, 2015, 11:35:20 AM3/25/15
to po...@googlegroups.com
Hi there,

First of all, thanks or this really helpful project.

I'm sung PODAM to fill JAXB generated obeject, one of the XML field is a date and this date is bind to a javax.xml.datatype.XMLGregorianCalendar object.
When podam tries fill generate a value for that field I get this warnings, and of course my field stay null.

Mar 25, 2015 3:21:35 PM uk.co.jemos.podam.api.PodamFactoryImpl populatePojo
WARNING: Couldn't find a suitable value for attribute class ocpp.cs._2_0_0.ChargingProfile[class javax.xml.datatype.XMLGregorianCalendar]. It will be left to null.
15:21:35,177  WARN CentralSystemService:122 - No default message found for [MeterValuesResponse], generate it
15:21:35,193  WARN CentralSystemService:122 - No default message found for [BootNotificationResponse], generate it
Mar 25, 2015 3:21:35 PM uk.co.jemos.podam.api.LoggingExternalFactory manufacturePojo
WARNING: Cannot instantiate class javax.xml.datatype.XMLGregorianCalendar with arguments []. Returning null.
Mar 25, 2015 3:21:35 PM uk.co.jemos.podam.api.PodamFactoryImpl instantiatePojoWithoutConstructors
WARNING: For class class javax.xml.datatype.XMLGregorianCalendar PODAM could not possibly create a value. It will be returned as null.


So my question is, how can I handle this particular type ? Can I make a kind of javax.xml.datatype.XMLGregorianCalendar handler in PODAM ?

Thanks.

lmu

unread,
Mar 26, 2015, 5:27:34 AM3/26/15
to po...@googlegroups.com
To go further I've successfully used @PodamStrategyValue(XMLGregorianCalendarStrategy.class) on the generated code.
And my question is more "How to do the same thing without the annotation" ?

For now my only choice is to process the generated code and add the annotation, do we have another mean to do this ?


Thanks.

Marco Tedone

unread,
Mar 26, 2015, 8:24:20 AM3/26/15
to po...@googlegroups.com
Hi, there is a test with an example of how to achieve this? 

Regards, 
--
You received this message because you are subscribed to the Google Groups "PODAM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to podam+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniil Ivanov

unread,
Mar 26, 2015, 3:31:32 PM3/26/15
to po...@googlegroups.com
Hi,

XMLGregorianCalendar is an abstract class, which PODAM cannot instantiate. So PODAM
will consult a DataProviderStrategy for a specific implementation to instantiate it
instead. Thus, define implementation extending XMLGregorianCalendar,
with static method returning instance

public static class XMLGregorianCalendarImpl extends XMLGregorianCalendar {

   
public static XMLGregorianCalendar getInstance() throws DatatypeConfigurationException {
       
DatatypeFactory factory = DatatypeFactory.newInstance();
       
return factory.newXMLGregorianCalendar(new GregorianCalendar());
   
}

   
...
}

Get reference to a DataProviderStrategy (there are many ways to do that)

RandomDataProviderStrategy strategy = RandomDataProviderStrategy.getInstance();
PodamFactory podam = new PodamFactoryImpl(strategy);


Map XMLGregorianCalendar to XMLGregorianCalendarImpl

strategy.addSpecific(XMLGregorianCalendar.class, XMLGregorianCalendarImpl.class);


Now it works:

XMLGregorianCalendar pojo = podam.manufacturePojo(XMLGregorianCalendar.class);

Thanks, Daniil

lmu

unread,
Mar 27, 2015, 5:18:33 AM3/27/15
to po...@googlegroups.com
Just perfect !
I works now ;)

lmu

unread,
Mar 27, 2015, 6:59:00 AM3/27/15
to po...@googlegroups.com
I've been to quick to celebrate...
I've post an extract of my project I'm sure I'm missing a dumb thing but I can't figure wich one ><

I've add the specific class handler but it seems it is not called by podam.


Podam.zip

Daniil Ivanov

unread,
Mar 27, 2015, 5:06:09 PM3/27/15
to po...@googlegroups.com
Right, it doesn't work outside of PODAM package...

Then use external factory

private final static PodamFactory externalFactory =
       
new PodamFactory() {

           
@Override
           
public <T> T manufacturePojo(Class<T> pojoClass,
                   
Type... genericTypeArgs) {
               
try {
                   
if (pojoClass.isAssignableFrom(XMLGregorianCalendar.class)) {
                       
DatatypeFactory factory = DatatypeFactory.newInstance();
                       
@SuppressWarnings("unchecked")
                        T calendar
= (T) factory.newXMLGregorianCalendar(new GregorianCalendar());
                        LOG
.info("Externally created XMLGregorianCalendar");
                       
return calendar;
                   
} else if (pojoClass.isAssignableFrom(Duration.class)) {
                       
DatatypeFactory factory = DatatypeFactory.newInstance();
                       
@SuppressWarnings("unchecked")
                        T duration
= (T) factory.newDuration(0L);
                        LOG
.info("Externally created Duration");
                       
return duration;
                   
}
               
} catch (Exception e) {
                   
throw new IllegalStateException("Manufacturing failed", e);
               
}
               
return null;
           
}

           
@Override
           
public <T> T manufacturePojo(Class<T> pojoClass) {
               
return this.manufacturePojo(pojoClass, NO_TYPES);
           
}

           
@Override
           
public <T> T manufacturePojoWithFullData(Class<T> pojoClass,
                   
Type... genericTypeArgs) {
               
return this.manufacturePojo(pojoClass, genericTypeArgs);
           
}

           
@Override
           
public DataProviderStrategy getStrategy() {
               
return null;
           
}

           
@Override
           
public ClassInfoStrategy getClassStrategy() {
               
return null;
           
}

           
@Override
           
public PodamFactory setClassStrategy(
                   
ClassInfoStrategy classInfoStrategy) {
               
return null;
           
}
};

Register external factory to PODAM factory:

private final static PodamFactory podam = new PodamFactoryImpl(externalFactory);

Now the external factory will be handling all requests PODAM factory cannot handle:

XMLGregorianCalendar pojo = podam.manufacturePojo(XMLGregorianCalendar.class);

Unfortunately, XMLGregorianCalendar throws PodamMockeryException. It was fixed in PODAM 5.2.0-SNAPSHOT (and will be in upcoming release).

lmu

unread,
Mar 30, 2015, 5:55:12 AM3/30/15
to po...@googlegroups.com
This one works perfectly !
Great job, thanks.


Le mercredi 25 mars 2015 16:35:20 UTC+1, lmu a écrit :

Marco Tedone

unread,
Mar 30, 2015, 10:47:39 AM3/30/15
to po...@googlegroups.com
I've made another change and introduced an Abstractexternalfactory that takes care of all the boilerplate code. Currently available from 5.2.1-SNAPSHOT. Will be released with 5.2.1.RELEASE
--

alessandro mancini

unread,
Jul 2, 2015, 4:22:35 AM7/2/15
to po...@googlegroups.com
Hello! I'm using 5.5.1.RELEASE Podam version. 
I'm tryng to do this:

RandomDataProviderStrategy strategy = new RandomDataProviderStrategyImpl();
strategy.addOrReplaceSpecific(XMLGregorianCalendar.class, XMLGregorianCalendarImpl.class);
   
PodamFactory factory = new PodamFactoryImpl(strategy);
XMLGregorianCalendar pojo = factory.manufacturePojo(XMLGregorianCalendar.class);


where XMLGregorianCalendarImpl is com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.
My code throws an Exception:

Exception in thread "main" uk.co.jemos.podam.exceptions.PodamMockeryException
at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojo(PodamFactoryImpl.java:182)
at it.sogei.podam.App.main(App.java:35)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at uk.co.jemos.podam.api.PodamFactoryImpl.populatePojoInternal(PodamFactoryImpl.java:1407)
at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojoInternal(PodamFactoryImpl.java:1249)
at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojoInternal(PodamFactoryImpl.java:1234)
at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojo(PodamFactoryImpl.java:175)
... 1 more
Caused by: java.lang.IllegalArgumentException: Valore -247.795.888 non valido per il campo Day.
at com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.invalidFieldValue(XMLGregorianCalendarImpl.java:1294)
at com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.setDay(XMLGregorianCalendarImpl.java:1250)
... 9 more


How can i fix this? 

Thanks in advance. Best regards,
Alessandro.
To unsubscribe from this group and stop receiving emails from it, send an email to podam+unsubscribe@googlegroups.com.
Message has been deleted

Daniil Ivanov

unread,
Jul 2, 2015, 8:09:59 AM7/2/15
to po...@googlegroups.com
Answer to your question is two comments above from it.

Thanks, Daniil
Reply all
Reply to author
Forward
0 new messages