• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Castor and java.util.Calendar

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
I am using Castor to unmarshall a Java Bean class to an XML file.
The only fields that are not going to the XML file are the Calendar fields:
private Calendar effectiveDate;
public Calendar getEffectiveDate() {
return effectiveDate;
}
public void setEffectiveDate(Calendar aCal ) {
effectiveDate = aCal;
}
These fields are just being ignored.
When I generate the a mapping.xml file it also ignores these fields.
Any suggestions would be appreciated.
Rick Bos
Rick_Bos@cooperators.ca
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe java.util.Calendar is not among the "built-in" data types. Can you use java.util.Date instead? If not, you could try adding a mapping for java.util.Calendar into your mapping.xml file.
 
Rick Bos
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
Could you point me to a document that would explain how to
add java.util.Calendar to the mapping file ?
Ideally I would just like to map it once, as opposed to for each
field.
Thanks again.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rick Bos:
Could you point me to a document that would explain how to
add java.util.Calendar to the mapping file ?


The official documentation is all I know of. I'm not a Castor user myself, but I guess you'll have to take a look at the source code of the Calendar class to see all the private fields you have to include into the mapping file. (again, if it's possible to use java.util.Date instead...)
 
Rick Bos
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still am not able to get my Calendar fields into my xml file.
Any suggestions would be appreciated.
Here is what I tried:
GeneralInfoRequest.java
private Calendar reviewDate_;
public Calendar getReviewDate() { return reviewDate_ ; }
public void setReviewDate( Calendar cal ) { reviewDate_ = cal; }
CalendarFieldHandler.java:
public class CalendarFieldHandler extends GeneralizedFieldHandler {
public final DateFormat format = new SimpleDateFormat("yyyy-MM-dd");


public Object convertUponGet(Object value) {
if ( value instanceof Calendar ) {
Calendar cal = (Calendar) value;

return format.format(cal.getTime());
} else {
return value;
}

}

public Object convertUponSet(Object value) {
String dateStr = (String) value;
Object result = null;
try {
Date date = (Date) format.parse(dateStr);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
result = cal;

} catch (ParseException e) {

}
return result;
}
public Class getFieldType() {
return Calendar.class;
}
}
mapping.xml:
<?xml version="1.0" encoding="UTF-8"?>
<mapping xmlns="http://castor.exolab.org/" xmlns:cst="http://castor.exolab.org/">
<description>Castor generated mapping file</description>
<class name="services.types.GeneralInfoRequest" >
<field name="reviewDate" handler="ca.cooperators.tests.xml.mapping.CalendarFieldHandler"
type="java.util.Date" >
</field>
</class>
</mapping>
[ March 02, 2004: Message edited by: Rick Bos ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your last code is almost working (I know it's old)
you probably figured out since 2004 !

if you map with a string instead of a java.util.date
it works flawlessly

<field name="reviewDate" handler="ca.cooperators.tests.xml.mapping.CalendarFieldHandler"
type="string" >
</field>

just for other people coming to this thread
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic