|  |  |  | License Codehaus OpenEJB OpenJMS OpenORB Tyrex

Old releases
General
Release 1.3
Release 1.3rc1
Release 1.2

Main
Home
About
Features
Download
Dependencies
Reference guide
Publications
JavaDoc
Maven 2 support
Maven 2 archetypes
DTD & Schemas
Recent HTML changes
News Archive
RSS news feed
Project Wiki

Development/Support
Mailing Lists
SVN/JIRA
Contributing
Support
Continuous builds
Prof. services

Related projects
Spring ORM support
Spring XML factories
WS frameworks

XML
XML

XML Code Generator
XML Code Generator

JDO
Introduction
First steps
Using JDO
JDO Config
Types
JDO Mapping
JDO FAQ
JDO Examples
JDO HOW-TOs
Tips & Tricks
Other Features
JDO sample JAR

Tools
Schema generator

Advanced JDO
Caching
OQL
Trans. & Locks
Design
KeyGen
Long Trans.
Nested Attrs.
Pooling Examples
LOBs
Best practice

DDL Generator
Using DDL Generator
Properties
Ant task
Type Mapping

More
The Examples
3rd Party Tools
JDO Tests
XML Tests
Configuration

About
License
User stories
Contributors
Marketplace
Status, Todo
Changelog
Library
Contact
Project Name

## Using the Ant task for the Castor XML Code Generator

Documentation Author(s):
Keith Visco
Arnaud Blandin

API Reference: The Source Builder API

Castor source generator Ant task
Specifying the source for generation
Parameters
Examples
Using a file
Using an URL
Using a nested <fileset>

## Castor source generator Ant task

An alternative to using the command line as shown in the previous section, the Castor Source Generator Ant Task can be used to call the source generator for class generation. The only requirement is that the castor-<version>-codegen-antask.jar must be on the CLASSPATH.

### Specifying the source for generation

As shown in the subsequent table, there's multiple ways of specifying the input for the Castor code generator. At least one input source has to be specified.

Attribute | Description | Required | Since
file | The XML schema, to be used as input for the source code generator. | No. | -
dir | Sets a directory such that all XML schemas in this directory will have code generated for them. | No | -
schemaURL | URL to an XML schema, to be used as input for the source code generator. | No. | 1.2

In addition, a nested <fileset> can be specified as the source of input. Please refer to the samples shown below.

### Parameters

Please find below the complete list of parameters that can be set on the Castor source generator to fine-tune the execution behavior.

Attribute | Description | Required | Since
package | The default package to be used during source code generation. | No; if not given, all classes will be placed in the root package. | -
todir | The destination directory to be used during source code generation. In this directory all generated Java classes will be placed. | No | -
bindingfile | A Castor source generator binding file, as detailed here. | No | -
lineseparator | Defines whether to use Unix- or Windows- or Mac-style line separators during source code generation. Possible values are: 'unix', 'win' or 'mac'. | No; if not set, system property 'line.separator' is used instead. | -
types | Defines what collection types to use (Java 1 vs. Java 2). Possible values: 'vector', 'arraylist' (aka 'j2') or 'odmg'. | No; if not set, the default collection used will be Java 1 type | -
verbose | Whether to output any logging messages as emitted by the source generator | No | -
warnings | Whether to suppress any warnings as otherwise emitted by the source generator | No | -
nodesc | If used, instructs the source generator not to generate *Descriptor classes. | No | -
generateMapping | If used, instructs the source generator to (additionally) generate a mapping file. | No | -
nomarshal | If specified, instructs the source generator not to create (un)marshalling methods within the Java classes generated. | No | -
caseInsensitive | If used, instructs the source generator to generate code for enumerated type lookup in a case insensitive manner. | No | -
sax1 | If used, instructs the source generator to generate SAX-1 compliant code. | No | -
generateImportedSchemas | If used, instructs the source generator to generate code for imported schemas as well. | No | -
nameConflictStrategy | If used, sets the name conflict strategy to use during XML code generation; possible values are '`warnViaConsoleDialog`' and '`informViaLog`'. | No | -
properties | Location of file defining a set of properties to be used during source code generation. This overrides the default mechanisms of configuring the source generator through a `castorbuilder.properties` (that has to be placed on the CLASSPATH) | No | -
automaticConflictStrategy | If used, sets the name conflict resolution strategy used during XML code generation; possible values are '`type`' and '`xpath`' (default being '`xpath`'). | No | -
jclassPrinterType | Sets the mode for printing JClass instances during XML code generation; possible values are '`standard`' and '`velocity`' (default being '`standard`'). | No | 1.2.1
generateJdoDescriptors | If used, instructs the source generator to generate JDO class descriptors as well; default is false. | No | 1.2.1

### Examples

#### Using a file

Below is an example of how to use this task from within an Ant target definition named 'castor:gen:src':

    <target name="castor:gen:src" depends="init"
             description="Generate Java source files from XSD.">

        <taskdef name="castor-srcgen"
                 classname="org.castor.anttask.CastorCodeGenTask"
                 classpathref="castor.class.path" />
        <mkdir dir="generated" />
        <castor-srcgen file="src/schema/sample.xsd"
                       todir="generated-source"
                       package="org.castor.example.schema"
                       types="j2"
                       warnings="true" />
    </target>

#### Using an URL

Below is the same sample as above, this time using the url attribute as the source of input instead:

    <target name="castor:gen:src" depends="init"
             description="Generate Java source files from XSD.">

        <taskdef name="castor-srcgen"
                 classname="org.castor.anttask.CastorCodeGenTask"
                 classpathref="castor.class.path" />
        <mkdir dir="generated" />
        <castor-srcgen schemaURL="http://some.domain/some/path/sample.xsd"
                       todir="generated-source"
                       package="org.castor.example.schema"
                       types="j2"
                       warnings="true" />
    </target>

#### Using a nested <fileset>

Below is the same sample as above, this time using the url attribute as the source of input instead:

    <target name="castor:gen:src" depends="init"
             description="Generate Java source files from XSD.">

        <taskdef name="castor-srcgen"
                 classname="org.castor.anttask.CastorCodeGenTask"
                 classpathref="castor.class.path" />
        <mkdir dir="generated" />
        <castor-srcgen todir="generated-source"
                       package="org.castor.example.schema"
                       types="j2"
                       warnings="true" >
           <fileset dir="${basedir}/src/schema">
              <include name="**/*.xsd"/>
           </fileset>
        </castor-srcgen>
    </target>

<notice> Copyright © 1999-2005 ExoLab Group, Intalio Inc., and Contributors. All rights reserved. </notice>
<notice> Java, EJB, JDBC, JNDI, JTA, Sun, Sun Microsystems are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and in other countries. XML, XML Schema, XSLT and related standards are trademarks or registered trademarks of MIT, INRIA, Keio or others, and a product of the World Wide Web Consortium. All other product names mentioned herein are trademarks of their respective owners. </notice>