View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id: XSDCompiler.java 6543 2006-12-18 23:07:56Z wguttmn $
44   */
45  package org.exolab.castor.tools;
46  
47  import java.io.File;
48  import java.io.IOException;
49  
50  import org.apache.tools.ant.BuildException;
51  import org.apache.tools.ant.Project;
52  import org.apache.tools.ant.Task;
53  import org.exolab.castor.builder.SourceGenerator;
54  import org.exolab.castor.builder.factory.FieldInfoFactory;
55  
56  /**
57   * Ant task that enables code generation from an XML _schema from within Ant.
58   *
59   * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
60   * @version $Revision: 6543 $ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
61   * @deprecated Please use {@link org.castor.anttask.CastorCodeGenTask} instead.
62   */
63  public final class XSDCompiler extends Task {
64      //--------------------------------------------------------------------------
65  
66      /** Schema to use to generate code. */
67      private File _schema;
68      
69      /** Package to generate code into. */
70      private String _pkgName;
71      
72      /** Line seperator to use. */
73      private String _lineSep;
74      
75      /** If true, suppress non-fatal warnings. */
76      private boolean _force;
77      
78      /** Custom type factory to supply to the code generator. */
79      private String _typeFactory;
80      
81      /** Directory into which to generate code. */
82      private File _destDir;
83  
84      //--------------------------------------------------------------------------
85  
86      /**
87       * Creates a new XSDCompiler Task.
88       */
89      public XSDCompiler() {
90          // No action needed
91      }
92  
93      //--------------------------------------------------------------------------
94  
95      /**
96       * Executes the task. If anything goes wrong during execution of the Ant task a
97       * BuildException will be thrown.
98       */
99      public void execute() {
100         if (_schema == null || !_schema.exists()) {
101             throw new BuildException("Schema file is required");
102         }
103 
104         if (_lineSep != null) {
105             if ("win".equals(_lineSep) || "\r\n".equals(_lineSep)) {
106                 project.log("Using Windows style line separation.", Project.MSG_VERBOSE);
107                 _lineSep = "\r\n";
108             } else if ("unix".equals(_lineSep) || "\n".equals(_lineSep)) {
109                 project.log("Using UNIX style line separation.", Project.MSG_VERBOSE);
110                 _lineSep = "\n";
111             } else if ("mac".equals(_lineSep) || "\r".equals(_lineSep)) {
112                 project.log("Using Macintosh style line separation.", Project.MSG_VERBOSE);
113                 _lineSep = "\r";
114             } else {
115                 throw new BuildException("Invalid line-separator style.");
116             }
117         } else {
118             _lineSep = "\n"; // default
119         }
120 
121         SourceGenerator sgen = null;
122         if (_typeFactory != null) {
123             try {
124                 Object factory = Class.forName(_typeFactory).newInstance();
125                 sgen = new SourceGenerator((FieldInfoFactory) factory);
126             } catch (Exception ex) {
127                 project.log("Type factory " + _typeFactory + " is invalid.", Project.MSG_INFO);
128                 throw new BuildException(ex);
129             }
130         } else {
131             // default
132             sgen = new SourceGenerator();
133         }
134 
135         sgen.setLineSeparator(_lineSep);
136         sgen.setSuppressNonFatalWarnings(_force);
137         sgen.setDestDir(_destDir.toString());
138         if (_force) { project.log("Suppressing non fatal warnings.", Project.MSG_VERBOSE); }
139 
140         try {
141             sgen.generateSource(_schema.getAbsolutePath(), _pkgName);
142         } catch (IOException ex) {
143             project.log("Failed to compile " + _schema, Project.MSG_INFO);
144             throw new BuildException(ex);
145         }
146     }
147 
148     //--------------------------------------------------------------------------
149 
150     /**
151      * Set the schema file name.
152      * 
153      * @param schema The schema to be used for code generation.
154      */
155     public void setSchema(final String schema) {
156         _schema = project.resolveFile(schema);
157     }
158 
159     /**
160      * Set the target package name.
161      * 
162      * @param pkgName The target package name.
163      */
164     public void setPackage(final String pkgName) {
165         _pkgName = pkgName;
166     }
167 
168     /**
169      * Set the line separator.
170      * 
171      * @param lineSep The line seperator to use for this platform.
172      */
173     public void setLineseperator(final String lineSep) {
174         _lineSep = lineSep;
175     }
176 
177     /**
178      * Set overwriting existing files.
179      * 
180      * @param force If true, existing files will be silently overwritten and non-fatal
181      *        warnings will be ignored
182      */
183     public void setForce(final boolean force) {
184         _force = force;
185     }
186 
187     /**
188      * Set the type factory.
189      * 
190      * @param typeFactory Name of the custom type factory class for collections.
191      */
192     public void setTypefactory(final String typeFactory) {
193         _typeFactory = typeFactory;
194     }
195 
196     /**
197      * Set the destination directory into which the Java sources should be copied to.
198      * 
199      * @param dirName The name of the destination directory
200      */
201     public void setDestdir(final String dirName) {
202         _destDir = project.resolveFile(dirName);
203     }
204 
205     //--------------------------------------------------------------------------
206 }