KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > reporting > html > files > CopyFiles


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 JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29
30 public abstract class CopyFiles
31 {
32
33     public static void copy(File JavaDoc destinationDir) throws IOException JavaDoc
34     {
35         File JavaDoc cssOutputDir = new File JavaDoc(destinationDir, "css");
36         File JavaDoc imagesOutputDir = new File JavaDoc(destinationDir, "images");
37         File JavaDoc jsOutputDir = new File JavaDoc(destinationDir, "js");
38
39         destinationDir.mkdirs();
40         cssOutputDir.mkdir();
41         imagesOutputDir.mkdir();
42         jsOutputDir.mkdir();
43
44         copyResourceFromJar("help.css", cssOutputDir);
45         copyResourceFromJar("main.css", cssOutputDir);
46         copyResourceFromJar("sortabletable.css", cssOutputDir);
47         copyResourceFromJar("source-viewer.css", cssOutputDir);
48         copyResourceFromJar("tooltip.css", cssOutputDir);
49
50         copyResourceFromJar("blank.png", imagesOutputDir);
51         copyResourceFromJar("downsimple.png", imagesOutputDir);
52         copyResourceFromJar("upsimple.png", imagesOutputDir);
53
54         copyResourceFromJar("customsorttypes.js", jsOutputDir);
55         copyResourceFromJar("popup.js", jsOutputDir);
56         copyResourceFromJar("sortabletable.js", jsOutputDir);
57         copyResourceFromJar("stringbuilder.js", jsOutputDir);
58
59         copyResourceFromJar("help.html", destinationDir);
60         copyResourceFromJar("index.html", destinationDir);
61     }
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 JavaDoc resourceName,
73             File JavaDoc directory) throws IOException JavaDoc
74     {
75         int n;
76         byte[] buf = new byte[1024];
77
78         InputStream JavaDoc in = null;
79         FileOutputStream JavaDoc out = null;
80         directory.mkdirs();
81         try
82         {
83             in = CopyFiles.class.getResourceAsStream(resourceName);
84             if (in == null)
85                 throw new IllegalArgumentException JavaDoc("Resource " + resourceName
86                         + " does not exist in this package.");
87             out = new FileOutputStream JavaDoc(new File JavaDoc(directory, resourceName));
88             while ((n = in.read(buf, 0, buf.length)) != -1)
89             {
90                 out.write(buf, 0, n);
91             }
92         }
93         finally
94         {
95             if (in != null)
96             {
97                 try
98                 {
99                     in.close();
100                 }
101                 catch (IOException JavaDoc e)
102                 {
103                 }
104             }
105             if (out != null)
106             {
107                 try
108                 {
109                     out.close();
110                 }
111                 catch (IOException JavaDoc e)
112                 {
113                 }
114             }
115         }
116     }
117
118 }
119
Popular Tags