You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by ru...@locus.apache.org on 2000/03/19 04:35:48 UTC

cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional Script.java

rubys       00/03/18 19:35:48

  Modified:    .        build.xml
               src/main/org/apache/tools/ant Project.java
                        ProjectHelper.java
               src/main/org/apache/tools/ant/taskdefs defaults.properties
  Added:       src/main/org/apache/tools/ant/taskdefs/optional Script.java
  Log:
  Added an *OPTIONAL* task which will allow a script, embedded or external,
  to be executed.
  
  Revision  Changes    Path
  1.13      +5 -2      jakarta-ant/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/build.xml,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- build.xml	2000/02/24 00:57:41	1.12
  +++ build.xml	2000/03/19 03:35:46	1.13
  @@ -28,6 +28,8 @@
   
       <property name="build.compiler" value="classic"/>
   
  +    <available property="bsf.present" classname="com.ibm.bsf.BSFManager" />
  +
       <!-- Give user a chance to override without editing this file 
            (and without typing -D each time it compiles it -->
       <property file="${user.home}/.ant.properties" />
  @@ -50,8 +52,9 @@
              classpath="${classpath}"
              debug="on"
              deprecation="on"
  -           optimize="on"
  -    />
  +           optimize="on" >
  +      <exclude name="**/Script.java" unless="bsf.present" />
  +    </javac>
     </target>
   
     <!-- =================================================================== -->
  
  
  
  1.14      +9 -0      jakarta-ant/src/main/org/apache/tools/ant/Project.java
  
  Index: Project.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/Project.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- Project.java	2000/03/02 20:40:04	1.13
  +++ Project.java	2000/03/19 03:35:47	1.14
  @@ -98,6 +98,7 @@
   
       private Hashtable properties = new Hashtable();
       private Hashtable userProperties = new Hashtable();
  +    private Hashtable references = new Hashtable();
       private String defaultTarget;
       private Hashtable taskClassDefinitions = new Hashtable();
       private Hashtable targets = new Hashtable();
  @@ -772,5 +773,13 @@
               sb.append(c);
           } while(!c.equals(end));
           return new BuildException(new String(sb));
  +    }
  +
  +    public void addReference(String name, Object value) {
  +        references.put(name,value);
  +    }
  +
  +    public Hashtable getReferences() {
  +        return references;
       }
   }
  
  
  
  1.8       +26 -0     jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
  
  Index: ProjectHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ProjectHelper.java	2000/02/28 14:10:46	1.7
  +++ ProjectHelper.java	2000/03/19 03:35:47	1.8
  @@ -137,6 +137,7 @@
               String targetName = element.getAttribute("name");
               String targetDep = element.getAttribute("depends");
               String targetCond = element.getAttribute("if");
  +            String targetId = element.getAttribute("id");
   
               // all targets must have a name
               if (targetName.equals("")) {
  @@ -149,6 +150,9 @@
               target.setCondition(targetCond);
               project.addTarget(targetName, target);
   
  +            if (targetId != null && !targetId.equals("")) 
  +                project.addReference(targetId,target);
  +
               // take care of dependencies
   
               if (targetDep.length() > 0) {
  @@ -214,6 +218,23 @@
               // right now, all we are interested in is element nodes
               // not quite sure what to do with others except drop 'em
   
  +            if (node.getNodeType() == Node.TEXT_NODE) {
  +                String text = ((Text)node).getData();
  +                try {
  +                    Method addProp = targetClass.getMethod(
  +                        "addText", new Class[]{"".getClass()});
  +                    Object child = addProp.invoke(target, new Object[] {text});
  +                } catch (NoSuchMethodException nsme) {
  +                    if (text.trim().length() > 0)
  +                        throw new BuildException(targetClass + 
  +                            " does not support nested text elements");
  +                } catch (InvocationTargetException ite) {
  +                    throw new BuildException(ite.getMessage());
  +                } catch (IllegalAccessException iae) {
  +                    throw new BuildException(iae.getMessage());
  +                }
  +            }
  +
               if (node.getNodeType() == Node.ELEMENT_NODE) {
                   Element element = (Element)node;
                   String propType = element.getTagName();
  @@ -299,6 +320,11 @@
   
                   Method setMethod = (Method)propertySetters.get(attr.getName());
                   if (setMethod == null) {
  +                    if (attr.getName().equals("id")) {
  +                        project.addReference(attr.getValue(), target);
  +                        continue;
  +                    }
  +
                       String msg = "Configuration property \"" + attr.getName() +
                           "\" does not have a setMethod in " + target.getClass();
                       throw new BuildException(msg);
  
  
  
  1.8       +3 -0      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/defaults.properties
  
  Index: defaults.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/defaults.properties,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- defaults.properties	2000/02/13 18:20:00	1.7
  +++ defaults.properties	2000/03/19 03:35:47	1.8
  @@ -27,6 +27,9 @@
   filter=org.apache.tools.ant.taskdefs.Filter
   fixcrlf=org.apache.tools.ant.taskdefs.FixCRLF
   
  +# optional tasks
  +script=org.apache.tools.ant.taskdefs.optional.Script
  +
   # deprecated ant tasks (kept for back compatibility)
   javadoc2=org.apache.tools.ant.taskdefs.Javadoc
   keysubst=org.apache.tools.ant.taskdefs.KeySubst
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/Script.java
  
  Index: Script.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 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 apache@apache.org.
   *
   * 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;
  
  import org.apache.tools.ant.*;
  import java.io.*;
  import java.util.*;
  import com.ibm.bsf.*;
  
  /**
   * Execute a script
   *
   * @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
   */
  public class Script extends Task {
      private String language;
      private String script = "";
      
      /**
       * Do the work.
       *
       * @exception BuildException if someting goes wrong with the build
       */
      public void execute() throws BuildException {
          BSFManager manager = new BSFManager ();
          try {
              // add id references (<task id="foo">)
              Hashtable references = project.getReferences();
              for (Enumeration e = references.keys() ; e.hasMoreElements() ;) {
                  String key = (String)e.nextElement();
                  Object value = references.get(key);
                  manager.declareBean(key, value, value.getClass());
              }
  
              // add properties (<property name="foo">)
              Hashtable properties = project.getProperties();
              for (Enumeration e = properties.keys() ; e.hasMoreElements() ;) {
                  String key = (String)e.nextElement();
                  if (!references.contains(key)) {
                      Object value = properties.get(key);
                      manager.declareBean(key, value, value.getClass());
                  }
              }
  
              // execute the script
              manager.exec(language, "<ANT>", 0, 0, script);
          } catch (BSFException be) {
  	    Exception e = be;
              Throwable te = be.getTargetException();
              if (te != null && te instanceof Exception) e = (Exception) te;
              throw new BuildException(e);
          }
      }
  
      /**
       * Defines the language (required).
       *
       * @param msg Sets the value for the script variable.
       */
      public void setLanguage(String language) {
  	this.language = language;
      }
  
      /**
       * Load the script from an external file 
       *
       * @param msg Sets the value for the script variable.
       */
      public void setSrc(String fileName) {
          File file = new File(fileName);
          if (!file.exists()) 
              throw new BuildException("file " + fileName + " not found.");
  
          int count = (int)file.length();
          byte data[] = new byte[count];
  
          try {
              FileInputStream inStream = new FileInputStream(file);
              inStream.read(data);
              inStream.close();
          } catch (IOException e) {
              throw new BuildException(e);
          }
  	
          script += new String(data);
      }
  
      /**
       * Defines the script.
       *
       * @param msg Sets the value for the script variable.
       */
      public void addText(String text) {
  	this.script += text;
      }
  }