[[Castor-Castor]]
Castor
~~~~~~

*Available as of Camel 2.1*

Castor is a link:data-format.html[Data Format] which uses the
http://www.castor.org/[Castor XML library] to unmarshal an XML payload
into Java objects or to marshal Java objects into an XML payload.

As usually you can use either Java DSL or Spring XML to work with Castor
Data Format.

[[Castor-UsingtheJavaDSL]]
Using the Java DSL
^^^^^^^^^^^^^^^^^^

[source,java]
-----------------------------
from("direct:order").
  marshal().castor().
  to("activemq:queue:order");
-----------------------------

For example the following uses a named DataFormat of Castor which uses
default Castor data binding features.

[source,java]
--------------------------------------------------
CastorDataFormat castor = new CastorDataFormat ();

from("activemq:My.Queue").
  unmarshal(castor).
  to("mqseries:Another.Queue");
--------------------------------------------------

If you prefer to use a named reference to a data format which can then
be defined in your Registry such as via your Spring XML file. e.g.

[source,java]
-------------------------------
from("activemq:My.Queue").
  unmarshal("mycastorType").
  to("mqseries:Another.Queue");
-------------------------------

If you want to override default mapping schema by providing a mapping
file you can set it as follows.

[source,java]
--------------------------------------------------
CastorDataFormat castor = new CastorDataFormat ();
castor.setMappingFile("mapping.xml");
--------------------------------------------------

Also if you want to have more control on Castor Marshaller and
Unmarshaller you can access them as below.

[source,java]
-------------------------
castor.getMarshaller();
castor.getUnmarshaller();
-------------------------

[[Castor-UsingSpringXML]]
Using Spring XML
^^^^^^^^^^^^^^^^

The following example shows how to use Castor to unmarshal using Spring
configuring the castor data type

[source,java]
-----------------------------------------------------------------------
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <unmarshal>
      <castor validation="true" />
    </unmarshal>
    <to uri="mock:result"/>
  </route>
</camelContext>
-----------------------------------------------------------------------

This example shows how to configure the data type just once and reuse it
on multiple routes. You have to set the <castor> element directly in
<camelContext>.

[source,java]
-----------------------------------------------------------------------
<camelContext>
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <dataFormats>
    <castor id="myCastor"/>
  </dataFormats>

  <route>
    <from uri="direct:start"/>
    <marshal ref="myCastor"/>
    <to uri="direct:marshalled"/>
  </route>
  <route>
    <from uri="direct:marshalled"/>
    <unmarshal ref="myCastor"/>
    <to uri="mock:result"/>
  </route>

</camelContext>
-----------------------------------------------------------------------

[[Castor-Options]]
Options
^^^^^^^

Castor supports the following options

[width="100%",cols="10%,10%,10%,70%",options="header",]
|=======================================================================
|Option |Type |Default |Description

|encoding |String |UTF-8 |Encoding to use when marshalling an Object to XML

|validation |Boolean |false |Whether validation is turned on or off.

|mappingFile |String |null |Path to a Castor mapping file to load from the classpath.

|packages |String[] |null |Add additional packages to Castor XmlContext

|classNames |String[] |null |Add additional class names to Castor XmlContext
|=======================================================================

[[Castor-Dependencies]]
Dependencies
^^^^^^^^^^^^

To use Castor in your camel routes you need to add the a dependency on
*camel-castor* which implements this data format.

If you use maven you could just add the following to your pom.xml,
substituting the version number for the latest & greatest release (see
link:download.html[the download page for the latest versions]).

[source,java]
---------------------------------------
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-castor</artifactId>
  <version>x.x.x</version>
</dependency>
---------------------------------------
