Hi,

  Here are the taskdef for XMLC (xmlc.enhydra.org).

Both of them go under:

  src/main/org/apache/tools/ant/taskdefs/optional/enhydra

Additional to build.xml

For target check_for_optional_packages:

    <available property="xmlc.present"
classname="org.enhydra.xml.xmlc.commands.xmlc.XMLC"/>

For target compile:

      <exclude
name="org/apache/tools/ant/taskdefs/optional/enhydra/*.java"
unless="xmlc.present" />

David Li
DigitalSesame
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant.taskdefs.optional.enhydra;

import java.io.*;
import java.util.*;

import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.*;

import org.enhydra.xml.xmlc.commands.xmlc.XMLC;

/**
 * This class defines a task for Enhydra <a 
href="http://xmlc.enhydra.org";>XMLC</a>
 * The parameters to the XMLC task are passed in using arg element.
 * 
 * <p>For example:
 * <pre>
 *   &lt;xmlc srcdir=&quot;.&quot; destdir=&quot;classes&quot;&gt;
 *     &lt;arg name=&quot;xforms&quot;/&gt;
 *     &lt;arg name=&quot;urlmapping&quot; param1=&quot;.*.html&quot; 
param2=&quot;*.po&quot;/&gt;
 *   &lt;/xmlc&gt;
 * </pre>
 *
 * @author David Li <[EMAIL PROTECTED]>
 */
public class Xmlc extends MatchingTask 
{
    /**
     * Number of threads for XmlcRun
     */
    int threadCount = 2;

    /**
     * Directory for the source *ML files
     */
    private File srcDir;

    /**
     * Directory for the generated Java files
     */
    private File sourceOut;

    /**
     * Do the package prefix rewrite 
     * Ugly hack, performance improvement potential here. (david)
     */
    private String rewritePrefix (String pkgname) {
        PrefixMap pm;

        for (int i = 0; i < prefixMaps.size() ; i++) {
            pm = (PrefixMap)prefixMaps.get(i);
            if (pkgname.startsWith(pm.oldPrefix)) {
                log ("  replace \"" + pkgname + "\"", project.MSG_VERBOSE);
                pkgname = pm.newPrefix + 
pkgname.substring(pm.oldPrefix.length());
                log ("     with \"" + pkgname + "\"", project.MSG_VERBOSE);
                break;
            }
            i++;
        }

        return pkgname;
    }

    /**
     * Execution method for Ant task
     */
    public void execute() throws BuildException {

        if (srcDir == null) {
            throw new BuildException("srcdir attribute must be set!", location);
        }

        if (sourceOut == null) {
            throw new BuildException("sourceout attribute must be set!", 
location);
        }

        /* default options set for XMLC */
        add("keep");
        add("nocompile");
        add("sourceout", sourceOut.getAbsolutePath());

        /* 
         * read the argument from the list 
         * reserve two space for -class 
         */
        int count = args.size();
        String[] argv = new String[count + 3];
        args.toArray(argv);

        String htmlClassName;
        XMLC xmlc = new XMLC();
        DirectoryScanner dscan = getDirectoryScanner(srcDir);
        String[] files = dscan.getIncludedFiles();

        for (int i = 0 ; i < files.length; i++) {

            log("processing " + files[i], project.MSG_VERBOSE);

            /* compute the output class name */
            htmlClassName = files[i].replace(File.pathSeparatorChar, '.');
            htmlClassName = files[i].replace('\\', '.');
            htmlClassName = htmlClassName.replace('/', '.');
            String ext = htmlClassName.substring(htmlClassName.lastIndexOf('.') 
+ 1);
            htmlClassName = htmlClassName.substring(0, 
htmlClassName.lastIndexOf('.'));
            htmlClassName += ext.toUpperCase();
            
            htmlClassName = rewritePrefix (htmlClassName);
                    
            log("  className: " + htmlClassName, project.MSG_VERBOSE);

            log ("  XMLC args: ", project.MSG_DEBUG);
            for (int j = 0 ; j < argv.length; j++)
                log("    argv[" + j + "] = " + argv[j], project.MSG_DEBUG);

            File srcFile = new File(srcDir, files[i]);
            /* ouch! This is Unix only. Fix sometime */
            File javaFile = new File(sourceOut, htmlClassName.replace('.', '/') 
+ ".java");

            log ("    srcFile = " + srcFile.getAbsolutePath(), 
project.MSG_VERBOSE);
            log ("    javaFile = " + javaFile.getAbsolutePath(), 
project.MSG_VERBOSE);

            /* set up output class */
            argv[count] = "-class";
            argv[count+1] = htmlClassName;
            argv[count+2] = srcFile.getAbsolutePath();

            if (!javaFile.exists() || srcFile.lastModified() > 
javaFile.lastModified()) {
                XmlcRun.addMetaData(argv);
            }
        }

        log("run with " + threadCount + " threads to process " + 
XmlcRun.getNumberOfFiles() + " files");
        XmlcRun.compile(this);
    }

    /**
     * srcdir attribute
     */
    public void setSrcdir(String srcdir) {
        srcDir = project.resolveFile(srcdir); 
        srcDir.mkdirs();
    }

    /**
     * sourceout attribute
     */
    public void setSourceout(String sourceout) {
        sourceOut = project.resolveFile(sourceout);
    }

    /**
     * threadcount attribute
     */
    public void setThreadcount(int threadCount) {
        this.threadCount = threadCount;
    }

    /**
     * To process a list of &lt;prefixmap&gt;
     */
    private ArrayList prefixMaps = new ArrayList();

    public class PrefixMap {
        String oldPrefix;
        String newPrefix;

        public void setOld (String oldPrefix) {
            this.oldPrefix = oldPrefix;
        }

        public void setNew (String newPrefix) {
            this.newPrefix = newPrefix;
        }
    }

    /** 
     * To process a list of &lt;prefixmap&gt;
     */
    public PrefixMap createPrefixmap() {
        PrefixMap pmap = new PrefixMap();
        prefixMaps.add (pmap);
        return pmap;
    }

    /**
     * To process a list of &lt;arg&gt;
     */
    public Xmlc createArg() {
        return this;
    }

    private ArrayList args = new ArrayList();

    public void setName (String name) {
        args.add("-" + name);
    }
        
    public void setParam1 (String param1) {
        args.add(param1);
    }

    public void setParam2 (String param2) {
        args.add(param2);
    }

    public void add(String name) {
        setName(name);
    }

    public void add(String name, String param1) {
        setName(name);
        setParam1(param1);
    }

    public void add(String name, String param1, String param2) {
        setName(name);
        setParam1(param1);
        setParam2(param2);
    }
}
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant.taskdefs.optional.enhydra;

import java.util.*;
import java.io.IOException;

import org.apache.tools.ant.*;

import org.enhydra.xml.xmlc.commands.xmlc.XMLC;
import org.enhydra.xml.xmlc.metadata.MetaData;
import org.enhydra.xml.xmlc.XMLCException;

/**
 * Real XMLC working
 */
class XmlcRun implements Runnable {
    /** 
     * static part to hold list of xmlcRun to be run
     */
    private static Stack metaDataList = new Stack();

    static void addMetaData (String args[]) {
        String[] argsCopy = new String[args.length];
        System.arraycopy(args, 0, argsCopy, 0, args.length);
        metaDataList.push(argsCopy);
    }

    static int getNumberOfFiles() {
        return metaDataList.size();
    }
    
    /**
     * Reference to Xmlc Ant task for logging
     */
    static Xmlc xmlcTask = null;

    /**
     * compile all the files with XMLC
     * @param x Xmlc object for logging
     */
    static void compile(Xmlc x) {
        xmlcTask = x;
        Thread[] threads = new Thread[x.threadCount];
        for (int i = 0; i < x.threadCount; i++) {
            threads[i] = new Thread(new XmlcRun(), "XmlcRunThread" + i);
            threads[i].start();
        }

        for (int i = 0 ; i < x.threadCount; i++) {
            try {
                threads[i].join();
            } catch (Exception e) {
                log("XmlcRun: ERROR " + e.getMessage());
            }
        }
    }

    static synchronized void log (String msg, int level) {
        xmlcTask.log(msg, level);
    }

    static synchronized void log (String msg) {
        xmlcTask.log(msg);
    }

    /**
     * 
     */
    public void run () {
        String[] args;
        XMLC x = new XMLC();
        try {
            while (true) {
                args = (String[])metaDataList.pop();
                x.compile(args);

                /* FIXME: bad assumption on the last element 
                 * of array to be the source file. */
                log("XmlcRun: " 
                    + Thread.currentThread().getName() 
                    + " process " 
                    + args[args.length - 1]);
            }
        } catch (EmptyStackException see) {
            log ("XmlcRun: " + Thread.currentThread().getName() + " done", 
Project.MSG_VERBOSE);
        } catch (Exception e) {
            log ("XmlcRun: " + Thread.currentThread().getName() + 
e.getMessage());
        }
    }
}

Reply via email to