You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by cmoulliard <cm...@gmail.com> on 2008/10/21 15:09:34 UTC

Xstream marshalling & Alias

Hi,

Is it possible during the Xstream marshalling to define alias attribute as
this is possible in the Xstream package
(http://xstream.codehaus.org/alias-tutorial.html) ? 

If the answer is yes, where can I find an example ?

KR,

Charles Moulliard

-----
Enterprise Architect

Xpectis
12, route d'Esch
L-1470 Luxembourg

Phone +352 25 10 70 470
Mobile +352 621 45 36 22

e-mail : cmoulliard@xpectis.com
web site :  www.xpectis.com www.xpectis.com 
My Blog :  http://cmoulliard.blogspot.com/ http://cmoulliard.blogspot.com/  
-- 
View this message in context: http://www.nabble.com/Xstream-marshalling---Alias-tp20089956s22882p20089956.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Xstream marshalling & Alias

Posted by James Strachan <ja...@gmail.com>.
I've just added a couple of factory methods so you can easily create
an XStreamDataFormat using a String factory method or via Java code...

XStreamDataFormat format =
XStreamDataFormat.processAnnotations(A.class, B.class);


2008/10/23 James Strachan <ja...@gmail.com>:
> That sounds great.
>
> I wonder BTW if we should just provide a constructor to the
> XStreamDataFormat class which allows a list of classes to be
> specified?
>
> Then in spring XML you can do the equivalent of invoking the constructor...
>
> XStreamDataFormat(Class... classesWithAnnotations) {
> ...
> }
>
> 2008/10/23 cmoulliard <cm...@gmail.com>:
>>
>> James,
>>
>> In the meantime, I have implemented an interesting workaround to allow what
>> I describe in my previous message.
>>
>> Here is a explanation + code of what I have done. Maybe, this could interest
>> other users.
>>
>> 1) Formatter Class
>>
>> I have created a factory Formatter class who will generate a
>> XStreamDataFormat object through a createInstance method. This method is
>> called by Spring through the factory-method property and receives as
>> parameter the name of the class to be formatted by Xstream.
>>
>> import org.apache.camel.dataformat.xstream.XStreamDataFormat;
>>
>> import com.thoughtworks.xstream.XStream;
>>
>> public class Formatter {
>>
>>        private static XStreamDataFormat myDataformat;
>>
>>        private Formatter() {}
>>
>>        @SuppressWarnings("unchecked")
>>        public static XStreamDataFormat createInstance(Class classToBeFormated) {
>>
>>        XStream xstream = new XStream();
>>        xstream.processAnnotations(classToBeFormated);
>>        myDataformat = new XStreamDataFormat();
>>        myDataformat.setXStream(xstream);
>>
>>                return myDataformat;
>>
>>        }
>>
>> }
>>
>> 2) Spring and Camel configuration
>>
>> To allow Camel to use the XStreamDataFormat object formatted with my
>> StockPrice class, I create a bean reference in the spring-camel.xml file
>> where the parameter provided as argument is the name of the class to be
>> formatted and the method to be called in my factory is createInstance.
>>
>> <bean id="myXstreamDataFormat" class="com.mycompany.model.Formatter"
>>                factory-method="createInstance">
>>                <constructor-arg value="com.mycompany.model.StockPrice" />
>> </bean>
>>
>> Here is a snaphot of the Camel route who uses the myXStreamDataFormat bean
>> reference
>>
>> <route>
>>        <from
>> uri="timer:generateStockPrice?fixedRate=true&amp;delay=0&amp;period=6000" />
>>                <to uri="bean:stockPriceGenerator" />
>>                <marshal ref="myXstreamDataFormat" />
>>
>> Remark : the class to be formatted uses Xstream annotation
>>
>> Here is the code :
>>
>> import com.thoughtworks.xstream.annotations.XStreamAlias;
>> import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
>>
>> @XStreamAlias("price")
>> public class StockPrice {
>>        @XStreamAsAttribute
>>    private String stock;
>>
>>        @XStreamAsAttribute
>>        private double price;
>>
>>        @XStreamAsAttribute
>>        private String movement;
>>
>>        @XStreamAsAttribute
>>        private double offer;
>>
>>    public double getOffer() {
>>                return offer;
>>        }
>>
>>        public void setOffer(double offer) {
>>                this.offer = offer;
>>        }
>>
>>    public String getMovement() {
>>                return movement;
>>        }
>>
>>        public void setMovement(String movement) {
>>                this.movement = movement;
>>        }
>>
>>    public String getStock() {
>>        return stock;
>>    }
>>
>>    public void setStock(String stock) {
>>        this.stock = stock;
>>    }
>>
>>    public double getPrice() {
>>        return price;
>>    }
>>
>>    public void setPrice(double price) {
>>        this.price = price;
>>    }
>>
>>    @Override
>>    public String toString() {
>>        return "StockPrice[stock: " + stock + " bid: " + price + " offer: "
>> + offer + " movement: " + movement + "]";
>>    }
>> }
>>
>> In consequence, using spring in combination with XStream Annotation, I'm
>> able to marshall/unsmarshall my objects no matter which model is required
>> (StockPrice.class, ....) and mapping strategy required between the XML
>> and/or java attributes.
>>
>> KR,
>>
>> Charles
>>
>> James.Strachan wrote:
>>>
>>> 2008/10/22 cmoulliard <cm...@gmail.com>:
>>>>
>>>> Many thanks.
>>>>
>>>> In this case, it will be required that I create my own format
>>>> (XSTreamDataFormat) like this o use annotation :
>>>>
>>>>                XStream xstream = new XStream();
>>>>                xstream.processAnnotations(StockPrice.class);
>>>>                XStreamDataFormat myformat = new XStreamDataFormat();
>>>>                myformat.setXStream(xstream);
>>>>
>>>>                from("direct:in").marshal(myformat).to("mock:result");
>>>
>>>
>>> Do you have to tell XStream which classes to look for annotations? I'd
>>> have thought, like JAXB, it'd just look for the annotations by default
>>> on whatever classes it was marshalling. But I guess for unmarshalling
>>> it needs to know (rather like the JAXBContext needs to know the
>>> classes/packages)
>>>
>>>> Remark :
>>>>
>>>> It should be interesting to use the xstream dataformat with a parameter
>>>> indicating that XStream must process annotation
>>>> from("").marshal().xstream().processAnnotation().to("") to avoid to
>>>> create
>>>> its own DataFormat ?
>>>
>>> I think like JAXB, we'd just need to create the XStreamDataFormat
>>> explicitly using whatever configuration is required
>>>
>>> --
>>> James
>>> -------
>>> http://macstrac.blogspot.com/
>>>
>>> Open Source Integration
>>> http://fusesource.com/
>>>
>>>
>>
>>
>> -----
>> Enterprise Architect
>>
>> Xpectis
>> 12, route d'Esch
>> L-1470 Luxembourg
>>
>> Phone +352 25 10 70 470
>> Mobile +352 621 45 36 22
>>
>> e-mail : cmoulliard@xpectis.com
>> web site :  www.xpectis.com www.xpectis.com
>> My Blog :  http://cmoulliard.blogspot.com/ http://cmoulliard.blogspot.com/
>> --
>> View this message in context: http://www.nabble.com/Xstream-marshalling---Alias-tp20089956s22882p20130279.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>>
>
>
>
> --
> James
> -------
> http://macstrac.blogspot.com/
>
> Open Source Integration
> http://fusesource.com/
>



-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Re: Xstream marshalling & Alias

Posted by James Strachan <ja...@gmail.com>.
That sounds great.

I wonder BTW if we should just provide a constructor to the
XStreamDataFormat class which allows a list of classes to be
specified?

Then in spring XML you can do the equivalent of invoking the constructor...

XStreamDataFormat(Class... classesWithAnnotations) {
...
}

2008/10/23 cmoulliard <cm...@gmail.com>:
>
> James,
>
> In the meantime, I have implemented an interesting workaround to allow what
> I describe in my previous message.
>
> Here is a explanation + code of what I have done. Maybe, this could interest
> other users.
>
> 1) Formatter Class
>
> I have created a factory Formatter class who will generate a
> XStreamDataFormat object through a createInstance method. This method is
> called by Spring through the factory-method property and receives as
> parameter the name of the class to be formatted by Xstream.
>
> import org.apache.camel.dataformat.xstream.XStreamDataFormat;
>
> import com.thoughtworks.xstream.XStream;
>
> public class Formatter {
>
>        private static XStreamDataFormat myDataformat;
>
>        private Formatter() {}
>
>        @SuppressWarnings("unchecked")
>        public static XStreamDataFormat createInstance(Class classToBeFormated) {
>
>        XStream xstream = new XStream();
>        xstream.processAnnotations(classToBeFormated);
>        myDataformat = new XStreamDataFormat();
>        myDataformat.setXStream(xstream);
>
>                return myDataformat;
>
>        }
>
> }
>
> 2) Spring and Camel configuration
>
> To allow Camel to use the XStreamDataFormat object formatted with my
> StockPrice class, I create a bean reference in the spring-camel.xml file
> where the parameter provided as argument is the name of the class to be
> formatted and the method to be called in my factory is createInstance.
>
> <bean id="myXstreamDataFormat" class="com.mycompany.model.Formatter"
>                factory-method="createInstance">
>                <constructor-arg value="com.mycompany.model.StockPrice" />
> </bean>
>
> Here is a snaphot of the Camel route who uses the myXStreamDataFormat bean
> reference
>
> <route>
>        <from
> uri="timer:generateStockPrice?fixedRate=true&amp;delay=0&amp;period=6000" />
>                <to uri="bean:stockPriceGenerator" />
>                <marshal ref="myXstreamDataFormat" />
>
> Remark : the class to be formatted uses Xstream annotation
>
> Here is the code :
>
> import com.thoughtworks.xstream.annotations.XStreamAlias;
> import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
>
> @XStreamAlias("price")
> public class StockPrice {
>        @XStreamAsAttribute
>    private String stock;
>
>        @XStreamAsAttribute
>        private double price;
>
>        @XStreamAsAttribute
>        private String movement;
>
>        @XStreamAsAttribute
>        private double offer;
>
>    public double getOffer() {
>                return offer;
>        }
>
>        public void setOffer(double offer) {
>                this.offer = offer;
>        }
>
>    public String getMovement() {
>                return movement;
>        }
>
>        public void setMovement(String movement) {
>                this.movement = movement;
>        }
>
>    public String getStock() {
>        return stock;
>    }
>
>    public void setStock(String stock) {
>        this.stock = stock;
>    }
>
>    public double getPrice() {
>        return price;
>    }
>
>    public void setPrice(double price) {
>        this.price = price;
>    }
>
>    @Override
>    public String toString() {
>        return "StockPrice[stock: " + stock + " bid: " + price + " offer: "
> + offer + " movement: " + movement + "]";
>    }
> }
>
> In consequence, using spring in combination with XStream Annotation, I'm
> able to marshall/unsmarshall my objects no matter which model is required
> (StockPrice.class, ....) and mapping strategy required between the XML
> and/or java attributes.
>
> KR,
>
> Charles
>
> James.Strachan wrote:
>>
>> 2008/10/22 cmoulliard <cm...@gmail.com>:
>>>
>>> Many thanks.
>>>
>>> In this case, it will be required that I create my own format
>>> (XSTreamDataFormat) like this o use annotation :
>>>
>>>                XStream xstream = new XStream();
>>>                xstream.processAnnotations(StockPrice.class);
>>>                XStreamDataFormat myformat = new XStreamDataFormat();
>>>                myformat.setXStream(xstream);
>>>
>>>                from("direct:in").marshal(myformat).to("mock:result");
>>
>>
>> Do you have to tell XStream which classes to look for annotations? I'd
>> have thought, like JAXB, it'd just look for the annotations by default
>> on whatever classes it was marshalling. But I guess for unmarshalling
>> it needs to know (rather like the JAXBContext needs to know the
>> classes/packages)
>>
>>> Remark :
>>>
>>> It should be interesting to use the xstream dataformat with a parameter
>>> indicating that XStream must process annotation
>>> from("").marshal().xstream().processAnnotation().to("") to avoid to
>>> create
>>> its own DataFormat ?
>>
>> I think like JAXB, we'd just need to create the XStreamDataFormat
>> explicitly using whatever configuration is required
>>
>> --
>> James
>> -------
>> http://macstrac.blogspot.com/
>>
>> Open Source Integration
>> http://fusesource.com/
>>
>>
>
>
> -----
> Enterprise Architect
>
> Xpectis
> 12, route d'Esch
> L-1470 Luxembourg
>
> Phone +352 25 10 70 470
> Mobile +352 621 45 36 22
>
> e-mail : cmoulliard@xpectis.com
> web site :  www.xpectis.com www.xpectis.com
> My Blog :  http://cmoulliard.blogspot.com/ http://cmoulliard.blogspot.com/
> --
> View this message in context: http://www.nabble.com/Xstream-marshalling---Alias-tp20089956s22882p20130279.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>



-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Re: Xstream marshalling & Alias

Posted by cmoulliard <cm...@gmail.com>.
James, 

In the meantime, I have implemented an interesting workaround to allow what
I describe in my previous message.

Here is a explanation + code of what I have done. Maybe, this could interest
other users.

1) Formatter Class

I have created a factory Formatter class who will generate a
XStreamDataFormat object through a createInstance method. This method is
called by Spring through the factory-method property and receives as
parameter the name of the class to be formatted by Xstream.

import org.apache.camel.dataformat.xstream.XStreamDataFormat;

import com.thoughtworks.xstream.XStream;

public class Formatter {
	
	private static XStreamDataFormat myDataformat;
	
	private Formatter() {}
	
	@SuppressWarnings("unchecked")
	public static XStreamDataFormat createInstance(Class classToBeFormated) {
		
        XStream xstream = new XStream();
        xstream.processAnnotations(classToBeFormated);
        myDataformat = new XStreamDataFormat();
        myDataformat.setXStream(xstream);
		
		return myDataformat;
		
	}

}

2) Spring and Camel configuration

To allow Camel to use the XStreamDataFormat object formatted with my
StockPrice class, I create a bean reference in the spring-camel.xml file
where the parameter provided as argument is the name of the class to be
formatted and the method to be called in my factory is createInstance. 

<bean id="myXstreamDataFormat" class="com.mycompany.model.Formatter"
		factory-method="createInstance">
		<constructor-arg value="com.mycompany.model.StockPrice" />
</bean>

Here is a snaphot of the Camel route who uses the myXStreamDataFormat bean
reference

<route>
	<from
uri="timer:generateStockPrice?fixedRate=true&amp;delay=0&amp;period=6000" />
		<to uri="bean:stockPriceGenerator" />
		<marshal ref="myXstreamDataFormat" />

Remark : the class to be formatted uses Xstream annotation

Here is the code :

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("price")
public class StockPrice {
	@XStreamAsAttribute
    private String stock;

	@XStreamAsAttribute
	private double price;

	@XStreamAsAttribute
	private String movement;
	
	@XStreamAsAttribute
	private double offer;

    public double getOffer() {
		return offer;
	}

	public void setOffer(double offer) {
		this.offer = offer;
	}

    public String getMovement() {
		return movement;
	}

	public void setMovement(String movement) {
		this.movement = movement;
	}
    
    public String getStock() {
        return stock;
    }

    public void setStock(String stock) {
        this.stock = stock;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    
    @Override
    public String toString() {
        return "StockPrice[stock: " + stock + " bid: " + price + " offer: "
+ offer + " movement: " + movement + "]";
    }
}

In consequence, using spring in combination with XStream Annotation, I'm
able to marshall/unsmarshall my objects no matter which model is required
(StockPrice.class, ....) and mapping strategy required between the XML
and/or java attributes.

KR,

Charles

James.Strachan wrote:
> 
> 2008/10/22 cmoulliard <cm...@gmail.com>:
>>
>> Many thanks.
>>
>> In this case, it will be required that I create my own format
>> (XSTreamDataFormat) like this o use annotation :
>>
>>                XStream xstream = new XStream();
>>                xstream.processAnnotations(StockPrice.class);
>>                XStreamDataFormat myformat = new XStreamDataFormat();
>>                myformat.setXStream(xstream);
>>
>>                from("direct:in").marshal(myformat).to("mock:result");
> 
> 
> Do you have to tell XStream which classes to look for annotations? I'd
> have thought, like JAXB, it'd just look for the annotations by default
> on whatever classes it was marshalling. But I guess for unmarshalling
> it needs to know (rather like the JAXBContext needs to know the
> classes/packages)
> 
>> Remark :
>>
>> It should be interesting to use the xstream dataformat with a parameter
>> indicating that XStream must process annotation
>> from("").marshal().xstream().processAnnotation().to("") to avoid to
>> create
>> its own DataFormat ?
> 
> I think like JAXB, we'd just need to create the XStreamDataFormat
> explicitly using whatever configuration is required
> 
> -- 
> James
> -------
> http://macstrac.blogspot.com/
> 
> Open Source Integration
> http://fusesource.com/
> 
> 


-----
Enterprise Architect

Xpectis
12, route d'Esch
L-1470 Luxembourg

Phone +352 25 10 70 470
Mobile +352 621 45 36 22

e-mail : cmoulliard@xpectis.com
web site :  www.xpectis.com www.xpectis.com 
My Blog :  http://cmoulliard.blogspot.com/ http://cmoulliard.blogspot.com/  
-- 
View this message in context: http://www.nabble.com/Xstream-marshalling---Alias-tp20089956s22882p20130279.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Xstream marshalling & Alias

Posted by James Strachan <ja...@gmail.com>.
2008/10/22 cmoulliard <cm...@gmail.com>:
>
> Many thanks.
>
> In this case, it will be required that I create my own format
> (XSTreamDataFormat) like this o use annotation :
>
>                XStream xstream = new XStream();
>                xstream.processAnnotations(StockPrice.class);
>                XStreamDataFormat myformat = new XStreamDataFormat();
>                myformat.setXStream(xstream);
>
>                from("direct:in").marshal(myformat).to("mock:result");


Do you have to tell XStream which classes to look for annotations? I'd
have thought, like JAXB, it'd just look for the annotations by default
on whatever classes it was marshalling. But I guess for unmarshalling
it needs to know (rather like the JAXBContext needs to know the
classes/packages)

> Remark :
>
> It should be interesting to use the xstream dataformat with a parameter
> indicating that XStream must process annotation
> from("").marshal().xstream().processAnnotation().to("") to avoid to create
> its own DataFormat ?

I think like JAXB, we'd just need to create the XStreamDataFormat
explicitly using whatever configuration is required

-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Re: Xstream marshalling & Alias

Posted by cmoulliard <cm...@gmail.com>.
Many thanks.

In this case, it will be required that I create my own format
(XSTreamDataFormat) like this o use annotation :

            	XStream xstream = new XStream();
            	xstream.processAnnotations(StockPrice.class);
            	XStreamDataFormat myformat = new XStreamDataFormat();
            	myformat.setXStream(xstream);
            	
            	from("direct:in").marshal(myformat).to("mock:result");

Remark : 

It should be interesting to use the xstream dataformat with a parameter
indicating that XStream must process annotation
from("").marshal().xstream().processAnnotation().to("") to avoid to create
its own DataFormat ?

KR,

Charles


James.Strachan wrote:
> 
> Or use XStream annotations on your types...
> http://xstream.codehaus.org/annotations-tutorial.html
> 
> 2008/10/21 James Strachan <ja...@gmail.com>:
>> 2008/10/21 cmoulliard <cm...@gmail.com>:
>>>
>>> Hi,
>>>
>>> Is it possible during the Xstream marshalling to define alias attribute
>>> as
>>> this is possible in the Xstream package
>>> (http://xstream.codehaus.org/alias-tutorial.html) ?
>>>
>>> If the answer is yes, where can I find an example ?
>>
>> There's no example, but you can just instantiate the XStreamDataFormat
>> class and configure its XStream object to be whatever you like.
>>
>> XStreamDataFormat myformat = new XStreamDataFormat();
>> myformat.setXStream(whatever);
>>
>>
>> from("...").marshal(myformat);
>>
>>
>>
>> --
>> James
>> -------
>> http://macstrac.blogspot.com/
>>
>> Open Source Integration
>> http://fusesource.com/
>>
> 
> 
> 
> -- 
> James
> -------
> http://macstrac.blogspot.com/
> 
> Open Source Integration
> http://fusesource.com/
> 
> 


-----
Enterprise Architect

Xpectis
12, route d'Esch
L-1470 Luxembourg

Phone +352 25 10 70 470
Mobile +352 621 45 36 22

e-mail : cmoulliard@xpectis.com
web site :  www.xpectis.com www.xpectis.com 
My Blog :  http://cmoulliard.blogspot.com/ http://cmoulliard.blogspot.com/  
-- 
View this message in context: http://www.nabble.com/Xstream-marshalling---Alias-tp20089956s22882p20108946.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Xstream marshalling & Alias

Posted by James Strachan <ja...@gmail.com>.
Or use XStream annotations on your types...
http://xstream.codehaus.org/annotations-tutorial.html

2008/10/21 James Strachan <ja...@gmail.com>:
> 2008/10/21 cmoulliard <cm...@gmail.com>:
>>
>> Hi,
>>
>> Is it possible during the Xstream marshalling to define alias attribute as
>> this is possible in the Xstream package
>> (http://xstream.codehaus.org/alias-tutorial.html) ?
>>
>> If the answer is yes, where can I find an example ?
>
> There's no example, but you can just instantiate the XStreamDataFormat
> class and configure its XStream object to be whatever you like.
>
> XStreamDataFormat myformat = new XStreamDataFormat();
> myformat.setXStream(whatever);
>
>
> from("...").marshal(myformat);
>
>
>
> --
> James
> -------
> http://macstrac.blogspot.com/
>
> Open Source Integration
> http://fusesource.com/
>



-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Re: Xstream marshalling & Alias

Posted by James Strachan <ja...@gmail.com>.
2008/10/21 cmoulliard <cm...@gmail.com>:
>
> Hi,
>
> Is it possible during the Xstream marshalling to define alias attribute as
> this is possible in the Xstream package
> (http://xstream.codehaus.org/alias-tutorial.html) ?
>
> If the answer is yes, where can I find an example ?

There's no example, but you can just instantiate the XStreamDataFormat
class and configure its XStream object to be whatever you like.

XStreamDataFormat myformat = new XStreamDataFormat();
myformat.setXStream(whatever);


from("...").marshal(myformat);



-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/