Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 263   Methods: 6
NCLOC: 142   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ExtensionUtil.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.taskdefs.optional.extension;
 55   
 
 56   
 import java.io.File;
 57   
 import java.io.IOException;
 58   
 import java.util.ArrayList;
 59   
 import java.util.Iterator;
 60   
 import java.util.jar.JarFile;
 61   
 import java.util.jar.Manifest;
 62   
 import org.apache.tools.ant.BuildException;
 63   
 import org.apache.tools.ant.DirectoryScanner;
 64   
 import org.apache.tools.ant.Project;
 65   
 import org.apache.tools.ant.types.FileSet;
 66   
 
 67   
 /**
 68   
  * A set of useful methods relating to extensions.
 69   
  *
 70   
  * @author <a href="mailto:peter@apache.org">Peter Donald</a>
 71   
  * @version $Revision: 1.3 $ $Date: 2003/02/10 14:14:03 $
 72   
  */
 73   
 public class ExtensionUtil
 74   
 {
 75   
     /**
 76   
      * Convert a list of extensionAdapter objects to extensions.
 77   
      *
 78   
      * @param adapters the list of ExtensionAdapterss to add to convert
 79   
      * @throws BuildException if an error occurs
 80   
      */
 81  0
     static ArrayList toExtensions( final ArrayList adapters )
 82   
         throws BuildException
 83   
     {
 84  0
         final ArrayList results = new ArrayList();
 85   
 
 86  0
         final int size = adapters.size();
 87  0
         for( int i = 0; i < size; i++ )
 88   
         {
 89  0
             final ExtensionAdapter adapter =
 90   
                 (ExtensionAdapter)adapters.get( i );
 91  0
             final Extension extension = adapter.toExtension();
 92  0
             results.add( extension );
 93   
         }
 94   
 
 95  0
         return results;
 96   
     }
 97   
 
 98   
     /**
 99   
      * Generate a list of extensions from a specified fileset.
 100   
      *
 101   
      * @param librarys the list to add extensions to
 102   
      * @param fileset the filesets containing librarys
 103   
      * @throws BuildException if an error occurs
 104   
      */
 105  0
     static void extractExtensions( final Project project,
 106   
                                    final ArrayList librarys,
 107   
                                    final ArrayList fileset )
 108   
         throws BuildException
 109   
     {
 110  0
         if( !fileset.isEmpty() )
 111   
         {
 112  0
             final Extension[] extensions = getExtensions( project,
 113   
                                                           fileset );
 114  0
             for( int i = 0; i < extensions.length; i++ )
 115   
             {
 116  0
                 librarys.add( extensions[ i ] );
 117   
             }
 118   
         }
 119   
     }
 120   
 
 121   
     /**
 122   
      * Retrieve extensions from the specified librarys.
 123   
      *
 124   
      * @param librarys the filesets for librarys
 125   
      * @return the extensions contained in librarys
 126   
      * @throws BuildException if failing to scan librarys
 127   
      */
 128  0
     private static Extension[] getExtensions( final Project project,
 129   
                                               final ArrayList librarys )
 130   
         throws BuildException
 131   
     {
 132  0
         final ArrayList extensions = new ArrayList();
 133  0
         final Iterator iterator = librarys.iterator();
 134  0
         while( iterator.hasNext() )
 135   
         {
 136  0
             final FileSet fileSet = (FileSet)iterator.next();
 137   
 
 138  0
             boolean includeImpl = true;
 139  0
             boolean includeURL = true;
 140   
 
 141  0
             if( fileSet instanceof LibFileSet )
 142   
             {
 143  0
                 LibFileSet libFileSet = (LibFileSet)fileSet;
 144  0
                 includeImpl = libFileSet.isIncludeImpl();
 145  0
                 includeURL = libFileSet.isIncludeURL();
 146   
             }
 147   
 
 148  0
             final DirectoryScanner scanner = fileSet.getDirectoryScanner( project );
 149  0
             final File basedir = scanner.getBasedir();
 150  0
             final String[] files = scanner.getIncludedFiles();
 151  0
             for( int i = 0; i < files.length; i++ )
 152   
             {
 153  0
                 final File file = new File( basedir, files[ i ] );
 154  0
                 loadExtensions( file, extensions, includeImpl, includeURL );
 155   
             }
 156   
         }
 157  0
         return (Extension[])extensions.toArray( new Extension[ extensions.size() ] );
 158   
     }
 159   
 
 160   
     /**
 161   
      * Load list of available extensions from specified file.
 162   
      *
 163   
      * @param file the file
 164   
      * @param extensionList the list to add available extensions to
 165   
      * @throws BuildException if there is an error
 166   
      */
 167  0
     private static void loadExtensions( final File file,
 168   
                                         final ArrayList extensionList,
 169   
                                         final boolean includeImpl,
 170   
                                         final boolean includeURL )
 171   
         throws BuildException
 172   
     {
 173  0
         try
 174   
         {
 175  0
             final JarFile jarFile = new JarFile( file );
 176  0
             final Extension[] extensions =
 177   
                 Extension.getAvailable( jarFile.getManifest() );
 178  0
             for( int i = 0; i < extensions.length; i++ )
 179   
             {
 180  0
                 final Extension extension = extensions[ i ];
 181  0
                 addExtension( extensionList, extension, includeImpl, includeURL );
 182   
             }
 183   
         }
 184   
         catch( final Exception e )
 185   
         {
 186  0
             throw new BuildException( e.getMessage(), e );
 187   
         }
 188   
     }
 189   
 
 190   
     /**
 191   
      * Add extension to list.
 192   
      * If extension should not have implementation details but
 193   
      * does strip them. If extension should not have url but does
 194   
      * then strip it.
 195   
      *
 196   
      * @param extensionList the list of extensions to add to
 197   
      * @param originalExtension the extension
 198   
      * @param includeImpl false to exclude implementation details
 199   
      * @param includeURL false to exclude implementation URL
 200   
      */
 201  0
     private static void addExtension( final ArrayList extensionList,
 202   
                                       final Extension originalExtension,
 203   
                                       final boolean includeImpl,
 204   
                                       final boolean includeURL )
 205   
     {
 206  0
         Extension extension = originalExtension;
 207  0
         if( !includeURL &&
 208   
             null != extension.getImplementationURL() )
 209   
         {
 210  0
             extension =
 211   
                 new Extension( extension.getExtensionName(),
 212   
                                extension.getSpecificationVersion().toString(),
 213   
                                extension.getSpecificationVendor(),
 214   
                                extension.getImplementationVersion().toString(),
 215   
                                extension.getImplementationVendor(),
 216   
                                extension.getImplementationVendorID(),
 217   
                                null );
 218   
         }
 219   
 
 220  0
         final boolean hasImplAttributes =
 221   
             null != extension.getImplementationURL() ||
 222   
             null != extension.getImplementationVersion() ||
 223   
             null != extension.getImplementationVendorID() ||
 224   
             null != extension.getImplementationVendor();
 225   
 
 226  0
         if( !includeImpl && hasImplAttributes )
 227   
         {
 228  0
             extension =
 229   
                 new Extension( extension.getExtensionName(),
 230   
                                extension.getSpecificationVersion().toString(),
 231   
                                extension.getSpecificationVendor(),
 232   
                                null,
 233   
                                null,
 234   
                                null,
 235   
                                extension.getImplementationURL() );
 236   
         }
 237   
 
 238  0
         extensionList.add( extension );
 239   
     }
 240   
 
 241   
     /**
 242   
      * retrieve manifest for specified file.
 243   
      *
 244   
      * @param file the file
 245   
      * @return the manifest
 246   
      * @throws BuildException if errror occurs (file not exist,
 247   
      *         file not a jar, manifest not exist in file)
 248   
      */
 249  0
     static Manifest getManifest( final File file )
 250   
         throws BuildException
 251   
     {
 252  0
         try
 253   
         {
 254  0
             final JarFile jarFile = new JarFile( file );
 255  0
             return jarFile.getManifest();
 256   
         }
 257   
         catch( final IOException ioe )
 258   
         {
 259  0
             throw new BuildException( ioe.getMessage(), ioe );
 260   
         }
 261   
     }
 262   
 }
 263