2008/12/11 James Strachan <[EMAIL PROTECTED]>:
> 2008/12/11 cmoulliard <[EMAIL PROTECTED]>:
>>
>> Hi,
>>
>> I would like to know How we can extend the existing method
>> marshall/unmarshall of the class CsvDataFormat (supercsv component) in such
>> a way that we can provide the CsvPreferences (STANDARD, EXCEL, ...) and the
>> bean to be used to map the data ?
>>
>> idea :
>>
>> from("file://myCSVfile")
>> .marshal().csv("CsvPrefs","UserBeanToBeUsedForMappingData")
>> .to("");
>>
>> Why such request ? Because the SuperCSV classes allow to map directly the
>> content of a CSV file (with or withour headers) to a POJO and we can define
>> i nthe preference the kind of CSV provided (excel, ...) (see doc for more
>> info : http://supercsv.sourceforge.net/codeExamples_general.html) ?
>>
>> ex :
>>
>> public class UserBean {
>> String username, password, street, town;
>> int zip;
>>
>> public String getPassword() { return password; }
>> public String getStreet() { return street; }
>> public String getTown() { return town; }
>> public String getUsername() { return username; }
>> public int getZip() { return zip; }
>> public void setPassword(String password) { this.password = password; }
>> public void setStreet(String street) { this.street = street; }
>> public void setTown(String town) { this.town = town; }
>> public void setUsername(String username) { this.username = username; }
>> public void setZip(int zip) { this.zip = zip; }
>> }
>>
>> class ReadingObjects {
>> public static void main(String[] args) throws Exception{
>> ICsvBeanReader inFile = new CsvBeanReader(new FileReader("foo.csv"),
>> CsvPreference.EXCEL_PREFERENCE);
>> try {
>> final String[] header = inFile.getCSVHeader(true);
>> UserBean user;
>> while( (user = inFile.read(UserBean.class, header, processors)) !=
>> null) {
>> System.out.println(user.getZip());
>> }
>> } finally {
>> inFile.close();
>> }
>> }
>> }
>
> One of the big reasons for the DataFormat abstraction in Camel instead
> of the type conversion stuff was so that you could create a specific
> DataFormat instance - configure it in Java/Spring/Guice and use it
> explicitly in a route.
>
> e.g. see JAXB which is a great example - you typically wanna configure
> each one with a JAXB context (set of packages/classes etc)
> http://activemq.apache.org/camel/jaxb.html
>
> so sure, we should make it easy to configure all of the Data Format
> implementations - either through a DI framework like Spring/Guice or
> via Java/JNDI
I guess the wiki page just needs updating to describe it...
CsvDataFormat format = new CsvDataFormat();
format.setConfig(...);
format.setStrategy(...);
...
from("file://myCSVfile")
.unmarshal(format)
.to("");
--
James
-------
http://macstrac.blogspot.com/
Open Source Integration
http://fusesource.com/