KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > extra > StaticExportCopier


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2005 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.extra;
24
25 import java.io.*;
26 import org.meshcms.core.*;
27 import org.meshcms.util.*;
28
29 /**
30  * Cleans the directory used to export static files. This is done by removing
31  * all files that are not available in the dynamic version of the site. Empty
32  * directories are also removed. This class is used by {@link StaticExporter}.
33  */

34 public class StaticExportCopier extends DirectoryParser {
35   File destinationRoot;
36   private boolean checkDates;
37   private boolean mkDirs;
38   Writer writer;
39
40   /**
41    * Creates an instance for the given context root
42    */

43   public StaticExportCopier(File destinationRoot) {
44     super();
45     this.destinationRoot = destinationRoot;
46     setRecursive(true);
47   }
48
49   /**
50    * Sets the writer for logging (usually the writer of the web page).
51    */

52   public void setWriter(Writer writer) {
53     this.writer = writer;
54   }
55
56   /**
57    * Returns the writer (if any).
58    */

59   public Writer getWriter() {
60     return writer;
61   }
62
63   protected boolean preProcessDirectory(File file, Path path) {
64     File destDir = path.getFile(destinationRoot);
65     
66     if (mkDirs) {
67       destDir.mkdirs();
68     }
69     
70     return destDir.isDirectory();
71   }
72
73   protected void processFile(File file, Path path) {
74     if (!(FileTypes.isPage(file.getName()) ||
75         file.getName().equals(WebSite.CMS_ID_FILE) ||
76         file.getName().equals(WebSite.ADMIN_ID_FILE))) {
77       File copy = path.getFile(destinationRoot);
78       
79       if (!(checkDates && copy.exists() &&
80           file.lastModified() <= copy.lastModified())) {
81         try {
82           Utils.copyFile(file, copy, true, false);
83           write(path + " file copied");
84         } catch (IOException ex) {
85           ex.printStackTrace();
86         }
87       }
88     }
89   }
90
91   void write(String JavaDoc message) {
92     if (writer != null) {
93       try {
94         writer.write(message);
95         writer.write('\n');
96         writer.flush();
97       } catch (IOException ex) {}
98     }
99   }
100
101   public boolean isCheckDates() {
102     return checkDates;
103   }
104
105   public void setCheckDates(boolean checkDates) {
106     this.checkDates = checkDates;
107   }
108
109   public boolean isMkDirs() {
110     return mkDirs;
111   }
112
113   public void setMkDirs(boolean mkDirs) {
114     this.mkDirs = mkDirs;
115   }
116 }
117
Popular Tags