Coverage Report - net.sourceforge.cobertura.reporting.html.files.CopyFiles
 
Classes in this File Line Coverage Branch Coverage Complexity
CopyFiles
87%
39/45
62%
5/8
4.5
 
 1  
 /*
 2  
  * Cobertura - http://cobertura.sourceforge.net/
 3  
  *
 4  
  * Copyright (C) 2003 jcoverage ltd.
 5  
  * Copyright (C) 2005 Mark Doliner
 6  
  *
 7  
  * Cobertura is free software; you can redistribute it and/or modify
 8  
  * it under the terms of the GNU General Public License as published
 9  
  * by the Free Software Foundation; either version 2 of the License,
 10  
  * or (at your option) any later version.
 11  
  *
 12  
  * Cobertura is distributed in the hope that it will be useful, but
 13  
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 14  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15  
  * General Public License for more details.
 16  
  *
 17  
  * You should have received a copy of the GNU General Public License
 18  
  * along with Cobertura; if not, write to the Free Software
 19  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 20  
  * USA
 21  
  */
 22  
 
 23  
 package net.sourceforge.cobertura.reporting.html.files;
 24  
 
 25  
 import java.io.File;
 26  
 import java.io.FileOutputStream;
 27  
 import java.io.IOException;
 28  
 import java.io.InputStream;
 29  
 
 30  0
 public abstract class CopyFiles
 31  
 {
 32  
 
 33  
         public static void copy(File destinationDir) throws IOException
 34  
         {
 35  5
                 File cssOutputDir = new File(destinationDir, "css");
 36  5
                 File imagesOutputDir = new File(destinationDir, "images");
 37  5
                 File jsOutputDir = new File(destinationDir, "js");
 38  
 
 39  5
                 destinationDir.mkdirs();
 40  5
                 cssOutputDir.mkdir();
 41  5
                 imagesOutputDir.mkdir();
 42  5
                 jsOutputDir.mkdir();
 43  
 
 44  5
                 copyResourceFromJar("help.css", cssOutputDir);
 45  5
                 copyResourceFromJar("main.css", cssOutputDir);
 46  5
                 copyResourceFromJar("sortabletable.css", cssOutputDir);
 47  5
                 copyResourceFromJar("source-viewer.css", cssOutputDir);
 48  5
                 copyResourceFromJar("tooltip.css", cssOutputDir);
 49  
 
 50  5
                 copyResourceFromJar("blank.png", imagesOutputDir);
 51  5
                 copyResourceFromJar("downsimple.png", imagesOutputDir);
 52  5
                 copyResourceFromJar("upsimple.png", imagesOutputDir);
 53  
 
 54  5
                 copyResourceFromJar("customsorttypes.js", jsOutputDir);
 55  5
                 copyResourceFromJar("popup.js", jsOutputDir);
 56  5
                 copyResourceFromJar("sortabletable.js", jsOutputDir);
 57  5
                 copyResourceFromJar("stringbuilder.js", jsOutputDir);
 58  
 
 59  5
                 copyResourceFromJar("help.html", destinationDir);
 60  5
                 copyResourceFromJar("index.html", destinationDir);
 61  5
         }
 62  
 
 63  
         /**
 64  
          * Copy a file from the jar to a directory on the local machine.
 65  
          *
 66  
          * @param resourceName The name of the file in the jar.  This file
 67  
          *        must exist the same package as this method.
 68  
          * @param directory The directory to copy the jar to.
 69  
          * @throws IOException If the file could not be read from the
 70  
          *         jar or written to the disk.
 71  
          */
 72  
         private static void copyResourceFromJar(String resourceName,
 73  
                         File directory) throws IOException
 74  
         {
 75  
                 int n;
 76  70
                 byte[] buf = new byte[1024];
 77  
 
 78  70
                 InputStream in = null;
 79  70
                 FileOutputStream out = null;
 80  70
                 directory.mkdirs();
 81  
                 try
 82  
                 {
 83  70
                         in = CopyFiles.class.getResourceAsStream(resourceName);
 84  70
                         if (in == null)
 85  0
                                 throw new IllegalArgumentException("Resource " + resourceName
 86  
                                                 + " does not exist in this package.");
 87  70
                         out = new FileOutputStream(new File(directory, resourceName));
 88  250
                         while ((n = in.read(buf, 0, buf.length)) != -1)
 89  
                         {
 90  180
                                 out.write(buf, 0, n);
 91  180
                         }
 92  
                 }
 93  
                 finally
 94  
                 {
 95  70
                         if (in != null)
 96  
                         {
 97  
                                 try
 98  
                                 {
 99  70
                                         in.close();
 100  
                                 }
 101  0
                                 catch (IOException e)
 102  
                                 {
 103  70
                                 }
 104  
                         }
 105  70
                         if (out != null)
 106  
                         {
 107  
                                 try
 108  
                                 {
 109  70
                                         out.close();
 110  
                                 }
 111  0
                                 catch (IOException e)
 112  
                                 {
 113  70
                                 }
 114  0
                         }
 115  0
                 }
 116  70
         }
 117  
 
 118  
 }