001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.utils.misc;
009:
010: //base classes
011: import java.io.File;
012: import java.io.FileReader;
013: import java.io.LineNumberReader;
014: import java.util.ArrayList;
015: import java.util.Collections;
016: import java.util.HashMap;
017:
018: //project specific classes
019:
020: //other classes
021:
022: public class MergeFileToDirectory {
023:
024: public static void main(String args[]) throws Exception {
025: //
026: String mergeFileName = args[0];
027: File sourceDir = new File(args[1]);
028: File destDir = new File(args[2]);
029: //
030: String mergeFileContent = MiscHelper.readTextFile(new File(
031: mergeFileName));
032: //
033: //System.out.println(
034: // "mergeFileName = " + mergeFileName);
035: //System.out.println(
036: // "mergeFileContext.length() = " + mergeFileContent.length());
037: performMerge(mergeFileContent, sourceDir, destDir);
038: //
039: //String startDirName = args[0];
040: //int totalLines = processFile(new File(startDirName));
041: //System.out.println(" TOTAL LINES = " + totalLines);
042: }
043:
044: private final static void performMerge(String inContent,
045: File inSourceDir, File inDestDir) {
046: //
047: if (inSourceDir.isDirectory()) {
048: File subFiles[] = inSourceDir.listFiles();
049: //
050: for (int i = 0; i < subFiles.length; i++) {
051: File nextFile = subFiles[i];
052: if (nextFile.isDirectory()) {
053: File nextSubDestDir = new File(inDestDir, nextFile
054: .getName());
055: if (checkDir(nextFile)) {
056: nextSubDestDir.mkdir();
057: performMerge(inContent, nextFile,
058: nextSubDestDir);
059: }
060: }
061: }
062: //
063: for (int i = 0; i < subFiles.length; i++) {
064: File nextFile = subFiles[i];
065: if (nextFile.isFile()) {
066: File nextSubDestFile = new File(inDestDir, nextFile
067: .getName());
068: //
069: String nextSourceContent = MiscHelper
070: .readTextFile(nextFile);
071: if (checkFile(nextFile)) {
072: if (!checkBlockHeader(nextFile)) {
073: MiscHelper.writeTextFile(nextSubDestFile,
074: inContent + nextSourceContent);
075: } else {
076: MiscHelper.writeTextFile(nextSubDestFile,
077: nextSourceContent);
078: }
079: }
080: }
081: }
082: } else {
083: //
084: String nextSourceContent = MiscHelper
085: .readTextFile(inSourceDir);
086: //
087: File destFile = new File(inDestDir, inSourceDir.getName());
088: //
089: if (checkFile(inSourceDir)) {
090: if (!checkBlockHeader(inSourceDir)) {
091: MiscHelper.writeTextFile(destFile, inContent
092: + nextSourceContent);
093: } else {
094: MiscHelper.writeTextFile(destFile,
095: nextSourceContent);
096: }
097: }
098: }
099: }
100:
101: private final static boolean checkBlockHeader(File inFile) {
102:
103: boolean outValue = false;
104:
105: String fileName = inFile.getName();
106: //
107: if (fileName.endsWith(".bat")) {
108: //
109: outValue = true;
110: }
111:
112: return outValue;
113: }
114:
115: private final static boolean checkFile(File inFile) {
116:
117: boolean outValue = false;
118:
119: String fileName = inFile.getName();
120: //
121: if (fileName.endsWith(".txt") || fileName.endsWith(".xml")
122: || fileName.endsWith(".java")
123: || fileName.endsWith(".bat")) {
124: //
125: outValue = true;
126: }
127:
128: return outValue;
129: }
130:
131: private final static boolean checkDir(File inDir) {
132: //
133: boolean outValue = false;
134:
135: String dirName = inDir.getName();
136: if (!dirName.equals("CVS")) {
137: outValue = true;
138: }
139:
140: return outValue;
141: }
142: }
|