001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
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:
017: package org.romaframework.wizard.helper;
018:
019: import java.io.BufferedInputStream;
020: import java.io.BufferedOutputStream;
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028:
029: import org.romaframework.core.Utility;
030: import org.romaframework.core.command.CommandIO;
031: import org.romaframework.core.io.virtualfile.VirtualFile;
032: import org.romaframework.core.io.virtualfile.VirtualFileFactory;
033: import org.romaframework.wizard.WizardConstants;
034:
035: /**
036: * Helper class to management of files and directories.
037: *
038: * @author Luca Garulli (luca.garulli@assetdata.it)
039: */
040: public class FileManagementHelper {
041:
042: private static String[] UNFILTERED_EXTENSIONS = { ".jar", ".zip",
043: ".exe", ".png", ".gif", ".jpg" };
044:
045: public static boolean filterTemplateFile(String iFileName) {
046: String lowercaseFileName = iFileName.toLowerCase();
047: for (String ext : UNFILTERED_EXTENSIONS) {
048: if (lowercaseFileName.endsWith(ext))
049: return false;
050: }
051:
052: return true;
053: }
054:
055: public static int executeCopy(CommandIO iContext,
056: VirtualFile sourceDir, String iDestinationPath,
057: boolean iOverwrite) {
058: if (sourceDir == null)
059: return -1;
060:
061: File destDir = new File(iDestinationPath);
062:
063: try {
064: iContext.getOutput().println(
065: "\n[roma] Copying '" + sourceDir + "' -> '"
066: + destDir + "'...");
067:
068: int fileCopied = copyDirectory(iContext, sourceDir,
069: destDir, iOverwrite);
070:
071: iContext.getOutput().println(
072: "\n[roma] " + fileCopied + " file(s) copied");
073:
074: return fileCopied;
075: } catch (IOException e) {
076: iContext.getError().println("Error on copying resources:");
077: e.printStackTrace(iContext.getError());
078: return -1;
079: }
080: }
081:
082: public static String getVirtualPath(String iPath) {
083: iPath = Utility.removeLastSeparatorIfAny(iPath);
084:
085: if (!new File(iPath).exists()) {
086: iPath += WizardConstants.MODULE_PACKAGE_EXT;
087:
088: if (!new File(iPath).exists())
089: return null;
090:
091: iPath = "jar:file:" + iPath + "!";
092: } else
093: iPath = Utility.FILE_PREFIX + iPath;
094: return iPath;
095: }
096:
097: public static VirtualFile getVirtualFile(String iSourcePath) {
098: URL url;
099: try {
100: url = new URL(iSourcePath);
101: } catch (MalformedURLException e) {
102: return null;
103: }
104:
105: VirtualFile sourceDir = VirtualFileFactory.getFile(url);
106: if (sourceDir == null || !sourceDir.exists())
107: return null;
108:
109: return sourceDir;
110: }
111:
112: private static int copyDirectory(CommandIO iContext,
113: VirtualFile sourceDir, File iDestPath, boolean iOverwrite)
114: throws IOException {
115: if (!iDestPath.exists())
116: if (!iDestPath.mkdirs()) {
117: return 0;
118: }
119:
120: File newFile;
121: String newName;
122: int fileCopied = 0;
123: for (VirtualFile file : sourceDir.listFiles()) {
124: if (file.isDirectory())
125: fileCopied += copyDirectory(iContext, file, new File(
126: iDestPath.getAbsolutePath() + File.separator
127: + file.getName()), iOverwrite);
128: else {
129: newName = TemplateHelper.resolveValue(iDestPath
130: .getAbsolutePath()
131: + File.separator + file.getName());
132:
133: newFile = new File(newName);
134:
135: if (!iOverwrite && newFile.exists()) {
136: iContext.getError().println(
137: "Error: File " + newFile.getName()
138: + " already exists, continue...");
139: } else {
140: newFile.createNewFile();
141: fileCopied += copyFile(iContext, file, newFile);
142: }
143: }
144: }
145: return fileCopied;
146: }
147:
148: private static int copyFile(CommandIO iContext, VirtualFile file,
149: File iFileDest) {
150: InputStream in = null;
151: OutputStream out = null;
152: try {
153: iContext.getOutput().println(
154: "[roma] - " + file.getName() + " -> "
155: + iFileDest.getParentFile().getName() + "/"
156: + iFileDest.getName());
157:
158: in = new BufferedInputStream(file.getInputStream());
159: out = new BufferedOutputStream(new FileOutputStream(
160: iFileDest));
161:
162: byte[] bufferTemp = new byte[(int) file.length()];
163:
164: // READ SOURCE FILE
165: in.read(bufferTemp);
166:
167: // REPLACE ALL VARIABLES IN FILE
168: byte[] bufferToWrite;
169:
170: if (filterTemplateFile(file.getName())) {
171: String resolvedBuffer = TemplateHelper
172: .resolveValue(new String(bufferTemp));
173: bufferToWrite = resolvedBuffer.getBytes();
174: } else
175: bufferToWrite = bufferTemp;
176:
177: // WRITE DESTINATION FILE
178: out.write(bufferToWrite);
179: return 1;
180: } catch (IOException e) {
181: e.printStackTrace(iContext.getError());
182: } finally {
183: try {
184: if (in != null)
185: in.close();
186: if (out != null)
187: out.close();
188: } catch (IOException e) {
189: }
190: }
191: return 0;
192: }
193: }
|