Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Provide a CSV output for duplicates #14
- Loading branch information
Showing
11 changed files
with
332 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/org/fim/internal/DuplicateOutputGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* This file is part of Fim - File Integrity Manager | ||
* | ||
* Copyright (C) 2017 Etienne Vrignaud | ||
* | ||
* Fim is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Fim is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Fim. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package org.fim.internal; | ||
|
||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVPrinter; | ||
import org.fim.model.Context; | ||
import org.fim.model.DuplicateResult; | ||
import org.fim.model.DuplicateSet; | ||
import org.fim.model.FileState; | ||
import org.fim.model.output.DuplicatedFile; | ||
import org.fim.model.output.DuplicatedFiles; | ||
import org.fim.util.JsonIO; | ||
import org.fim.util.Logger; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DuplicateOutputGenerator { | ||
private Context context; | ||
|
||
public DuplicateOutputGenerator(Context context) { | ||
this.context = context; | ||
} | ||
|
||
public void generate(DuplicateResult duplicateResult) { | ||
List<DuplicatedFiles> duplicates = generateDuplicatedFiles(duplicateResult); | ||
switch (context.getOutputType()) { | ||
case csv: | ||
generateCSV(duplicates); | ||
break; | ||
|
||
case json: | ||
generateJson(duplicates); | ||
break; | ||
} | ||
} | ||
|
||
private List<DuplicatedFiles> generateDuplicatedFiles(DuplicateResult duplicateResult) { | ||
List<DuplicatedFiles> duplicateList = new ArrayList<>(); | ||
for (DuplicateSet duplicateSet : duplicateResult.getDuplicateSets()) { | ||
DuplicatedFiles duplicatedFiles = new DuplicatedFiles(); | ||
duplicatedFiles.setWastedSpace(duplicateSet.getWastedSpace()); | ||
for (FileState fileState : duplicateSet.getDuplicatedFiles()) { | ||
DuplicatedFile file = new DuplicatedFile(); | ||
file.setName(fileState.getFileName()); | ||
file.setLength(fileState.getFileLength()); | ||
file.setPath(getPath(fileState.getFileName())); | ||
file.setType(getExtension(fileState.getFileName())); | ||
duplicatedFiles.getFileList().add(file); | ||
} | ||
duplicateList.add(duplicatedFiles); | ||
} | ||
return duplicateList; | ||
} | ||
|
||
private String getPath(String fileName) { | ||
int index = fileName.lastIndexOf("/"); | ||
if (index == -1) { | ||
return ""; | ||
} | ||
return fileName.substring(0, index); | ||
} | ||
|
||
private String getExtension(String fileName) { | ||
int index = fileName.lastIndexOf("."); | ||
if (index == -1) { | ||
return ""; | ||
} | ||
return fileName.substring(index + 1).toLowerCase(); | ||
} | ||
|
||
private void generateCSV(List<DuplicatedFiles> duplicates) { | ||
CSVFormat format = CSVFormat.DEFAULT.withHeader("SetIndex", "FileIndex", "WastedSpace", "FilePath", "FileName", "FileLength", "FileType"); | ||
try (CSVPrinter csvPrinter = new CSVPrinter(Logger.out, format)) { | ||
int setIndex = 0; | ||
for (DuplicatedFiles files : duplicates) { | ||
setIndex++; | ||
int fileIndex = 0; | ||
for (DuplicatedFile file : files.getFileList()) { | ||
fileIndex++; | ||
csvPrinter.printRecord(setIndex, fileIndex, files.getWastedSpace(), file.getPath(), file.getName(), file.getLength(), file.getType()); | ||
} | ||
} | ||
csvPrinter.flush(); | ||
} catch (IOException ex) { | ||
Logger.error("Error displaying duplicates in CSV format", ex, context.isDisplayStackTrace()); | ||
} | ||
} | ||
|
||
private void generateJson(List<DuplicatedFiles> duplicates) { | ||
JsonIO jsonIO = new JsonIO(); | ||
try { | ||
jsonIO.getObjectWriter().writeValue(Logger.out, duplicates); | ||
Logger.out.println(""); | ||
} catch (IOException ex) { | ||
Logger.error("Error displaying duplicates in JSON format", ex, context.isDisplayStackTrace()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* This file is part of Fim - File Integrity Manager | ||
* | ||
* Copyright (C) 2017 Etienne Vrignaud | ||
* | ||
* Fim is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Fim is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Fim. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package org.fim.model; | ||
|
||
public enum OutputType { | ||
human, | ||
csv, | ||
json | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* This file is part of Fim - File Integrity Manager | ||
* | ||
* Copyright (C) 2017 Etienne Vrignaud | ||
* | ||
* Fim is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Fim is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Fim. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package org.fim.model.output; | ||
|
||
public class DuplicatedFile { | ||
private String path; | ||
private String name; | ||
private long length; | ||
private String type; | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public void setPath(String path) { | ||
this.path = path; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public long getLength() { | ||
return length; | ||
} | ||
|
||
public void setLength(long length) { | ||
this.length = length; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
} |
Oops, something went wrong.