Index: C:/Java/castor-1/anttask/src/main/java/org/castor/anttask/CastorCodeGenTask.java =================================================================== --- C:/Java/castor-1/anttask/src/main/java/org/castor/anttask/CastorCodeGenTask.java (revision 6710) +++ C:/Java/castor-1/anttask/src/main/java/org/castor/anttask/CastorCodeGenTask.java (working copy) @@ -44,6 +44,13 @@ */ package org.castor.anttask; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Properties; +import java.util.Vector; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.taskdefs.MatchingTask; @@ -48,17 +55,14 @@ import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.taskdefs.MatchingTask; import org.apache.tools.ant.types.FileSet; - import org.exolab.castor.builder.FieldInfoFactory; import org.exolab.castor.builder.SourceGenerator; import org.exolab.castor.builder.binding.ExtendedBinding; -import org.exolab.castor.xml.schema.reader.SchemaUnmarshaller; +import org.exolab.castor.util.LocalConfiguration; +import org.exolab.castor.xml.XMLException; +import org.exolab.castor.xml.schema.Schema; import org.exolab.castor.xml.schema.reader.Sax2ComponentReader; -import org.exolab.castor.xml.schema.Schema; -import org.exolab.castor.xml.XMLException; - -import org.exolab.castor.util.LocalConfiguration; - +import org.exolab.castor.xml.schema.reader.SchemaUnmarshaller; import org.xml.sax.InputSource; import org.xml.sax.Parser; import org.xml.sax.SAXException; @@ -63,13 +67,6 @@ import org.xml.sax.Parser; import org.xml.sax.SAXException; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.Properties; -import java.util.Vector; - /** * An Ant task to call the Castor * Source Generator. It can be passed a file, a directory, a Fileset or all @@ -276,16 +273,6 @@ * Sets whether or not marshaling methods are generated. * * @param b If true, marshaling methods are generated. - * @deprecated For the correct spelling, see {@link #setNomarshal(boolean)}. - */ - public void setNomarshall(final boolean b) { - _nomarshal = b; - } - - /** - * Sets whether or not marshaling methods are generated. - * - * @param b If true, marshaling methods are generated. */ public void setNomarshal(final boolean b) { _nomarshal = b; Index: C:/Java/castor-1/anttask/src/main/java/org/exolab/castor/tools/XSDCompiler.java =================================================================== --- C:/Java/castor-1/anttask/src/main/java/org/exolab/castor/tools/XSDCompiler.java (revision 6710) +++ C:/Java/castor-1/anttask/src/main/java/org/exolab/castor/tools/XSDCompiler.java (working copy) @@ -44,14 +44,14 @@ */ package org.exolab.castor.tools; +import java.io.File; import java.io.IOException; -import java.io.File; +import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; -import org.apache.tools.ant.BuildException; -import org.exolab.castor.builder.SourceGenerator; import org.exolab.castor.builder.FieldInfoFactory; +import org.exolab.castor.builder.SourceGenerator; /** * Ant task that enables code generation from an XML _schema from within Ant. Index: C:/Java/castor-1/anttask/src/main/java/org/exolab/castor/tools/ant/taskdefs/CastorSourceGenTask.java =================================================================== --- C:/Java/castor-1/anttask/src/main/java/org/exolab/castor/tools/ant/taskdefs/CastorSourceGenTask.java (revision 6710) +++ C:/Java/castor-1/anttask/src/main/java/org/exolab/castor/tools/ant/taskdefs/CastorSourceGenTask.java (working copy) @@ -44,31 +44,11 @@ */ package org.exolab.castor.tools.ant.taskdefs; -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.DirectoryScanner; +import java.io.File; + import org.apache.tools.ant.taskdefs.MatchingTask; import org.apache.tools.ant.types.FileSet; - -import org.exolab.castor.builder.FieldInfoFactory; -import org.exolab.castor.builder.SourceGenerator; -import org.exolab.castor.builder.binding.ExtendedBinding; -import org.exolab.castor.xml.schema.reader.SchemaUnmarshaller; -import org.exolab.castor.xml.schema.reader.Sax2ComponentReader; -import org.exolab.castor.xml.schema.Schema; -import org.exolab.castor.xml.XMLException; - -import org.exolab.castor.util.LocalConfiguration; - -import org.xml.sax.InputSource; -import org.xml.sax.Parser; -import org.xml.sax.SAXException; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.Properties; -import java.util.Vector; +import org.castor.anttask.CastorCodeGenTask; /** * An Ant task to call the Castor @@ -83,83 +63,8 @@ public final class CastorSourceGenTask extends MatchingTask { //-------------------------------------------------------------------------- - /** Msg indicating class descriptor generation has been disabled. */ - private static final String DISABLE_DESCRIPTORS_MSG = - "Disabling generation of Class descriptors"; - - /** Msg indicating marshaling framework code will not be generated. */ - private static final String DISABLE_MARSHAL_MSG = - "Disabling generation of Marshaling framework methods (marshal, unmarshal, validate)."; - - /** Msg indicating that castor-testable code will be generated. */ - private static final String CASTOR_TESTABLE_MSG = - "The generated classes will implement org.exolab.castor.tests.CastorTestable"; - - /** Error message -- invalid line seperator. */ - private static final String INVALID_LINESEP_MSG = - "Invalid value for lineseparator, must be win, unix, or mac."; - - /** Error message -- no schemas to run code generator on. */ - private static final String NO_SCHEMA_MSG = - "At least one of the file or dir attributes, or a fileset element, must be set."; - - //-------------------------------------------------------------------------- - - /** If processing one schema file, this lists the file. */ - private File _schemaFile = null; - - /** If processing all schemas in a directory, this lists the directory. */ - private File _schemaDir = null; - - /** If processing a fileset, this lists the fileset. */ - private Vector _schemaFilesets = new Vector(); - - // Begin Source Generator parameters - /** The package that generated code will belong to. */ - private String _srcpackage; - - /** The directory that code will be generated into. */ - private String _todir; - - /** Binding file for the code generator. */ - private String _bindingfile; - - /** Information about how to generate collections. */ - private String _types; - - /** Line seperator to use for generated code. */ - private String _lineseparator; - - /** If true, the code generator will be verbose. */ - private boolean _verbose; - - /** If true, non-fatal warnings will be suppressed. Also, existing source - * files will be silently overwritten. */ - private boolean _warnings = true; - - /** If true, class descriptors will not be generated. */ - private boolean _nodesc; - - /** If true, marshaling code will not be generated. */ - private boolean _nomarshal; - - /** If true, Castor's CTF testable framework code will be generated. */ - private boolean _testable; - - /** Whether to generate code for imported schemas, too. */ - private boolean _generateImportedSchemas; - - /** Whether to generate SAX-1 compliant code. */ - private boolean _sax1; - - /** Whether enumerated type lookup should be performed in a case insensitive manner. */ - private boolean _caseInsensitive; - - /** CastorBuilderProperties file. */ - private String _properties; - - /** SourceGenerator instance. */ - private SourceGenerator _sgen; + /** CastorCodeGenTask to delegate all work to. */ + private final CastorCodeGenTask _codeGen = new CastorCodeGenTask(); //-------------------------------------------------------------------------- @@ -166,9 +71,7 @@ /** * No-arg constructor. */ - public CastorSourceGenTask() { - // Nothing needed - } + public CastorSourceGenTask() { } //-------------------------------------------------------------------------- @@ -178,7 +81,7 @@ * @param file One schema file. */ public void setFile(final File file) { - _schemaFile = file; + _codeGen.setFile(file); } /** @@ -188,7 +91,7 @@ * @param dir The directory containing schemas to process. */ public void setDir(final File dir) { - _schemaDir = dir; + _codeGen.setDir(dir); } /** @@ -197,7 +100,7 @@ * @param set An individual file set containing schemas. */ public void addFileset(final FileSet set) { - _schemaFilesets.addElement(set); + _codeGen.addFileset(set); } /** @@ -206,7 +109,7 @@ * @param pack The package that generated code will belong to. */ public void setPackage(final String pack) { - _srcpackage = pack; + _codeGen.setPackage(pack); } /** @@ -215,7 +118,7 @@ * @param dest The directory into which code will be generated. */ public void setTodir(final String dest) { - _todir = dest; + _codeGen.setTodir(dest); } /** @@ -224,7 +127,7 @@ * @param bindingfile The binding file to be used for code generation. */ public void setBindingfile(final String bindingfile) { - _bindingfile = bindingfile; + _codeGen.setBindingfile(bindingfile); } /** @@ -233,7 +136,7 @@ * @param ls The line seperator to use for code generation. */ public void setLineseparator(final String ls) { - _lineseparator = ls; + _codeGen.setLineseparator(ls); } /** @@ -242,7 +145,7 @@ * @param tf The type factory to use for code generation. */ public void setTypes(final String tf) { - _types = (tf.equals("j2")) ? "arraylist" : tf; + _codeGen.setTypes(tf); } /** @@ -251,7 +154,7 @@ * @param b If true, the code generator will be verbose. */ public void setVerbose(final boolean b) { - _verbose = b; + _codeGen.setVerbose(b); } /** @@ -261,7 +164,7 @@ * means that existing source files will be silently overwritten. */ public void setWarnings(final boolean b) { - _warnings = b; + _codeGen.setWarnings(b); } /** @@ -270,7 +173,7 @@ * @param b If true, class descriptors are generated. */ public void setNodesc(final boolean b) { - _nodesc = b; + _codeGen.setNodesc(b); } /** @@ -280,7 +183,7 @@ * @deprecated For the correct spelling, see {@link #setNomarshal(boolean)}. */ public void setNomarshall(final boolean b) { - _nomarshal = b; + _codeGen.setNomarshal(b); } /** @@ -289,7 +192,7 @@ * @param b If true, marshaling methods are generated. */ public void setNomarshal(final boolean b) { - _nomarshal = b; + _codeGen.setNomarshal(b); } /** @@ -298,7 +201,7 @@ * @param b If true, the generated code will be instrumented for the CTF. */ public void setTestable(final boolean b) { - _testable = b; + _codeGen.setTestable(b); } /** @@ -307,7 +210,7 @@ * @param generateImportedSchemas True if code should be generated for imported schemas. */ public void setGenerateImportedSchemas(final boolean generateImportedSchemas) { - _generateImportedSchemas = generateImportedSchemas; + _codeGen.setGenerateImportedSchemas(generateImportedSchemas); } /** @@ -316,7 +219,7 @@ * @param sax1 True if SAX-1 compliant code should be generated. */ public void setSAX1(final boolean sax1) { - _sax1 = sax1; + _codeGen.setSAX1(sax1); } /** @@ -327,7 +230,7 @@ * insensitive manner */ public void setCaseInsensitive(final boolean caseInsensitive) { - _caseInsensitive = caseInsensitive; + _codeGen.setCaseInsensitive(caseInsensitive); } /** @@ -336,7 +239,7 @@ * @param properties The properties to use. */ public void setProperties(final String properties) { - _properties = properties; + _codeGen.setProperties(properties); } //-------------------------------------------------------------------------- @@ -342,104 +245,6 @@ //-------------------------------------------------------------------------- /** - * Configured the code generator. If anything goes wrong during configuration of the - * Ant task a BuildException will be thrown. - */ - private void config() { - // Create Source Generator with appropriate type factory - if (_types != null) { - FieldInfoFactory factory; - try { - factory = new FieldInfoFactory(_types); - _sgen = new CastorSourceGeneratorWrapper(factory); - } catch (Exception e) { - try { - factory = (FieldInfoFactory) Class.forName(_types).newInstance(); - _sgen = new CastorSourceGeneratorWrapper(factory); - } catch (Exception e2) { - throw new BuildException("Invalid types \"" + _types + "\": " + e.getMessage()); - } - } - } else { - // default - _sgen = new CastorSourceGeneratorWrapper(); - } - - // Set Line Separator - String lineSep = System.getProperty("line.separator"); - if (_lineseparator != null) { - if ("win".equals(_lineseparator)) { - log("Using Windows style line separation."); - lineSep = "\r\n"; - } else if ("unix".equals(_lineseparator)) { - log("Using UNIX style line separation."); - lineSep = "\n"; - } else if ("mac".equals(_lineseparator)) { - log("Using Macintosh style line separation."); - lineSep = "\r"; - } else { - throw new BuildException(INVALID_LINESEP_MSG); - } - } - _sgen.setLineSeparator(lineSep); - - _sgen.setDestDir(_todir); - - if (_bindingfile != null) { _sgen.setBinding(_bindingfile); } - - _sgen.setVerbose(_verbose); - _sgen.setSuppressNonFatalWarnings(!_warnings); - - _sgen.setDescriptorCreation(!_nodesc); - if (_nodesc) { log(DISABLE_DESCRIPTORS_MSG); } - - _sgen.setCreateMarshalMethods(!_nomarshal); - if (_nomarshal) { log(DISABLE_MARSHAL_MSG); } - - _sgen.setGenerateImportedSchemas(_generateImportedSchemas); - - _sgen.setSAX1(_sax1); - - _sgen.setCaseInsensitive(_caseInsensitive); - - _sgen.setTestable(_testable); - if (this._testable) { log(CASTOR_TESTABLE_MSG); } - - // Set Builder Properties; - if (_properties != null) { - String filePath = new File(_properties).getAbsolutePath(); - Properties customProperties = new Properties(); - try { - customProperties.load(new FileInputStream(filePath)); - } catch (FileNotFoundException e) { - throw new BuildException("Properties file \"" + filePath + "\" not found"); - } catch (IOException e) { - throw new BuildException("Can't read properties file \"" + filePath + "\": " + e); - } - _sgen.setDefaultProperties(customProperties); - } - } - - /** - * Runs source generation. If anything goes wrong during source generation a - * BuildException will be thrown. - * - * @param filePath an individual Schema to generate code for. - */ - private void processFile(final String filePath) { - log("Processing " + filePath); - try { - _sgen.generateSource(filePath, _srcpackage); - } catch (FileNotFoundException e) { - String message = "XML Schema file \"" + _schemaFile.getAbsolutePath() + "\" not found."; - log(message); - throw new BuildException(message); - } catch (Exception iox) { - throw new BuildException(iox); - } - } - - /** * Public execute method -- entry point for the Ant task. Loops over all * schema that need code generated and creates needed code generators, then * executes them. If anything goes wrong during execution of the Ant task a @@ -448,130 +253,13 @@ * @see org.apache.tools.ant.Task#execute() */ public void execute() { - // Must have something to run the source generator on - if (_schemaFile == null && _schemaDir == null && _schemaFilesets.size() == 0) { - throw new BuildException(NO_SCHEMA_MSG); - } - - try { - config(); - - // Run source generator on file - if (_schemaFile != null) { - processFile(_schemaFile.getAbsolutePath()); - } - - // Run source generator on all files in directory - if (_schemaDir != null && _schemaDir.exists() && _schemaDir.isDirectory()) { - DirectoryScanner ds = this.getDirectoryScanner(_schemaDir); - - String[] files = ds.getIncludedFiles(); - for (int i = 0; i < files.length; i++) { - String filePath = _schemaDir.getAbsolutePath() + File.separator + files[i]; - processFile(filePath); - } - } - - // Run source generator on all files in FileSet - for (int i = 0; i < _schemaFilesets.size(); i++) { - FileSet fs = (FileSet) _schemaFilesets.elementAt(i); - DirectoryScanner ds = fs.getDirectoryScanner(getProject()); - File subdir = fs.getDir(getProject()); - - String[] files = ds.getIncludedFiles(); - for (int j = 0; j < files.length; j++) { - String filePath = subdir.getAbsolutePath() + File.separator + files[j]; - processFile(filePath); - } - } - } finally { - _sgen = null; - } + System.out.println("The class org.exolab.castor.tools.ant.taskdefs.CastorSourceGenTask " + + "has been deprecated. Please use org.castor.anttask.CastorCodeGenTask instead."); + System.out.println(); + + _codeGen.execute(); } - //-------------------------------------------------------------------------- - - /** - * Override Castor's SourceGenerator to inject exception handling. - * Code based on castor-0.9.5.3-xml.jar. - */ - private final class CastorSourceGeneratorWrapper extends SourceGenerator { - /** - * No-arg constructor. - */ - public CastorSourceGeneratorWrapper() { - super(); - } - - /** - * Constructs a source generator with the provided FieldInfoFactory. - * - * @param fieldInfoFactory A FieldInfoFactory to use for collections. - */ - public CastorSourceGeneratorWrapper(final FieldInfoFactory fieldInfoFactory) { - super(fieldInfoFactory); - } - - /** - * Constructs a source generator with the provided FieldInfoFactory and - * binding file. - * - * @param fieldInfoFactory a FieldInfoFactory to use for collections. - * @param extendedBinding binding information for the code generator. - */ - public CastorSourceGeneratorWrapper( - final FieldInfoFactory fieldInfoFactory, final ExtendedBinding extendedBinding) { - super(fieldInfoFactory, extendedBinding); - } - - /** - * Generate source. If anything goes wrong during generation of source a - * BuildException will be thrown. - * - * @param source an individual schema to process. - * @param packageName the package name for generated code. - * - * @see org.exolab.castor.builder.SourceGenerator - * #generateSource(org.xml.sax.InputSource, java.lang.String) - */ - public void generateSource(final InputSource source, final String packageName) { - Parser parser = null; - try { - parser = LocalConfiguration.getInstance().getParser(); - } catch (RuntimeException e) { - throw new BuildException("Unable to create SAX parser.", e); - } - if (parser == null) { - throw new BuildException("Unable to create SAX parser."); - } - - SchemaUnmarshaller schemaUnmarshaller = null; - try { - schemaUnmarshaller = new SchemaUnmarshaller(); - } catch (XMLException e) { - throw new BuildException("Unable to create schema unmarshaller.", e); - } - - Sax2ComponentReader handler = new Sax2ComponentReader(schemaUnmarshaller); - parser.setDocumentHandler(handler); - parser.setErrorHandler(handler); - try { - parser.parse(source); - } catch (IOException e) { - String msg = "Can't read input file " + source.getSystemId() + ".\n" + e; - throw new BuildException(msg, e); - } catch (SAXException e) { - String msg = "Can't parse input file " + source.getSystemId() + ".\n" + e; - throw new BuildException(msg, e); - } - Schema schema = schemaUnmarshaller.getSchema(); - try { - generateSource(schema, packageName); - } catch (Exception iox) { - throw new BuildException(iox); - } - } - } //-------------------------------------------------------------------------- } Index: C:/Java/castor-1/src/doc/release-notes.xml =================================================================== --- C:/Java/castor-1/src/doc/release-notes.xml (revision 6710) +++ C:/Java/castor-1/src/doc/release-notes.xml (working copy) @@ -34,6 +34,27 @@ + + + Made org.exolab.castor.tools.ant.taskdefs.CastorSourceGenTask delegate to + CastorCodeGenTask. + + + Ralf Joachim + ralf.joachim@syscon-world.de + + + Ralf Joachim + ralf.joachim@syscon-world.de + + + Edward Kuns + edward.kuns@aspect.com + + Enh. + XML + 20070103 + Cleaned up 929 'Design For Extension' warnings at builder package. Index: C:/Java/castor-1/src/etc/CHANGELOG =================================================================== --- C:/Java/castor-1/src/etc/CHANGELOG (revision 6710) +++ C:/Java/castor-1/src/etc/CHANGELOG (working copy) @@ -1,6 +1,11 @@ SVN ------------------------------- +XML: Resolved issue CASTOR-1805 using contribution from Ralf Joachim [Ralf.joachim@syscon-world.de] + Made org.exolab.castor.tools.ant.taskdefs.CastorSourceGenTask delegate to CastorCodeGenTask. + Details: http://jira.codehaus.org/browse/CASTOR-1805 + (Ralf - 20070103) + XML: Resolved issue CASTOR-1797 using contribution from Ralf Joachim [Ralf.joachim@syscon-world.de] Cleaned up 929 'Design For Extension' warnings at builder package. Details: http://jira.codehaus.org/browse/CASTOR-1797