Hello! I am proposing this code to create an Ant task for the CodeGenerator in hibernate extension.
SchemaGeneration has already one, I think CodeGenerator needs one too. Example of use : <target name="codegenerator"> <taskdef name="codegenerator" classname="net.sf.hibernate.tool.hbm2java.CodeGeneratorTask" classpathref="hibernate.classpath"/> <codegenerator output="${src}"> <fileset dir="${src}"> <include name="**/*.hbm.xml"/> </fileset> </codegenerator> </target> Nico PS : I am not sure if I am using the right process to contribute. If not : tell me! package net.sf.hibernate.tool.hbm2java; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import net.sf.hibernate.MappingException; import net.sf.hibernate.util.DTDEntityResolver; import org.apache.commons.collections.MultiHashMap; import org.apache.commons.collections.MultiMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.FileSet; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXParseException; /* * Created on 25 mars 2004 By Nicolas Mayeur */ /** * * An Ant task for <tt>CodeGenerator</tt>. * * <pre> * <taskdef name="codegenerator" * classname="net.sf.hibernate.tool.hbm2java.CodeGeneratorTask" * classpathref="class.path"/> * * <codegenerator * output="dirname" * config="filename"> * <fileset dir="${build.classes.dir}"> * <include name="*.hbm.xml"/> * </fileset> * </codegenerator> * </pre> * * @see CodeGenerator * @author Nicolas Mayeur * */ public class CodeGeneratorTask extends Task { private List fileSets = new LinkedList(); private String output = ""; private String config = ""; private static final Log log = LogFactory.getLog(CodeGenerator.class); public void addFileset(FileSet set) { fileSets.add(set); } /** * @param config * The config to set. */ public void setConfig(String config) { this.config = config; } /** * @param output * The output to set. */ public void setOutput(String output) { this.output = output; } public void execute() throws BuildException { try { List generators = new ArrayList(); MultiMap globalMetas = new MultiHashMap(); SAXBuilder builder = new SAXBuilder(); builder.setEntityResolver(new DTDEntityResolver()); builder.setErrorHandler(new ErrorHandler() { public void error(SAXParseException error) { log.error("Error parsing XML: " + error.getSystemId() + '(' + error.getLineNumber() + ')', error); } public void fatalError(SAXParseException error) { error(error); } public void warning(SAXParseException error) { log.warn("Warning parsing XML: " + error.getSystemId() + '(' + error.getLineNumber() + ')'); } }); String outputDir = null; ArrayList mappingFiles = new ArrayList(); mappingFiles = getFiles(); if (!config.equals("")) { // parse config xml file Document document = builder.build(new File(output)); globalMetas = MetaAttributeHelper.loadAndMergeMetaMap(document .getRootElement(), null); Iterator generateElements = document.getRootElement() .getChildren("generate").iterator(); while (generateElements.hasNext()) { generators.add(new Generator((Element) generateElements .next())); } } if (!output.equals("")) { outputDir = output; } // if no config xml file, add a default generator if (generators.size() == 0) { generators.add(new Generator()); } HashMap classMappings = new HashMap(); builder.setValidation(true); for (Iterator iter = mappingFiles.iterator(); iter.hasNext();) { // parse the mapping file File file = new File((String) iter.next()); System.out.println(file.getCanonicalFile()); Document document = builder.build(file); Element rootElement = document.getRootElement(); Iterator classElements = rootElement.getChildren("class") .iterator(); MultiMap mm = MetaAttributeHelper.loadAndMergeMetaMap( rootElement, globalMetas); handleClass(classMappings, classElements, mm, false); classElements = rootElement.getChildren("subclass").iterator(); handleClass(classMappings, classElements, mm, true); classElements = rootElement.getChildren("joined-subclass") .iterator(); handleClass(classMappings, classElements, mm, true); } // generate source files for (Iterator iterator = generators.iterator(); iterator.hasNext();) { Generator g = (Generator) iterator.next(); g.setBaseDirName(outputDir); g.generate(classMappings); } } catch (Exception e) { e.printStackTrace(); } } private static void handleClass(HashMap classMappings, Iterator classElements, MultiMap mm, boolean extendz) throws MappingException { while (classElements.hasNext()) { Element clazz = (Element) classElements.next(); if (!extendz) { ClassMapping cmap = new ClassMapping(clazz, mm); classMappings.put(cmap.getCanonicalName(), cmap); } else { String ex = clazz.getAttributeValue("extends"); if (ex == null) { throw new MappingException("Missing extends attribute on <" + clazz.getName() + " name=" + clazz.getAttributeValue("name") + ">"); } ClassMapping superclass = (ClassMapping) classMappings.get(ex); if (superclass == null) { throw new MappingException("Cannot extend unmapped class " + ex); } ClassMapping subclassMapping = new ClassMapping(superclass .getClassName(), superclass, clazz, mm); superclass.addSubClass(subclassMapping); } } } private ArrayList getFiles() { ArrayList files = new ArrayList(); for (Iterator i = fileSets.iterator(); i.hasNext();) { FileSet fs = (FileSet) i.next(); DirectoryScanner ds = fs.getDirectoryScanner(project); String[] dsFiles = ds.getIncludedFiles(); for (int j = 0; j < dsFiles.length; j++) { File f = new File(dsFiles[j]); if (!f.isFile()) { f = new File(ds.getBasedir(), dsFiles[j]); } files.add(f.getAbsolutePath()); } } return files; } } Yahoo! Mail : votre e-mail personnel et gratuit qui vous suit partout ! Créez votre Yahoo! Mail sur http://fr.benefits.yahoo.com/ Dialoguez en direct avec vos amis grâce à Yahoo! Messenger !Téléchargez Yahoo! Messenger sur http://fr.messenger.yahoo.com ------------------------------------------------------- This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click _______________________________________________ hibernate-devel mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/hibernate-devel