001/*
002 * Cobertura - http://cobertura.sourceforge.net/
003 *
004 * Copyright (C) 2003 jcoverage ltd.
005 * Copyright (C) 2005 Mark Doliner
006 *
007 * Cobertura is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published
009 * by the Free Software Foundation; either version 2 of the License,
010 * or (at your option) any later version.
011 *
012 * Cobertura is distributed in the hope that it will be useful, but
013 * WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with Cobertura; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020 * USA
021 */
022
023package net.sourceforge.cobertura.reporting.html.files;
024
025import java.io.File;
026import java.io.FileOutputStream;
027import java.io.IOException;
028import java.io.InputStream;
029
030public abstract class CopyFiles
031{
032
033        public static void copy(File destinationDir) throws IOException
034        {
035                File cssOutputDir = new File(destinationDir, "css");
036                File imagesOutputDir = new File(destinationDir, "images");
037                File jsOutputDir = new File(destinationDir, "js");
038
039                destinationDir.mkdirs();
040                cssOutputDir.mkdir();
041                imagesOutputDir.mkdir();
042                jsOutputDir.mkdir();
043
044                copyResourceFromJar("help.css", cssOutputDir);
045                copyResourceFromJar("main.css", cssOutputDir);
046                copyResourceFromJar("sortabletable.css", cssOutputDir);
047                copyResourceFromJar("source-viewer.css", cssOutputDir);
048                copyResourceFromJar("tooltip.css", cssOutputDir);
049
050                copyResourceFromJar("blank.png", imagesOutputDir);
051                copyResourceFromJar("downsimple.png", imagesOutputDir);
052                copyResourceFromJar("upsimple.png", imagesOutputDir);
053
054                copyResourceFromJar("customsorttypes.js", jsOutputDir);
055                copyResourceFromJar("popup.js", jsOutputDir);
056                copyResourceFromJar("sortabletable.js", jsOutputDir);
057                copyResourceFromJar("stringbuilder.js", jsOutputDir);
058
059                copyResourceFromJar("help.html", destinationDir);
060                copyResourceFromJar("index.html", destinationDir);
061        }
062
063        /**
064         * Copy a file from the jar to a directory on the local machine.
065         *
066         * @param resourceName The name of the file in the jar.  This file
067         *        must exist the same package as this method.
068         * @param directory The directory to copy the jar to.
069         * @throws IOException If the file could not be read from the
070         *         jar or written to the disk.
071         */
072        private static void copyResourceFromJar(String resourceName,
073                        File directory) throws IOException
074        {
075                int n;
076                byte[] buf = new byte[1024];
077
078                InputStream in = null;
079                FileOutputStream out = null;
080                directory.mkdirs();
081                try
082                {
083                        in = CopyFiles.class.getResourceAsStream(resourceName);
084                        if (in == null)
085                                throw new IllegalArgumentException("Resource " + resourceName
086                                                + " does not exist in this package.");
087                        out = new FileOutputStream(new File(directory, resourceName));
088                        while ((n = in.read(buf, 0, buf.length)) != -1)
089                        {
090                                out.write(buf, 0, n);
091                        }
092                }
093                finally
094                {
095                        if (in != null)
096                        {
097                                try
098                                {
099                                        in.close();
100                                }
101                                catch (IOException e)
102                                {
103                                }
104                        }
105                        if (out != null)
106                        {
107                                try
108                                {
109                                        out.close();
110                                }
111                                catch (IOException e)
112                                {
113                                }
114                        }
115                }
116        }
117
118}