KickJava   Java API By Example, From Geeks To Geeks.

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


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 StaticExportCleaner extends DirectoryParser {
35   File contextRoot;
36   Writer writer;
37   private Path protectedPath;
38
39   /**
40    * Creates an instance for the given context root
41    */

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

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

58   public Writer getWriter() {
59     return writer;
60   }
61
62   protected boolean preProcess() {
63     return contextRoot != null && contextRoot.exists();
64   }
65
66   protected boolean preProcessDirectory(File file, Path path) {
67     return !path.equals(protectedPath);
68   }
69
70   protected void postProcessDirectory(File file, Path path) {
71     if (file.delete()) {
72       write("empty " + path + " directory deleted");
73     }
74   }
75
76   protected void processFile(File file, Path path) {
77     if (!path.getFile(contextRoot).exists() ||
78         file.getName().equals(WebSite.CMS_ID_FILE) ||
79         file.getName().equals(WebSite.ADMIN_ID_FILE)) {
80       if (file.delete()) {
81         write(path + " file deleted");
82       }
83     }
84   }
85
86   void write(String JavaDoc message) {
87     if (writer != null) {
88       try {
89         writer.write(message);
90         writer.write('\n');
91         writer.flush();
92       } catch (IOException ex) {}
93     }
94   }
95
96   public Path getProtectedPath() {
97     return protectedPath;
98   }
99
100   public void setProtectedPath(Path protectedPath) {
101     this.protectedPath = protectedPath;
102   }
103 }
104
Popular Tags