sanders     01/05/05 18:08:17

  Modified:    cjan     build.xml test-cjan.xml
               cjan/repository jakarta-commons.xml
               cjan/src/java/org/apache/tools/ant/taskdefs/optional/cjan
                        CJANSubTask.java CJANTask.java
  Added:       cjan/repository cjan-0.1.dtd
               cjan/src/java/org/apache/tools/ant/taskdefs/optional/cjan
                        CJANGetJar.java CJANInfo.java
  Removed:     cjan/src/java/org/apache/tools/ant/taskdefs/optional/cjan
                        CJANGet.java CJANSubTask.java~
  Log:
  Initial stab at the remote repository XML DTD and Java functionality to
  match.
  
  Revision  Changes    Path
  1.4       +7 -36     jakarta-commons-sandbox/cjan/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cjan/build.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- build.xml 2001/05/02 00:53:37     1.3
  +++ build.xml 2001/05/06 01:08:16     1.4
  @@ -2,7 +2,7 @@
   
   <!--
           "CJAN" component of the Jakarta Commons Subproject
  -        $Id: build.xml,v 1.3 2001/05/02 00:53:37 sanders Exp $
  +        $Id: build.xml,v 1.4 2001/05/06 01:08:16 sanders Exp $
           Author: Scott Sanders <[EMAIL PROTECTED]>
   -->
   
  @@ -24,11 +24,6 @@
     <property name="ant.home"                value="/usr/local/ant"/>
     <property name="jaxp.home"               value="/usr/local/jaxp1.1"/>
     <property name="junit.home"              value="/usr/local/junit3.5"/>
  -  <property name="commons.home"            value="../../jakarta-commons"/>
  -  <property name="sandbox.home"            value="../../jakarta-commons-sanbox"/>
  -  <property name="digester.home"           value="${sandbox.home}/digester"/>
  -  <property name="beanutils.home"          value="${commons.home}/beanutils"/>
  -  <property name="collections.home"        value="${commons.home}/collections"/>
   
   
   <!-- ========== Derived Values ============================================ -->
  @@ -38,11 +33,7 @@
     <property name="ant.jar"                 value="${ant.home}/lib/ant.jar"/>
     <property name="ant.optional.jar"        value="${ant.home}/lib/optional.jar"/>
     <property name="jaxp.jaxp.jar"           value="${jaxp.home}/jaxp.jar"/>
  -  <property name="jaxp.parser.jar"         value="${jaxp.home}/crimson.jar"/>
     <property name="junit.jar"               value="${junit.home}/junit.jar"/>
  -  <property name="digester.jar"            
value="${digester.home}/build/commons-digester.jar"/>
  -  <property name="beanutils.jar"           
value="${beanutils.home}/build/commons-beanutils.jar"/>
  -  <property name="collections.jar"         
value="${collections.home}/build/commons-collections.jar"/>
   
   
   <!-- ========== Component Declarations ==================================== -->
  @@ -52,7 +43,7 @@
     <property name="component.name"          value="cjan"/>
   
     <!-- The title of this component -->
  -  <property name="component.title"         value="XML to Java object 
configuration"/>
  +  <property name="component.title"         value="Automated Jar file download 
utility"/>
   
     <!-- The current version number of this component -->
     <property name="component.version"       value="0.2-dev"/>
  @@ -66,6 +57,8 @@
     <!-- The base directory for distribution targets -->
     <property name="dist.home"               value="dist"/>
   
  +  <property name="dist.jar"                
value="${dist.home}/commons-${component.name}.jar"/>
  +
     <!-- The base directory for component sources -->
     <property name="source.home"             value="src/java"/>
   
  @@ -89,32 +82,10 @@
     <path id="compile.classpath">
       <pathelement location="${build.home}/classes"/>
       <pathelement location="${jaxp.jaxp.jar}"/>
  -    <pathelement location="${jaxp.parser.jar}"/>
       <pathelement location="${ant.jar}"/>
  -    <pathelement location="${ant.optional.jar}"/>
  -    <pathelement location="${digester.jar}"/>
  -    <pathelement location="${beanutils.jar}"/>
  -    <pathelement location="${collections.jar}"/>
  -    <pathelement location="${junit.jar}"/>
  -  </path>
  -
  -
  -<!-- ========== Test Execution Defaults =================================== -->
  -
  -
  -  <!-- Construct unit test classpath -->
  -  <path id="test.classpath">
  -    <pathelement location="${build.home}/classes"/>
  -    <pathelement location="${build.home}/tests"/>
       <pathelement location="${junit.jar}"/>
     </path>
   
  -  <!-- Should all tests fail if one does? -->
  -  <property name="test.failonerror"        value="true"/>
  -
  -  <!-- The test runner to execute -->
  -  <property name="test.runner"             value="junit.textui.TestRunner"/>
  -
   
   <!-- ========== Executable Targets ======================================== -->
   
  @@ -193,7 +164,7 @@
       <mkdir      dir="${dist.home}/javadoc"/>
       <javadoc sourcepath="${source.home}"
                   destdir="${dist.home}/javadoc"
  -           packagenames="org.apache.*"
  +           packagenames="org.apache.tools.ant.taskdefs.optional.cjan.*"
                    author="true"
                   private="true"
                   version="true"
  @@ -207,7 +178,7 @@
      description="Create binary distribution">
       <!-- TODO: top level files like LICENSE and README -->
       <mkdir      dir="${dist.home}"/>
  -    <jar    jarfile="${dist.home}/commons-${component.name}.jar"
  +    <jar    jarfile="${dist.jar}"
               basedir="${build.home}/classes"
              manifest="${build.home}/conf/MANIFEST.MF"/>
       <mkdir      dir="${dist.home}/src"/>
  @@ -220,7 +191,7 @@
     <target name="install" depends="dist"
      description="Install CJAN into $ANT_HOME/lib">
       <copy todir="${ant.home}/lib" 
  -           file="${dist.home}/commons-${component.name}.jar"/>
  +           file="${dist.jar}"/>
     </target>
   
   
  
  
  
  1.3       +25 -10    jakarta-commons-sandbox/cjan/test-cjan.xml
  
  Index: test-cjan.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cjan/test-cjan.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- test-cjan.xml     2001/05/02 00:53:37     1.2
  +++ test-cjan.xml     2001/05/06 01:08:16     1.3
  @@ -1,6 +1,14 @@
  +<!--  $Id: test-cjan.xml,v 1.3 2001/05/06 01:08:16 sanders Exp $ 
  +      Copyright 2001 The Apache Software Foundation.
  +      All rights reserved.
  +-->
   <project name="CJAN Test" default="main" basedir=".">
     <taskdef name="cjan" 
  -      classname="org.apache.tools.ant.taskdefs.optional.cjan.CJANTask"/>
  +      classname="org.apache.tools.ant.taskdefs.optional.cjan.CJANTask">
  +    <classpath>
  +      <pathelement location="${ant.home}/lib/commons-cjan.jar"/>
  +    </classpath>
  +  </taskdef>
   
   
     <target name="main">
  @@ -10,18 +18,23 @@
     </target>
   
   
  +  <!-- This test will download three jars from ${cjan.repository.remote}
  +       with the first 2 tests specifying where to store the jars, and the
  +       third test will copy the jar to ${cjan.repository.local} and then
  +       set a property pointing to it
  +  -->
     <target name="test.cjan.get">
       <echo message="Testing cjan get ..."/>
       <cjan project="jakarta-commons"> 
  -      <get component="commons-beanutils"
  -            revision="0.1-dev" 
  -              toFile="${basedir}/commons-beanutils-0.1-dev.jar"/>
  -      <get component="commons-collections"
  -            revision="0.1-dev" 
  -              toFile="${basedir}/commons-collections-0.1-dev.jar"/>
  -      <get component="commons-digester" 
  -              toFile="${basedir}/commons-digester-0.1-dev.jar"/>
  +      <getjar component="commons-beanutils"
  +               revision="0.1-dev"
  +               property="commons-beanutils.jar"/>
  +      <getjar component="commons-collections"
  +               property="commons-collections.jar"/>
  +      <getjar component="commons-digester" 
  +               property="commons-digester.jar"/>
       </cjan>
  +    <echo message="Property commons-digester.jar == ${commons-digester.jar}"/>
       <echo message="Finished testing cjan."/>
     </target>
   
  @@ -33,7 +46,9 @@
     in ${user.home}/.build.properties, and defaults to:
        http://jakarta.apache.org/cjan
       
  -  <cjan remoteRepository="${cjan.repository.remote">
  +  <cjan remoteRepository="${cjan.repository.remote}" 
  +         localRepository="${cjan.repository.local}"
  +                protocol="http(default) or ftp">
   -->
   
   </project>
  
  
  
  1.3       +19 -7     jakarta-commons-sandbox/cjan/repository/jakarta-commons.xml
  
  Index: jakarta-commons.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/cjan/repository/jakarta-commons.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- jakarta-commons.xml       2001/05/02 01:00:11     1.2
  +++ jakarta-commons.xml       2001/05/06 01:08:16     1.3
  @@ -1,18 +1,30 @@
   <?xml version="1.0" encoding="utf-8"?>
  -<project name="jakarta-commons" version="0.1">
  +<!--  $Id: jakarta-commons.xml,v 1.3 2001/05/06 01:08:16 sanders Exp $ 
  +      Copyright 2001 The Apache Software Foundation.
  +      All rights reserved.
  +-->
  +<!DOCTYPE project SYSTEM "cjan-0.1.dtd">
  +<project name="jakarta-commons" 
  +      version="0.1" 
  +     revision="$Revision: 1.3 $">
     <component name="commons-beanutils" latest-revision="0.1-dev">
  -    <revision version="0.1-dev">
  -      <jar 
url="http://jakarta.apache.org/jars/jakarta-commons/beanutils/commons-beanutils-0.1-dev.jar"/>
  +    <revision number="0.1-dev">
  +      <jar protocol="http" 
url="http://jakarta.apache.org/jars/jakarta-commons/beanutils/commons-beanutils-0.1-dev.jar"/>
       </revision>
     </component>
     <component name="commons-collections" latest-revision="0.1-dev">
  -    <revision version="0.1-dev">
  -      <jar 
url="http://jakarta.apache.org/jars/jakarta-commons/collections/commons-collections-0.1-dev.jar"/>
  +    <revision number="0.1-dev">
  +      <jar protocol="http" 
url="http://jakarta.apache.org/jars/jakarta-commons/collections/commons-collections-0.1-dev.jar"/>
       </revision>
     </component>
     <component name="commons-digester" latest-revision="0.1-dev">
  -    <revision version="0.1-dev">
  -      <jar 
url="http://jakarta.apache.org/jars/jakarta-commons/digester/commons-digester-0.1-dev.jar"/>
  +    <revision number="0.1-dev">
  +      <jar protocol="http" 
url="http://jakarta.apache.org/jars/jakarta-commons/digester/commons-digester-0.1-dev.jar"/>
       </revision>
     </component>
   </project>
  +
  +
  +
  +
  +
  
  
  
  1.1                  jakarta-commons-sandbox/cjan/repository/cjan-0.1.dtd
  
  Index: cjan-0.1.dtd
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <!--  $Id: cjan-0.1.dtd,v 1.1 2001/05/06 01:08:16 sanders Exp $ 
        Copyright 2001 The Apache Software Foundation.
        All rights reserved.
  -->
  <!ELEMENT project (component+)>
  <!ATTLIST project
      name     CDATA #REQUIRED
      version  (0.1) #REQUIRED
      revision CDATA #REQUIRED
  >
  <!ELEMENT component (revision+)>
  <!ATTLIST component
      name            ID    #REQUIRED
      latest-revision CDATA #REQUIRED
  >
  <!ELEMENT revision (jar+)>
  <!ATTLIST revision 
      number CDATA #REQUIRED
  >
  <!ElEMENT jar EMPTY>
  <!ATTLIST jar
      protocol (ftp | http) #REQUIRED
      url      CDATA #REQUIRED
  >
  
  
  
  1.2       +5 -4      
jakarta-commons-sandbox/cjan/src/java/org/apache/tools/ant/taskdefs/optional/cjan/CJANSubTask.java
  
  Index: CJANSubTask.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/cjan/src/java/org/apache/tools/ant/taskdefs/optional/cjan/CJANSubTask.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CJANSubTask.java  2001/04/17 06:49:23     1.1
  +++ CJANSubTask.java  2001/05/06 01:08:17     1.2
  @@ -56,17 +56,18 @@
   
   
   import org.apache.tools.ant.BuildException;
  -import org.apache.tools.ant.Project;
  -import org.apache.tools.ant.Task;
   
   /**
    * Interface for all CJAN sub tasks.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Scott Sanders</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   public interface CJANSubTask {
   
  -    public void execute(Project p, Task t, String localRepo, String remoteRepo) 
throws BuildException;
  +    /**
  +     * Execute the task
  +     */
  +    public void execute(CJANInfo cjan) throws BuildException;
   
   }
  
  
  
  1.2       +143 -51   
jakarta-commons-sandbox/cjan/src/java/org/apache/tools/ant/taskdefs/optional/cjan/CJANTask.java
  
  Index: CJANTask.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/cjan/src/java/org/apache/tools/ant/taskdefs/optional/cjan/CJANTask.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CJANTask.java     2001/04/17 06:49:23     1.1
  +++ CJANTask.java     2001/05/06 01:08:17     1.2
  @@ -55,62 +55,88 @@
   package org.apache.tools.ant.taskdefs.optional.cjan;
   
   
  +import java.io.IOException;
  +import java.net.URL;
   import java.util.Enumeration;
   import java.util.Vector;
   
  +import javax.xml.parsers.DocumentBuilderFactory;
  +import javax.xml.parsers.DocumentBuilder;
  +import javax.xml.parsers.ParserConfigurationException;
  +
  +import org.w3c.dom.Document;
  +import org.w3c.dom.Element;
  +import org.w3c.dom.Node;
  +import org.w3c.dom.NodeList;
  +
  +import org.xml.sax.SAXException;
  +
   import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.Task;
   import org.apache.tools.ant.Project;
  -import org.apache.tools.ant.DirectoryScanner;
  -import org.apache.tools.ant.taskdefs.MatchingTask;
  -import org.apache.tools.ant.BuildException;
   
   /**
    * Class to provide automated jar file download ala CPAN
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Scott Sanders</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
  -
   public class CJANTask extends Task {
       
  +    /**
  +     * The 'state' object of the CJAN system
  +     */
  +    private CJANInfo cjan = new CJANInfo();
   
  -    public CJANTask()
  -    {
  -        super();
  +    /**
  +     *  The name of the project to operate upon
  +     */
  +    private String project   = null;
  +
  +    /**
  +     *  The list of commands for this task
  +     */
  +    private Vector cjanTasks = new Vector();
  +
  +
  +    /**
  +     *  Set the local repository attribute 
  +     */
  +    public void setLocalRepository(String l) { 
  +        cjan.setLocalRepository(l); 
       }
   
  -    private void setDefaults()
  -    {
  -        if (getProject() != null)
  -        {
  -            if (localRepo == null)
  -         {
  -                localRepo = getProject().getProperty("cjan.repository.local");
  -         }
  -            if (remoteRepo == null)
  -         {
  -                remoteRepo = getProject().getProperty("cjan.repository.remote");
  -         }
  -     }        
  -     //System.err.println("localRepo in setDefaults is: " + localRepo);
  -        //System.err.println("remoteRepo in setDefaults is: " + remoteRepo);
  +    /**
  +     *  Sets the project name to locate in the remote repository
  +    */
  +    public void setProject(String p) {
  +        project = p; 
       }
   
       /**
  -     *  The home directory of the local CJAN repository
  +     *  Set the desired protocol to use in downloading 
        */
  -    private String localRepo  = null;
  +    public void setProtocol(String p) { 
  +        cjan.setProtocol(p); 
  +    }
   
       /**
  -     *  The url of the remote repository
  +     *  Set the remote repository attribute 
        */
  -    private String remoteRepo  = null;
  +    public void setRemoteRepository(String r) { 
  +        cjan.setRemoteRepository(r); 
  +    }
   
       /**
  -     *  The list of read/write commands for this task
  +     *  A subTask &lt;get&gt; tag was found.  Create the object, 
  +     *  save it in our list, and return it.
        */
  -    private Vector cjanTasks = new Vector();
  +    public CJANSubTask createGetjar()
  +    {
  +        CJANSubTask task = new CJANGetJar();
  +        cjanTasks.addElement(task);
  +        return task;
  +    }
   
       /** 
        *  Verify that all parameters are included. 
  @@ -120,42 +146,108 @@
       public void execute() throws BuildException 
       {
           setDefaults();
  -        /**  A localRepo is required to continue */
  -        if (localRepo == null)
  -            throw new BuildException("No Local Repository Specified. Please set the 
cjan.repository.local property or set the localRepository attribute.");
  +
  +        /**  A project is required to continue */
  +        if (project == null)
  +            throw new BuildException("No Project Specified. Please set the project 
attribute.");
   
           /**  A remoteRepo is required to continue */
  -        if (remoteRepo == null)
  -            throw new BuildException("No Remote Repository Specified. Please set 
the cjan.repository.remote property or set the remoteRepository attribute.");
  +        if (cjan.getRemoteRepository() == null)
  +            throw new BuildException("No Remote Repository Specified. Please set 
the " +
  +                                     CJANInfo.REMOTE_REPO + 
  +                                     " property or set the remoteRepository 
attribute.");
  +
  +        //Parse the project definition and find the URL of the jar to download
  +        String projectURI = cjan.getRemoteRepository() + "/" + project + ".xml";
  +        cjan.getProject().log("Attempting to download project repository 
definition" +
  +                              projectURI + ".", Project.MSG_INFO);
  +        
  +        Document xml = getProjectDocument(projectURI);
  +        if (xml == null) {
  +            throw new BuildException("Could not parse document " + projectURI);
  +        }
   
  +        cjan.setDocument(xml);
  +
           /**  Process each sub command */
           Enumeration tasksToRun = cjanTasks.elements();
           while (tasksToRun != null && tasksToRun.hasMoreElements())
           {
               CJANSubTask task = (CJANSubTask) tasksToRun.nextElement();
  -            task.execute(getProject(), (Task)this, localRepo, remoteRepo);
  +            task.execute(cjan);
           }
       }
   
       /**
  -     *  Set the local repository attribute 
  +     * Make sure that the necessary properties have been set prior to execute()
        */
  -    public void setLocalRepository(String r) { this.localRepo = r; }
  +    private void setDefaults() {
  +        if (getProject() != null) {
   
  -    /**
  -     *  Set the remote repository attribute 
  -     */
  -    public void setRemoteRepository(String r) { this.remoteRepo = r; }
  +            cjan.setProject(getProject());
  +            cjan.setTask((Task)this);
   
  -    /**
  -     *  A subTask &lt;get&gt; tag was found.  Create the object, 
  -     *  save it in our list, and return it.
  -     */
  -    public CJANSubTask createGet()
  -    {
  -        CJANSubTask task = new CJANGet();
  -        cjanTasks.addElement(task);
  -        return task;
  +            // Check to see if we need to set the remote repo location
  +            if (cjan.getRemoteRepository() == null) {
  +                cjan.setRemoteRepository(getProject().
  +                                         getProperty(CJANInfo.REMOTE_REPO));
  +
  +                // Is the remoteRepo still empty?  Set it to the default
  +                if (cjan.getRemoteRepository() == null) {
  +                    cjan.setRemoteRepository(CJANInfo.REMOTE_REPO_DEFAULT);
  +                }   
  +             }
  +            // Check to see if we need to set the local repo location
  +            if (cjan.getLocalRepository() == null) {
  +                cjan.setLocalRepository(getProject().
  +                                        getProperty(CJANInfo.LOCAL_REPO));
  +                }   
  +             }
  +         }        
  +
  +    /**
  +     * Download the proxect.xml file from the remote repository and return it
  +     * as a DOM Document
  +     */
  +    private Document getProjectDocument(String remote) {
  +      
  +        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  +
  +        dbf.setCoalescing(false);
  +        dbf.setExpandEntityReferences(true);
  +        dbf.setIgnoringComments(true);
  +        dbf.setIgnoringElementContentWhitespace(true);
  +        dbf.setNamespaceAware(false);
  +        dbf.setValidating(true);
  +        
  +        try {
  +            DocumentBuilder db = dbf.newDocumentBuilder();
  +            Document doc = db.parse(remote);
  +
  +            Element docElem = doc.getDocumentElement();
  +            //Check to make sure the version information is correct.
  +            if (docElem.getAttribute(CJANInfo.XML_ATTR_VERSION) == 
CJANInfo.DTD_VERSION) {
  +                return doc;
  +            } else {
  +                getProject().log("Repository version: " +
  +                                 docElem.getAttribute(CJANInfo.XML_ATTR_VERSION) +
  +                                 " is not equal to: " +
  +                                 CJANInfo.DTD_VERSION, Project.MSG_INFO);
  +                return null;
  +            }
  +                
  +        } catch (IOException e) {
  +            System.out.println("IOException:");
  +            e.printStackTrace(System.out);
  +        } catch (SAXException e) {
  +            System.out.println("SAXException:");
  +            e.printStackTrace(System.out);
  +        } catch (ParserConfigurationException e) {
  +            System.out.println("ParserConfigurationException:");
  +            e.printStackTrace(System.out);
  +        }
  +        
  +        return null;
       }
  -
   }
  +
  
  
  
  1.1                  
jakarta-commons-sandbox/cjan/src/java/org/apache/tools/ant/taskdefs/optional/cjan/CJANGetJar.java
  
  Index: CJANGetJar.java
  ===================================================================
  /*
   * 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", "Ant", 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.cjan;
  
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.InputStream;
  
  import java.net.MalformedURLException;
  import java.net.URL;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Project;
  
  /**
   * Class to provide get functionality for CJAN.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Scott Sanders</a>
   * @version $Revision: 1.1 $
   */
  public class CJANGetJar extends Object implements CJANSubTask {
  
      private String component = null;
      private String property  = null;
      private String revision  = CJANInfo.LATEST_REVISION;
  
      /**
       *  Sets the component name to retrieve from the remote repository
       */
      public void setComponent(String c) { 
          component = c; 
      }
  
      /**
       * The name of the property to set the resulting downloaded filename to
       */
      public void setProperty(String p) {
          property = p;
      }
  
      /**
       *  Sets the component revisiont to be retrieved.
       *  Defaults to &quot;latest&quot;
       */
      public void setRevision(String r) { 
          revision = r; 
      }
  
      /**
       * Execute the task to download a jar file from the CJAN remote
       * repository.
       */
      public void execute(CJANInfo cjan) throws BuildException {
  
          Element elem = cjan.getDocument().getElementById(component);
          
          String remoteURLStr = getJarDownloadURL(cjan, elem);
          URL remoteURL = null;
  
          try {
              remoteURL = new URL(remoteURLStr);
          } catch (MalformedURLException e) {
              throw new BuildException("URL is malformed: " + remoteURLStr);
          }
         
          String localFileName = cjan.getLocalRepository() + "/" + remoteURL.getFile();
  
          File localFile = new File(localFileName);
  
          if (localFile.canRead()) {
  
              //No need to download the file, as it already exists
              cjan.getProject().log(cjan.getTask(), "File: " + localFileName + 
                         " already exists.  No action taken.", 
                         Project.MSG_INFO);
          } else {
                  
              //Now download the file
              cjan.getProject().log(cjan.getTask(), "Attempting to download " + 
                                    remoteURL.toString() + 
                                    " -> " + localFileName, Project.MSG_INFO);
              WriteURLToFile(remoteURL, localFileName);
  
              if (cjan.getProject().getProperty(property) == null) {
                  cjan.getProject().log(cjan.getTask(), "Setting property " + property 
+
                              " to " + localFileName, Project.MSG_DEBUG);
                  cjan.getProject().setProperty(property, localFileName);
              }
          }
      }
  
      /**
       * Walk the DOM and find the exact URL to download from, given the revision, 
protocol, etc.
       */
      private String getJarDownloadURL(CJANInfo cjan, Element elem) {
          if (elem != null) {
              if (revision == CJANInfo.LATEST_REVISION) {
                  revision = elem.getAttribute(CJANInfo.XML_ATTR_LATEST_REVISION);
              }
              NodeList revChildren = 
elem.getElementsByTagName(CJANInfo.XML_ELEM_REVISION);
              for (int i = 0; i < revChildren.getLength(); i++) {
                  Element revElem = (Element)revChildren.item(i);
                  if (revElem.getAttribute(CJANInfo.XML_ATTR_REVISION_NUMBER) == 
revision) {
                      NodeList jarChildren = 
revElem.getElementsByTagName(CJANInfo.XML_ELEM_JAR);
                      for (int j = 0; j < jarChildren.getLength(); j++) {
                          Element jarElem = (Element)jarChildren.item(j);
                          if (jarElem.getAttribute(CJANInfo.XML_ATTR_PROTOCOL) == 
cjan.getProtocol()) {
                              return jarElem.getAttribute(CJANInfo.XML_ATTR_URL);
                          }
                      }
                  }
              }
          } 
          return null;
      }
  
      /**
       * Write the given URL to a local file specified in outFile
       */
      private void WriteURLToFile(URL inURL, String outFile) {
  
          try {             
              FileOutputStream out = new FileOutputStream(outFile);
              InputStream in = inURL.openStream();
              
              byte[] buffer = new byte[8 * 1024];
              int count = 0;
              do {
                  out.write(buffer, 0, count);
                  count = in.read(buffer, 0, buffer.length);
              } while (count != -1);
              
              in.close();
              out.close();
          } catch (Exception e) {
              e.printStackTrace(System.out);
          }
      }
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/cjan/src/java/org/apache/tools/ant/taskdefs/optional/cjan/CJANInfo.java
  
  Index: CJANInfo.java
  ===================================================================
  /*
   * 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", "Ant", 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.cjan;
  
  
  import org.w3c.dom.Document;
  
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.Task;
  
  /**
   * Class to provide repository information for CJAN.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Scott Sanders</a>
   * @version $Revision: 1.1 $
   */
  public class CJANInfo extends Object {
  
      public static final String LATEST_REVISION = "latest";
      public static final String DEFAULT_PROTOCOL = "http";
      public static final String REMOTE_REPO = "cjan.repository.remote";
      public static final String LOCAL_REPO = "cjan.repository.local";
      public static final String REMOTE_REPO_DEFAULT = 
"http://jakarta.apache.org/cjan/repository/0.1";;
      public static final String DTD_VERSION = "0.1";
  
      public static final String XML_ATTR_LATEST_REVISION = "latest-revision";
      public static final String XML_ATTR_PROTOCOL        = "protocol";
      public static final String XML_ATTR_REVISION_NUMBER = "number";
      public static final String XML_ATTR_URL             = "url";
      public static final String XML_ATTR_VERSION         = "version";
  
      public static final String XML_ELEM_COMPONENT = "component";
      public static final String XML_ELEM_JAR       = "jar";
      public static final String XML_ELEM_REVISION  = "revision";
  
  
      private String localRepo  = null;
      private String remoteRepo = null;
      private String protocol   = DEFAULT_PROTOCOL;
  
      private Document document = null;
      private Project project   = null;
      private Task task         = null;
  
      /**
       * The XML Document for the CJAN project
       */
      public Document getDocument() {
          return document;
      }
  
      /**
       * Set the XML Document for the CJAN project
       */
      public void setDocument(Document d) {
          document = d;
      }
  
      /**
       * Returns the local repository location
       */
      public String getLocalRepository() {
          return localRepo;
      }
  
      /**
       *  Sets the local repository
       */
      public void setLocalRepository(String l) { 
          localRepo = l; 
      }
  
      /**
       * The parent Ant Project for logging purposes
       */
      public Project getProject() {
          return project;
      }
  
      /**
       * Set the parent Ant Project for logging purposes
       */
      public void setProject(Project p) {
          project = p;
      }
  
      /**
       * Returns the desired protocol to use in downloading
       */
      public String getProtocol() {
          return protocol;
      }
  
      /**
       *  Sets the desired protocol
       */
      public void setProtocol(String p) { 
          protocol = p; 
      }
  
      /**
       * Returns the remote repository location
       */
      public String getRemoteRepository() {
          return remoteRepo;
      }
  
      /**
       *  Sets the remote repository
       */
      public void setRemoteRepository(String r) { 
          remoteRepo = r; 
      }
  
      /**
       * The parent Ant Task for logging purposes
       */
      public Task getTask() {
          return task;
      }
  
      /**
       * Set the parent Ant Task for logging purposes
       */
      public void setTask(Task t) {
          task = t;
      }
  
  }
  
  
  

Reply via email to