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