Coverage Report - org.geotoolkit.maven.jar.PackFiles
 
Classes in this File Line Coverage Branch Coverage Complexity
PackFiles
0 %
0/63
0 %
0/36
5,8
 
 1  
 /*
 2  
  *    Geotoolkit.org - An Open Source Java GIS Toolkit
 3  
  *    http://www.geotoolkit.org
 4  
  *
 5  
  *    (C) 2008-2012, Open Source Geospatial Foundation (OSGeo)
 6  
  *    (C) 2009-2012, Geomatys
 7  
  *
 8  
  *    This library is free software; you can redistribute it and/or
 9  
  *    modify it under the terms of the GNU Lesser General Public
 10  
  *    License as published by the Free Software Foundation;
 11  
  *    version 2.1 of the License.
 12  
  *
 13  
  *    This library is distributed in the hope that it will be useful,
 14  
  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16  
  *    Lesser General Public License for more details.
 17  
  */
 18  
 package org.geotoolkit.maven.jar;
 19  
 
 20  
 import java.io.*;
 21  
 import java.util.Arrays;
 22  
 import java.util.Comparator;
 23  
 import java.util.zip.ZipEntry;
 24  
 import java.util.zip.ZipOutputStream;
 25  
 
 26  
 
 27  
 /**
 28  
  * Packs javadoc and source files.
 29  
  *
 30  
  * @author Martin Desruisseaux (Geomatys)
 31  
  * @version 3.00
 32  
  *
 33  
  * @since 3.00
 34  
  */
 35  0
 final class PackFiles implements FileFilter, Comparator<File> {
 36  
     /**
 37  
      * The directory where to write the ZIP files.
 38  
      */
 39  
     private final File targetDirectory;
 40  
 
 41  
     /**
 42  
      * The source directory for the javadoc or the source files.
 43  
      */
 44  
     private String sourcePath;
 45  
 
 46  
     /**
 47  
      * The output stream in process of being written.
 48  
      */
 49  
     private ZipOutputStream out;
 50  
 
 51  
     /**
 52  
      * Temporary buffer for copying data.
 53  
      */
 54  0
     private final byte[] buffer = new byte[8 * 1024];
 55  
 
 56  
     /**
 57  
      * Creates a new object which will packs the javadoc and source files in the given directory.
 58  
      *
 59  
      * @param targetDirectory The directory where to write the ZIP files.
 60  
      */
 61  0
     public PackFiles(final File targetDirectory) {
 62  0
         this.targetDirectory = targetDirectory;
 63  0
     }
 64  
 
 65  
     /**
 66  
      * Filters the files to be included in the ZIP file. We omit hidden files and everything
 67  
      * beginning with a dot (this is the same than hidden files on Unix only). Also omit the
 68  
      * target directory created by Maven, and the Derby log file.
 69  
      */
 70  
     @Override
 71  
     public boolean accept(final File file) {
 72  0
         if (file.isHidden()) {
 73  0
             return false;
 74  
         }
 75  0
         final String name = file.getName();
 76  0
         if (file.isDirectory()) {
 77  0
             if (name.equalsIgnoreCase("target")) {
 78  0
                 return false;
 79  
             }
 80  
         }
 81  0
         if (file.isFile()) {
 82  0
             if (name.equalsIgnoreCase("derby.log")) {
 83  0
                 return false;
 84  
             }
 85  
         }
 86  0
         return !name.startsWith(".");
 87  
     }
 88  
 
 89  
     /**
 90  
      * Sorts the files to be included in ZIP files. We put files before directories.
 91  
      */
 92  
     @Override
 93  
     public int compare(final File file1, final File file2) {
 94  0
         if (file1.isDirectory()) {
 95  0
             if (!file2.isDirectory()) {
 96  0
                 return +1;
 97  
             }
 98  0
         } else if (file2.isDirectory()) {
 99  0
             return -1;
 100  
         }
 101  0
         return file1.getName().compareToIgnoreCase(file2.getName());
 102  
     }
 103  
 
 104  
     /**
 105  
      * Packs everything in the given source directory.
 106  
      *
 107  
      * @param  source The source directory containing the file to ZIP.
 108  
      * @param  targetFilename The name of the file to create.
 109  
      * @throws IOException if an error occurred while packing the javadoc.
 110  
      */
 111  
     public void pack(final File sourceDirectory, final String targetFilename) throws IOException {
 112  0
         sourcePath = sourceDirectory.getPath();
 113  0
         if (!sourcePath.endsWith(File.separator)) {
 114  0
             sourcePath += File.separatorChar;
 115  
         }
 116  0
         final File outFile = new File(targetDirectory, targetFilename);
 117  0
         out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
 118  0
         out.setLevel(9);
 119  0
         addEntries(sourceDirectory);
 120  0
         out.close();
 121  0
         out = null;
 122  0
         sourcePath = null;
 123  0
     }
 124  
 
 125  
     /**
 126  
      * Adds the entries for the given directory. This method invokes itself recursively.
 127  
      */
 128  
     private void addEntries(final File directory) throws IOException {
 129  0
         final File[] content = directory.listFiles(this);
 130  0
         if (content == null) {
 131  0
             return;
 132  
         }
 133  0
         Arrays.sort(content, this);
 134  0
         for (final File file : content) {
 135  0
             final boolean isDirectory = file.isDirectory();
 136  0
             String name = file.getPath();
 137  0
             if (!name.startsWith(sourcePath)) {
 138  
                 // Should never happen.
 139  0
                 throw new IOException("Illegal path: " + name);
 140  
             }
 141  0
             name = name.substring(sourcePath.length()).replace(File.separatorChar, '/');
 142  0
             if (isDirectory) {
 143  0
                 name += '/';
 144  
             }
 145  0
             final ZipEntry entry = new ZipEntry(name);
 146  0
             entry.setTime(file.lastModified());
 147  0
             if (isDirectory) {
 148  0
                 entry.setMethod(ZipEntry.STORED);
 149  0
                 entry.setSize(0);
 150  0
                 entry.setCompressedSize(0);
 151  0
                 entry.setCrc(0);
 152  
             } else {
 153  0
                 entry.setMethod(ZipEntry.DEFLATED);
 154  0
                 entry.setSize(file.length());
 155  
             }
 156  0
             out.putNextEntry(entry);
 157  0
             if (!isDirectory) {
 158  0
                 final InputStream in = new FileInputStream(file);
 159  0
                 int n; while ((n = in.read(buffer)) >= 0) {
 160  0
                     out.write(buffer, 0, n);
 161  
                 }
 162  0
                 in.close();
 163  
             }
 164  0
             out.closeEntry();
 165  0
             if (isDirectory) {
 166  0
                 addEntries(file);
 167  
             }
 168  
         }
 169  0
     }
 170  
 }