001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.fsguide.visitors;
017:
018: import java.io.BufferedReader;
019: import java.io.File;
020: import java.io.FileReader;
021: import java.io.FileWriter;
022: import java.io.IOException;
023: import java.io.PrintWriter;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.directwebremoting.fsguide.Visitor;
028: import org.directwebremoting.util.LocalUtil;
029:
030: /**
031: *
032: */
033: public class DeepCopyVisitor implements Visitor {
034: /**
035: *
036: */
037: public DeepCopyVisitor(File origRoot, File destRoot) {
038: this .origRoot = origRoot;
039: this .destRoot = destRoot;
040: }
041:
042: /* (non-Javadoc)
043: * @see com.barclaycard.fsguide.Visitor#visitFile(java.io.File)
044: */
045: public void visitFile(File inputFile) {
046: File destFile = getDestFile(inputFile);
047:
048: BufferedReader in = null;
049: PrintWriter out = null;
050:
051: try {
052: in = new BufferedReader(new FileReader(inputFile));
053: out = new PrintWriter(new FileWriter(destFile));
054:
055: while (true) {
056: String line = in.readLine();
057:
058: if (line == null) {
059: break;
060: }
061:
062: out.println(map(line));
063: }
064:
065: log.info("Writing to file: " + destFile.getAbsolutePath());
066: } catch (IOException ex) {
067: throw new RuntimeException(ex);
068: } finally {
069: LocalUtil.close(in);
070: LocalUtil.close(out);
071: }
072: }
073:
074: /* (non-Javadoc)
075: * @see com.barclaycard.fsguide.Visitor#visitDirectory(java.io.File)
076: */
077: public boolean visitDirectory(File file) {
078: File destFile = getDestFile(file);
079:
080: destFile.mkdirs();
081: log.info("Created dir: " + destFile.getAbsolutePath());
082:
083: return true;
084: }
085:
086: /**
087: *
088: */
089: private File getDestFile(File file) {
090: String currentPath = file.getAbsolutePath();
091: String origPath = origRoot.getAbsolutePath();
092:
093: if (!currentPath.startsWith(origPath)) {
094: throw new IllegalArgumentException("'" + currentPath
095: + "' is not rooted in '" + origPath + "'");
096: }
097:
098: String suffix = currentPath.substring(origPath.length());
099: String prefix = destRoot.getAbsolutePath();
100: String full = prefix + suffix;
101: full = map(full);
102:
103: File destFile = new File(full);
104: return destFile;
105: }
106:
107: /**
108: *
109: */
110: protected String map(String name) {
111: return name;
112: }
113:
114: private File origRoot;
115:
116: private File destRoot;
117:
118: /**
119: * The log stream
120: */
121: private static final Log log = LogFactory
122: .getLog(DeepCopyVisitor.class);
123: }
|