19 package org.sleuthkit.autopsy.casemodule.services;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.LinkedHashSet;
24 import java.util.List;
25 import java.util.Objects;
27 import java.util.logging.Level;
28 import javax.annotation.concurrent.Immutable;
29 import org.openide.util.NbBundle;
44 final class TagNameDefinition
implements Comparable<TagNameDefinition> {
46 private static final Logger LOGGER = Logger.getLogger(TagNameDefinition.class.getName());
47 @NbBundle.Messages({
"TagNameDefinition.predefTagNames.bookmark.text=Bookmark",
48 "TagNameDefinition.predefTagNames.followUp.text=Follow Up",
49 "TagNameDefinition.predefTagNames.notableItem.text=Notable Item"})
50 private static final String TAGS_SETTINGS_NAME =
"Tags";
51 private static final String TAG_NAMES_SETTING_KEY =
"TagNames";
53 private static final List<String> STANDARD_NOTABLE_TAG_DISPLAY_NAMES = Arrays.asList(Bundle.TagNameDefinition_predefTagNames_notableItem_text(), DhsImageCategory.ONE.getDisplayName(), DhsImageCategory.TWO.getDisplayName(), DhsImageCategory.THREE.getDisplayName());
54 private static final List<String> STANDARD_TAG_DISPLAY_NAMES = Arrays.asList(Bundle.TagNameDefinition_predefTagNames_bookmark_text(), Bundle.TagNameDefinition_predefTagNames_followUp_text(),
55 Bundle.TagNameDefinition_predefTagNames_notableItem_text(), DhsImageCategory.ONE.getDisplayName(),
56 DhsImageCategory.TWO.getDisplayName(), DhsImageCategory.THREE.getDisplayName(),
57 DhsImageCategory.FOUR.getDisplayName(), DhsImageCategory.FIVE.getDisplayName(), DhsImageCategory.ZERO.getDisplayName());
58 private final String displayName;
59 private final String description;
60 private final TagName.HTML_COLOR color;
61 private final TskData.FileKnown knownStatus;
72 TagNameDefinition(String displayName, String description, TagName.HTML_COLOR color, TskData.FileKnown status) {
73 this.displayName = displayName;
74 this.description = description;
76 this.knownStatus = status;
79 static List<String> getStandardTagNames() {
80 return STANDARD_TAG_DISPLAY_NAMES;
88 String getDisplayName() {
97 String getDescription() {
106 TagName.HTML_COLOR getColor() {
115 TskData.FileKnown getKnownStatus() {
131 public int compareTo(TagNameDefinition other) {
132 return this.getDisplayName().toLowerCase().compareTo(other.getDisplayName().toLowerCase());
141 public int hashCode() {
143 hash = 83 * hash + Objects.hashCode(this.displayName);
156 public boolean equals(Object obj) {
157 if (!(obj instanceof TagNameDefinition)) {
160 boolean sameName = this.getDisplayName().equals(((TagNameDefinition) obj).getDisplayName());
161 boolean sameStatus = this.getKnownStatus().equals(((TagNameDefinition) obj).getKnownStatus());
162 return sameName && sameStatus;
171 public String toString() {
179 private String toSettingsFormat() {
180 return displayName +
"," + description +
"," + color.name() +
"," + knownStatus.toString();
183 TagName saveToCase(SleuthkitCase caseDb) {
184 TagName tagName = null;
186 tagName = caseDb.addOrUpdateTagName(displayName, description, color, knownStatus);
187 }
catch (TskCoreException ex) {
188 LOGGER.log(Level.SEVERE,
"Error updating non-file object ", ex);
199 static synchronized Set<TagNameDefinition> getTagNameDefinitions() {
200 Set<TagNameDefinition> tagNames =
new LinkedHashSet<>();
202 Set<String> standardTags =
new LinkedHashSet<>(STANDARD_TAG_DISPLAY_NAMES);
203 String setting = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY);
204 if (null != setting && !setting.isEmpty()) {
205 List<String> tagNameTuples = Arrays.asList(setting.split(
";"));
206 int numberOfAttributes = 0;
207 if (tagNameTuples.size() > 0) {
209 numberOfAttributes = tagNameTuples.get(0).split(
",").length;
211 if (numberOfAttributes == 3) {
213 tagNames.addAll(upgradeTagPropertiesFile(tagNameTuples, standardTags));
214 }
else if (numberOfAttributes == 4) {
216 tagNames.addAll(readCurrentTagPropertiesFile(tagNameTuples, standardTags));
220 for (String standardTagName : standardTags) {
221 if (STANDARD_NOTABLE_TAG_DISPLAY_NAMES.contains(standardTagName)) {
222 tagNames.add(
new TagNameDefinition(standardTagName,
"", TagName.HTML_COLOR.NONE, TskData.FileKnown.BAD));
224 tagNames.add(
new TagNameDefinition(standardTagName,
"", TagName.HTML_COLOR.NONE, TskData.FileKnown.UNKNOWN));
242 private static Set<TagNameDefinition> upgradeTagPropertiesFile(List<String> tagProperties, Set<String> standardTagsToBeCreated) {
243 Set<TagNameDefinition> tagNames =
new LinkedHashSet<>();
244 List<String> legacyNotableTags =
new ArrayList<>();
245 String badTagsStr = ModuleSettings.getConfigSetting(
"CentralRepository",
"db.badTags");
246 if (badTagsStr == null || badTagsStr.isEmpty()) {
247 legacyNotableTags.addAll(STANDARD_NOTABLE_TAG_DISPLAY_NAMES);
249 legacyNotableTags.addAll(Arrays.asList(badTagsStr.split(
",")));
251 for (String tagNameTuple : tagProperties) {
252 String[] tagNameAttributes = tagNameTuple.split(
",");
253 standardTagsToBeCreated.remove(tagNameAttributes[0]);
254 if (legacyNotableTags.contains(tagNameAttributes[0])) {
255 tagNames.add(
new TagNameDefinition(tagNameAttributes[0], tagNameAttributes[1],
256 TagName.HTML_COLOR.valueOf(tagNameAttributes[2]), TskData.FileKnown.BAD));
258 tagNames.add(
new TagNameDefinition(tagNameAttributes[0], tagNameAttributes[1],
259 TagName.HTML_COLOR.valueOf(tagNameAttributes[2]), TskData.FileKnown.UNKNOWN));
276 private static Set<TagNameDefinition> readCurrentTagPropertiesFile(List<String> tagProperties, Set<String> standardTagsToBeCreated) {
277 Set<TagNameDefinition> tagNames =
new LinkedHashSet<>();
278 for (String tagNameTuple : tagProperties) {
279 String[] tagNameAttributes = tagNameTuple.split(
",");
280 standardTagsToBeCreated.remove(tagNameAttributes[0]);
281 tagNames.add(
new TagNameDefinition(tagNameAttributes[0], tagNameAttributes[1],
282 TagName.HTML_COLOR.valueOf(tagNameAttributes[2]), TskData.FileKnown.valueOf(tagNameAttributes[3])));
292 static synchronized void setTagNameDefinitions(Set<TagNameDefinition> tagNames) {
293 StringBuilder setting =
new StringBuilder();
294 for (TagNameDefinition tagName : tagNames) {
295 if (setting.length() != 0) {
298 setting.append(tagName.toSettingsFormat());
300 SleuthkitCase caseDb = Case.getCurrentCaseThrows().getSleuthkitCase();
301 tagName.saveToCase(caseDb);
302 }
catch (NoCurrentCaseException ex) {
303 LOGGER.log(Level.SEVERE,
"Exception while getting open case.", ex);
306 ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY, setting.toString());