001: /*
002: * MeshCMS - A simple CMS based on SiteMesh
003: * Copyright (C) 2004-2005 Luciano Vernaschi
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: * You can contact the author at http://www.cromoteca.com
020: * and at info@cromoteca.com
021: */
022:
023: package org.meshcms.extra;
024:
025: import java.io.*;
026: import org.meshcms.core.*;
027: import org.meshcms.util.*;
028:
029: /**
030: * Cleans the directory used to export static files. This is done by removing
031: * all files that are not available in the dynamic version of the site. Empty
032: * directories are also removed. This class is used by {@link StaticExporter}.
033: */
034: public class StaticExportCopier extends DirectoryParser {
035: File destinationRoot;
036: private boolean checkDates;
037: private boolean mkDirs;
038: Writer writer;
039:
040: /**
041: * Creates an instance for the given context root
042: */
043: public StaticExportCopier(File destinationRoot) {
044: super ();
045: this .destinationRoot = destinationRoot;
046: setRecursive(true);
047: }
048:
049: /**
050: * Sets the writer for logging (usually the writer of the web page).
051: */
052: public void setWriter(Writer writer) {
053: this .writer = writer;
054: }
055:
056: /**
057: * Returns the writer (if any).
058: */
059: public Writer getWriter() {
060: return writer;
061: }
062:
063: protected boolean preProcessDirectory(File file, Path path) {
064: File destDir = path.getFile(destinationRoot);
065:
066: if (mkDirs) {
067: destDir.mkdirs();
068: }
069:
070: return destDir.isDirectory();
071: }
072:
073: protected void processFile(File file, Path path) {
074: if (!(FileTypes.isPage(file.getName())
075: || file.getName().equals(WebSite.CMS_ID_FILE) || file
076: .getName().equals(WebSite.ADMIN_ID_FILE))) {
077: File copy = path.getFile(destinationRoot);
078:
079: if (!(checkDates && copy.exists() && file.lastModified() <= copy
080: .lastModified())) {
081: try {
082: Utils.copyFile(file, copy, true, false);
083: write(path + " file copied");
084: } catch (IOException ex) {
085: ex.printStackTrace();
086: }
087: }
088: }
089: }
090:
091: void write(String message) {
092: if (writer != null) {
093: try {
094: writer.write(message);
095: writer.write('\n');
096: writer.flush();
097: } catch (IOException ex) {
098: }
099: }
100: }
101:
102: public boolean isCheckDates() {
103: return checkDates;
104: }
105:
106: public void setCheckDates(boolean checkDates) {
107: this .checkDates = checkDates;
108: }
109:
110: public boolean isMkDirs() {
111: return mkDirs;
112: }
113:
114: public void setMkDirs(boolean mkDirs) {
115: this.mkDirs = mkDirs;
116: }
117: }
|