KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > util > DirectoryCopier


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.util;
24
25 import java.io.*;
26
27 public class DirectoryCopier extends DirectoryParser {
28   private File newDir;
29   private boolean overwriteDir;
30   private boolean overwriteFiles;
31   private boolean setLastModified;
32   private boolean result;
33
34   public DirectoryCopier(File dir, File newDir, boolean overwriteDir,
35       boolean overwriteFiles, boolean setLastModified) {
36     setInitialDir(dir);
37     this.newDir = newDir;
38     this.overwriteDir = overwriteDir;
39     this.overwriteFiles = overwriteFiles;
40     this.setLastModified = setLastModified;
41     setRecursive(true);
42     setProcessStartDir(true);
43     result = true;
44   }
45
46   protected boolean preProcess() {
47     return overwriteDir || !newDir.exists();
48   }
49
50   protected boolean preProcessDirectory(File file, Path path) {
51     File dir = path.getFile(newDir);
52     dir.mkdirs();
53
54     if (!dir.isDirectory()) {
55       return result = false;
56     }
57
58     return true;
59   }
60
61   protected void processFile(File file, Path path) {
62     try {
63       Utils.copyFile(file, path.getFile(newDir), overwriteFiles,
64           setLastModified);
65     } catch (IOException ex) {
66       result = false;
67       ex.printStackTrace();
68     }
69   }
70
71   public boolean getResult() {
72     return result;
73   }
74 }
75
Popular Tags