19 package org.sleuthkit.autopsy.filequery;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.List;
26 import org.openide.util.NbBundle;
32 class FileSorter
implements Comparator<ResultFile> {
34 private final List<Comparator<ResultFile>> comparators =
new ArrayList<>();
43 FileSorter(SortingMethod method) {
48 comparators.add(getDataSourceComparator());
51 comparators.add(getFileSizeComparator());
54 comparators.add(getFileTypeComparator());
55 comparators.add(getMIMETypeComparator());
58 comparators.add(getFrequencyComparator());
60 case BY_KEYWORD_LIST_NAMES:
61 comparators.add(getKeywordListNameComparator());
64 comparators.add(getParentPathComparator());
67 comparators.add(getFileNameComparator());
76 comparators.add(getDefaultComparator());
80 public int compare(ResultFile file1, ResultFile file2) {
83 for (Comparator<ResultFile> comp : comparators) {
84 result = comp.compare(file1, file2);
99 private static Comparator<ResultFile> getDataSourceComparator() {
100 return (ResultFile file1, ResultFile file2) -> Long.compare(file1.getFirstInstance().getDataSourceObjectId(), file2.getFirstInstance().getDataSourceObjectId());
109 private static Comparator<ResultFile> getFileTypeComparator() {
110 return (ResultFile file1, ResultFile file2) -> Integer.compare(file1.getFileType().getRanking(), file2.getFileType().getRanking());
121 private static Comparator<ResultFile> getKeywordListNameComparator() {
122 return (ResultFile file1, ResultFile file2) -> {
124 if (file1.getKeywordListNames().isEmpty()) {
125 if (file2.getKeywordListNames().isEmpty()) {
129 }
else if (file2.getKeywordListNames().isEmpty()) {
133 String list1 = String.join(
",", file1.getKeywordListNames());
134 String list2 = String.join(
",", file2.getKeywordListNames());
135 return compareStrings(list1, list2);
145 private static Comparator<ResultFile> getParentPathComparator() {
147 return new Comparator<ResultFile>() {
149 public int compare(ResultFile file1, ResultFile file2) {
150 String file1ParentPath;
152 file1ParentPath = file1.getFirstInstance().getParent().getUniquePath();
153 }
catch (TskCoreException ingored) {
154 file1ParentPath = file1.getFirstInstance().getParentPath();
156 String file2ParentPath;
158 file2ParentPath = file2.getFirstInstance().getParent().getUniquePath();
159 }
catch (TskCoreException ingored) {
160 file2ParentPath = file2.getFirstInstance().getParentPath();
162 return compareStrings(file1ParentPath.toLowerCase(), file2ParentPath.toLowerCase());
173 private static Comparator<ResultFile> getFrequencyComparator() {
174 return (ResultFile file1, ResultFile file2) -> Integer.compare(file1.getFrequency().getRanking(), file2.getFrequency().getRanking());
183 private static Comparator<ResultFile> getMIMETypeComparator() {
184 return (ResultFile file1, ResultFile file2) -> compareStrings(file1.getFirstInstance().getMIMEType(), file2.getFirstInstance().getMIMEType());
192 private static Comparator<ResultFile> getFileSizeComparator() {
193 return (ResultFile file1, ResultFile file2) -> -1 * Long.compare(file1.getFirstInstance().getSize(), file2.getFirstInstance().getSize())
202 private static Comparator<ResultFile> getFileNameComparator() {
203 return (ResultFile file1, ResultFile file2) -> compareStrings(file1.getFirstInstance().getName().toLowerCase(), file2.getFirstInstance().getName().toLowerCase());
214 private static Comparator<ResultFile> getDefaultComparator() {
215 return (ResultFile file1, ResultFile file2) -> {
217 int result = getFileNameComparator().compare(file1, file2);
219 return Long.compare(file1.getFirstInstance().getId(), file2.getFirstInstance().getId());
233 private static int compareStrings(String s1, String s2) {
234 String string1 = s1 == null ?
"" : s1;
235 String string2 = s2 == null ?
"" : s2;
236 return string1.compareTo(string2);
243 "FileSorter.SortingMethod.datasource.displayName=Data Source",
244 "FileSorter.SortingMethod.filename.displayName=File Name",
245 "FileSorter.SortingMethod.filesize.displayName=File Size",
246 "FileSorter.SortingMethod.filetype.displayName=File Type",
247 "FileSorter.SortingMethod.frequency.displayName=Central Repo Frequency",
248 "FileSorter.SortingMethod.keywordlist.displayName=Keyword List Names",
249 "FileSorter.SortingMethod.fullPath.displayName=Full Path"})
251 BY_FILE_NAME(
new ArrayList<>(),
252 Bundle.FileSorter_SortingMethod_filename_displayName()),
253 BY_DATA_SOURCE(
new ArrayList<>(),
254 Bundle.FileSorter_SortingMethod_datasource_displayName()),
255 BY_FILE_SIZE(
new ArrayList<>(),
256 Bundle.FileSorter_SortingMethod_filesize_displayName()),
257 BY_FILE_TYPE(Arrays.asList(
new FileSearch.FileTypeAttribute()),
258 Bundle.FileSorter_SortingMethod_filetype_displayName()),
259 BY_FREQUENCY(Arrays.asList(
new FileSearch.FrequencyAttribute()),
260 Bundle.FileSorter_SortingMethod_frequency_displayName()),
261 BY_KEYWORD_LIST_NAMES(Arrays.asList(
new FileSearch.KeywordListAttribute()),
262 Bundle.FileSorter_SortingMethod_keywordlist_displayName()),
263 BY_FULL_PATH(
new ArrayList<>(),
264 Bundle.FileSorter_SortingMethod_fullPath_displayName());
266 private final String displayName;
267 private final List<FileSearch.AttributeType> requiredAttributes;
269 SortingMethod(List<FileSearch.AttributeType> attributes, String displayName) {
270 this.requiredAttributes = attributes;
271 this.displayName = displayName;
275 public String toString() {
279 List<FileSearch.AttributeType> getRequiredAttributes() {
280 return Collections.unmodifiableList(requiredAttributes);
288 static List<SortingMethod> getOptionsForOrdering() {
289 return Arrays.asList(BY_FILE_SIZE, BY_FULL_PATH, BY_FILE_NAME, BY_DATA_SOURCE);