Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileSorter.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.filequery;
20 
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;
27 import org.sleuthkit.datamodel.TskCoreException;
28 
32 class FileSorter implements Comparator<ResultFile> {
33 
34  private final List<Comparator<ResultFile>> comparators = new ArrayList<>();
35 
43  FileSorter(SortingMethod method) {
44 
45  // Set up the primary comparators that should applied to the files
46  switch (method) {
47  case BY_DATA_SOURCE:
48  comparators.add(getDataSourceComparator());
49  break;
50  case BY_FILE_SIZE:
51  comparators.add(getFileSizeComparator());
52  break;
53  case BY_FILE_TYPE:
54  comparators.add(getFileTypeComparator());
55  comparators.add(getMIMETypeComparator());
56  break;
57  case BY_FREQUENCY:
58  comparators.add(getFrequencyComparator());
59  break;
60  case BY_KEYWORD_LIST_NAMES:
61  comparators.add(getKeywordListNameComparator());
62  break;
63  case BY_FULL_PATH:
64  comparators.add(getParentPathComparator());
65  break;
66  case BY_FILE_NAME:
67  comparators.add(getFileNameComparator());
68  break;
69  default:
70  // The default comparator will be added afterward
71  break;
72  }
73 
74  // Add the default comparator to the end. This will ensure a consistent sort
75  // order regardless of the order the files were added to the list.
76  comparators.add(getDefaultComparator());
77  }
78 
79  @Override
80  public int compare(ResultFile file1, ResultFile file2) {
81 
82  int result = 0;
83  for (Comparator<ResultFile> comp : comparators) {
84  result = comp.compare(file1, file2);
85  if (result != 0) {
86  return result;
87  }
88  }
89 
90  // The files are the same
91  return result;
92  }
93 
99  private static Comparator<ResultFile> getDataSourceComparator() {
100  return (ResultFile file1, ResultFile file2) -> Long.compare(file1.getFirstInstance().getDataSourceObjectId(), file2.getFirstInstance().getDataSourceObjectId());
101  }
102 
109  private static Comparator<ResultFile> getFileTypeComparator() {
110  return (ResultFile file1, ResultFile file2) -> Integer.compare(file1.getFileType().getRanking(), file2.getFileType().getRanking());
111  }
112 
121  private static Comparator<ResultFile> getKeywordListNameComparator() {
122  return (ResultFile file1, ResultFile file2) -> {
123  // Put empty lists at the bottom
124  if (file1.getKeywordListNames().isEmpty()) {
125  if (file2.getKeywordListNames().isEmpty()) {
126  return 0;
127  }
128  return 1;
129  } else if (file2.getKeywordListNames().isEmpty()) {
130  return -1;
131  }
132 
133  String list1 = String.join(",", file1.getKeywordListNames());
134  String list2 = String.join(",", file2.getKeywordListNames());
135  return compareStrings(list1, list2);
136  };
137  }
138 
145  private static Comparator<ResultFile> getParentPathComparator() {
146 
147  return new Comparator<ResultFile>() {
148  @Override
149  public int compare(ResultFile file1, ResultFile file2) {
150  String file1ParentPath;
151  try {
152  file1ParentPath = file1.getFirstInstance().getParent().getUniquePath();
153  } catch (TskCoreException ingored) {
154  file1ParentPath = file1.getFirstInstance().getParentPath();
155  }
156  String file2ParentPath;
157  try {
158  file2ParentPath = file2.getFirstInstance().getParent().getUniquePath();
159  } catch (TskCoreException ingored) {
160  file2ParentPath = file2.getFirstInstance().getParentPath();
161  }
162  return compareStrings(file1ParentPath.toLowerCase(), file2ParentPath.toLowerCase());
163  }
164  };
165  }
166 
173  private static Comparator<ResultFile> getFrequencyComparator() {
174  return (ResultFile file1, ResultFile file2) -> Integer.compare(file1.getFrequency().getRanking(), file2.getFrequency().getRanking());
175  }
176 
183  private static Comparator<ResultFile> getMIMETypeComparator() {
184  return (ResultFile file1, ResultFile file2) -> compareStrings(file1.getFirstInstance().getMIMEType(), file2.getFirstInstance().getMIMEType());
185  }
186 
192  private static Comparator<ResultFile> getFileSizeComparator() {
193  return (ResultFile file1, ResultFile file2) -> -1 * Long.compare(file1.getFirstInstance().getSize(), file2.getFirstInstance().getSize()) // Sort large to small
194  ;
195  }
196 
202  private static Comparator<ResultFile> getFileNameComparator() {
203  return (ResultFile file1, ResultFile file2) -> compareStrings(file1.getFirstInstance().getName().toLowerCase(), file2.getFirstInstance().getName().toLowerCase());
204  }
205 
214  private static Comparator<ResultFile> getDefaultComparator() {
215  return (ResultFile file1, ResultFile file2) -> {
216  // Compare file names and then object ID (to ensure a consistent sort)
217  int result = getFileNameComparator().compare(file1, file2);
218  if (result == 0) {
219  return Long.compare(file1.getFirstInstance().getId(), file2.getFirstInstance().getId());
220  }
221  return result;
222  };
223  }
224 
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);
237  }
238 
242  @NbBundle.Messages({
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"})
250  enum SortingMethod {
251  BY_FILE_NAME(new ArrayList<>(),
252  Bundle.FileSorter_SortingMethod_filename_displayName()), // Sort alphabetically by file name
253  BY_DATA_SOURCE(new ArrayList<>(),
254  Bundle.FileSorter_SortingMethod_datasource_displayName()), // Sort in increasing order of data source ID
255  BY_FILE_SIZE(new ArrayList<>(),
256  Bundle.FileSorter_SortingMethod_filesize_displayName()), // Sort in decreasing order of size
257  BY_FILE_TYPE(Arrays.asList(new FileSearch.FileTypeAttribute()),
258  Bundle.FileSorter_SortingMethod_filetype_displayName()), // Sort in order of file type (defined in FileType enum), with secondary sort on MIME type
259  BY_FREQUENCY(Arrays.asList(new FileSearch.FrequencyAttribute()),
260  Bundle.FileSorter_SortingMethod_frequency_displayName()), // Sort by decreasing rarity in the central repository
261  BY_KEYWORD_LIST_NAMES(Arrays.asList(new FileSearch.KeywordListAttribute()),
262  Bundle.FileSorter_SortingMethod_keywordlist_displayName()), // Sort alphabetically by list of keyword list names found
263  BY_FULL_PATH(new ArrayList<>(),
264  Bundle.FileSorter_SortingMethod_fullPath_displayName()); // Sort alphabetically by path
265 
266  private final String displayName;
267  private final List<FileSearch.AttributeType> requiredAttributes;
268 
269  SortingMethod(List<FileSearch.AttributeType> attributes, String displayName) {
270  this.requiredAttributes = attributes;
271  this.displayName = displayName;
272  }
273 
274  @Override
275  public String toString() {
276  return displayName;
277  }
278 
279  List<FileSearch.AttributeType> getRequiredAttributes() {
280  return Collections.unmodifiableList(requiredAttributes);
281  }
282 
288  static List<SortingMethod> getOptionsForOrdering() {
289  return Arrays.asList(BY_FILE_SIZE, BY_FULL_PATH, BY_FILE_NAME, BY_DATA_SOURCE);
290  }
291  }
292 }

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.