19 package org.sleuthkit.autopsy.modules.interestingitems;
21 import java.util.List;
22 import java.util.logging.Level;
23 import java.util.regex.Pattern;
24 import java.util.regex.PatternSyntaxException;
25 import org.openide.DialogDisplayer;
26 import org.openide.NotifyDescriptor;
27 import org.openide.util.NbBundle;
34 final class FilesSetRulePanel
extends javax.swing.JPanel {
36 private static final Logger logger = Logger.
getLogger(FilesSetRulePanel.class.getName());
37 private static final String SLEUTHKIT_PATH_SEPARATOR =
"/";
38 private static final List<String> ILLEGAL_FILE_NAME_CHARS = InterestingItemDefsManager.getIllegalFileNameChars();
39 private static final List<String> ILLEGAL_FILE_PATH_CHARS = InterestingItemDefsManager.getIllegalFilePathChars();
46 populateComponentsWithDefaultValues();
54 FilesSetRulePanel(FilesSet.Rule rule) {
56 populateRuleNameComponent(rule);
57 populateTypeFilterComponents(rule);
58 populateNameFilterComponents(rule);
59 populatePathFilterComponents(rule);
65 private void populateComponentsWithDefaultValues() {
66 this.filesRadioButton.setSelected(
true);
67 this.fullNameRadioButton.setSelected(
true);
75 private void populateRuleNameComponent(FilesSet.Rule rule) {
76 this.ruleNameTextField.setText(rule.getName());
84 private void populateTypeFilterComponents(FilesSet.Rule rule) {
85 FilesSet.Rule.MetaTypeFilter typeFilter = rule.getMetaTypeFilter();
86 switch (typeFilter.getMetaType()) {
88 this.filesRadioButton.setSelected(
true);
91 this.dirsRadioButton.setSelected(
true);
93 case FILES_AND_DIRECTORIES:
94 this.filesAndDirsRadioButton.setSelected(
true);
104 private void populateNameFilterComponents(FilesSet.Rule rule) {
105 FilesSet.Rule.FileNameFilter nameFilter = rule.getFileNameFilter();
106 this.nameTextField.setText(nameFilter.getTextToMatch());
107 this.nameRegexCheckbox.setSelected(nameFilter.isRegex());
108 if (nameFilter instanceof FilesSet.Rule.FullNameFilter) {
109 this.fullNameRadioButton.setSelected(
true);
111 this.extensionRadioButton.setSelected(
true);
121 private void populatePathFilterComponents(FilesSet.Rule rule) {
122 FilesSet.Rule.ParentPathFilter pathFilter = rule.getPathFilter();
123 if (pathFilter != null) {
124 this.pathTextField.setText(pathFilter.getTextToMatch());
125 this.pathRegexCheckBox.setSelected(pathFilter.isRegex());
136 boolean isValidRuleDefinition() {
139 if (this.nameTextField.getText().isEmpty()) {
140 NotifyDescriptor notifyDesc =
new NotifyDescriptor.Message(
141 NbBundle.getMessage(FilesSetPanel.class,
"FilesSetRulePanel.messages.emptyNameFilter"),
142 NotifyDescriptor.WARNING_MESSAGE);
143 DialogDisplayer.getDefault().notify(notifyDesc);
149 if (this.nameRegexCheckbox.isSelected()) {
151 Pattern.compile(this.nameTextField.getText());
152 }
catch (PatternSyntaxException ex) {
153 NotifyDescriptor notifyDesc =
new NotifyDescriptor.Message(
154 NbBundle.getMessage(FilesSetPanel.class,
"FilesSetRulePanel.messages.invalidNameRegex", ex.getLocalizedMessage()),
155 NotifyDescriptor.WARNING_MESSAGE);
156 DialogDisplayer.getDefault().notify(notifyDesc);
160 if (!FilesSetRulePanel.containsOnlyLegalChars(
this.nameTextField.getText(), FilesSetRulePanel.ILLEGAL_FILE_NAME_CHARS)) {
161 NotifyDescriptor notifyDesc =
new NotifyDescriptor.Message(
162 NbBundle.getMessage(FilesSetPanel.class,
"FilesSetRulePanel.messages.invalidCharInName"),
163 NotifyDescriptor.WARNING_MESSAGE);
164 DialogDisplayer.getDefault().notify(notifyDesc);
171 if (!this.pathTextField.getText().isEmpty()) {
172 if (this.pathRegexCheckBox.isSelected()) {
174 Pattern.compile(this.pathTextField.getText());
175 }
catch (PatternSyntaxException ex) {
176 NotifyDescriptor notifyDesc =
new NotifyDescriptor.Message(
177 NbBundle.getMessage(FilesSetPanel.class,
"FilesSetRulePanel.messages.invalidPathRegex", ex.getLocalizedMessage()),
178 NotifyDescriptor.WARNING_MESSAGE);
179 DialogDisplayer.getDefault().notify(notifyDesc);
183 if (!FilesSetRulePanel.containsOnlyLegalChars(
this.pathTextField.getText(), FilesSetRulePanel.ILLEGAL_FILE_PATH_CHARS)) {
184 NotifyDescriptor notifyDesc =
new NotifyDescriptor.Message(
185 NbBundle.getMessage(FilesSetPanel.class,
"FilesSetRulePanel.messages.invalidCharInPath"),
186 NotifyDescriptor.WARNING_MESSAGE);
187 DialogDisplayer.getDefault().notify(notifyDesc);
201 String getRuleName() {
202 return this.ruleNameTextField.getText();
212 FilesSet.Rule.FileNameFilter getFileNameFilter() throws IllegalStateException {
213 FilesSet.Rule.FileNameFilter filter = null;
214 if (!this.nameTextField.getText().isEmpty()) {
215 if (this.nameRegexCheckbox.isSelected()) {
217 Pattern pattern = Pattern.compile(this.nameTextField.getText());
218 if (this.fullNameRadioButton.isSelected()) {
219 filter =
new FilesSet.Rule.FullNameFilter(pattern);
221 filter =
new FilesSet.Rule.ExtensionFilter(pattern);
223 }
catch (PatternSyntaxException ex) {
224 logger.log(Level.SEVERE,
"Attempt to get regex name filter that does not compile", ex);
225 throw new IllegalStateException(
"The files set rule panel name filter is not in a valid state");
228 if (FilesSetRulePanel.containsOnlyLegalChars(
this.nameTextField.getText(), FilesSetRulePanel.ILLEGAL_FILE_NAME_CHARS)) {
229 if (this.fullNameRadioButton.isSelected()) {
230 filter =
new FilesSet.Rule.FullNameFilter(this.nameTextField.getText());
232 filter =
new FilesSet.Rule.ExtensionFilter(this.nameTextField.getText());
235 logger.log(Level.SEVERE,
"Attempt to get name filter with illegal chars");
236 throw new IllegalStateException(
"The files set rule panel name filter is not in a valid state");
248 FilesSet.Rule.MetaTypeFilter getMetaTypeFilter() {
249 if (this.filesRadioButton.isSelected()) {
250 return new FilesSet.Rule.MetaTypeFilter(FilesSet.Rule.MetaTypeFilter.Type.FILES);
251 }
else if (this.dirsRadioButton.isSelected()) {
252 return new FilesSet.Rule.MetaTypeFilter(FilesSet.Rule.MetaTypeFilter.Type.DIRECTORIES);
254 return new FilesSet.Rule.MetaTypeFilter(FilesSet.Rule.MetaTypeFilter.Type.FILES_AND_DIRECTORIES);
265 FilesSet.Rule.ParentPathFilter getPathFilter() throws IllegalStateException {
266 FilesSet.Rule.ParentPathFilter filter = null;
267 if (!this.pathTextField.getText().isEmpty()) {
268 if (this.pathRegexCheckBox.isSelected()) {
270 filter =
new FilesSet.Rule.ParentPathFilter(Pattern.compile(
this.pathTextField.getText()));
271 }
catch (PatternSyntaxException ex) {
272 logger.log(Level.SEVERE,
"Attempt to get malformed path filter", ex);
273 throw new IllegalStateException(
"The files set rule panel path filter is not in a valid state");
276 String path = this.pathTextField.getText();
277 if (FilesSetRulePanel.containsOnlyLegalChars(path, FilesSetRulePanel.ILLEGAL_FILE_PATH_CHARS)) {
279 if (!path.startsWith(FilesSetRulePanel.SLEUTHKIT_PATH_SEPARATOR)) {
280 path = FilesSetRulePanel.SLEUTHKIT_PATH_SEPARATOR + path;
283 if (!path.endsWith(FilesSetRulePanel.SLEUTHKIT_PATH_SEPARATOR)) {
284 path += FilesSetRulePanel.SLEUTHKIT_PATH_SEPARATOR;
286 filter =
new FilesSet.Rule.ParentPathFilter(path);
288 logger.log(Level.SEVERE,
"Attempt to get path filter with illegal chars");
289 throw new IllegalStateException(
"The files set rule panel path filter is not in a valid state");
304 private static boolean containsOnlyLegalChars(String toBeChecked, List<String> illegalChars) {
305 for (String illegalChar : illegalChars) {
306 if (toBeChecked.contains(illegalChar)) {
317 private void setComponentsForSearchType() {
318 if (!this.filesRadioButton.isSelected()) {
319 this.fullNameRadioButton.setSelected(
true);
320 this.extensionRadioButton.setEnabled(
false);
322 this.extensionRadioButton.setEnabled(
true);
331 @SuppressWarnings(
"unchecked")
333 private
void initComponents() {
335 nameButtonGroup =
new javax.swing.ButtonGroup();
336 typeButtonGroup =
new javax.swing.ButtonGroup();
337 ruleNameLabel =
new javax.swing.JLabel();
338 ruleNameTextField =
new javax.swing.JTextField();
339 jLabel1 =
new javax.swing.JLabel();
340 dirsRadioButton =
new javax.swing.JRadioButton();
341 filesRadioButton =
new javax.swing.JRadioButton();
342 filesAndDirsRadioButton =
new javax.swing.JRadioButton();
343 jLabel2 =
new javax.swing.JLabel();
344 nameTextField =
new javax.swing.JTextField();
345 fullNameRadioButton =
new javax.swing.JRadioButton();
346 extensionRadioButton =
new javax.swing.JRadioButton();
347 nameRegexCheckbox =
new javax.swing.JCheckBox();
348 jLabel3 =
new javax.swing.JLabel();
349 pathTextField =
new javax.swing.JTextField();
350 pathRegexCheckBox =
new javax.swing.JCheckBox();
351 pathSeparatorInfoLabel =
new javax.swing.JLabel();
352 jLabel4 =
new javax.swing.JLabel();
353 jLabel5 =
new javax.swing.JLabel();
355 org.openide.awt.Mnemonics.setLocalizedText(ruleNameLabel,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.ruleNameLabel.text"));
357 ruleNameTextField.setText(
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.ruleNameTextField.text"));
359 org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.jLabel1.text"));
361 typeButtonGroup.add(dirsRadioButton);
362 org.openide.awt.Mnemonics.setLocalizedText(dirsRadioButton,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.dirsRadioButton.text"));
363 dirsRadioButton.addActionListener(
new java.awt.event.ActionListener() {
364 public void actionPerformed(java.awt.event.ActionEvent evt) {
365 dirsRadioButtonActionPerformed(evt);
369 typeButtonGroup.add(filesRadioButton);
370 org.openide.awt.Mnemonics.setLocalizedText(filesRadioButton,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.filesRadioButton.text"));
371 filesRadioButton.addActionListener(
new java.awt.event.ActionListener() {
372 public void actionPerformed(java.awt.event.ActionEvent evt) {
373 filesRadioButtonActionPerformed(evt);
377 typeButtonGroup.add(filesAndDirsRadioButton);
378 org.openide.awt.Mnemonics.setLocalizedText(filesAndDirsRadioButton,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.filesAndDirsRadioButton.text"));
379 filesAndDirsRadioButton.addActionListener(
new java.awt.event.ActionListener() {
380 public void actionPerformed(java.awt.event.ActionEvent evt) {
381 filesAndDirsRadioButtonActionPerformed(evt);
385 org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.jLabel2.text"));
387 nameTextField.setText(
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.nameTextField.text"));
389 nameButtonGroup.add(fullNameRadioButton);
390 org.openide.awt.Mnemonics.setLocalizedText(fullNameRadioButton,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.fullNameRadioButton.text"));
392 nameButtonGroup.add(extensionRadioButton);
393 org.openide.awt.Mnemonics.setLocalizedText(extensionRadioButton,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.extensionRadioButton.text"));
395 org.openide.awt.Mnemonics.setLocalizedText(nameRegexCheckbox,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.nameRegexCheckbox.text"));
397 org.openide.awt.Mnemonics.setLocalizedText(jLabel3,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.jLabel3.text"));
399 pathTextField.setText(
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.pathTextField.text"));
401 org.openide.awt.Mnemonics.setLocalizedText(pathRegexCheckBox,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.pathRegexCheckBox.text"));
403 pathSeparatorInfoLabel.setIcon(
new javax.swing.ImageIcon(getClass().getResource(
"/org/sleuthkit/autopsy/images/info-icon-16.png")));
404 org.openide.awt.Mnemonics.setLocalizedText(pathSeparatorInfoLabel,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.pathSeparatorInfoLabel.text"));
406 org.openide.awt.Mnemonics.setLocalizedText(jLabel4,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.jLabel4.text"));
408 org.openide.awt.Mnemonics.setLocalizedText(jLabel5,
org.openide.util.NbBundle.getMessage(FilesSetRulePanel.class,
"FilesSetRulePanel.jLabel5.text"));
410 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(
this);
411 this.setLayout(layout);
412 layout.setHorizontalGroup(
413 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
414 .addGroup(layout.createSequentialGroup()
416 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
417 .addGroup(layout.createSequentialGroup()
418 .addComponent(jLabel3)
420 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
421 .addGroup(layout.createSequentialGroup()
422 .addComponent(pathRegexCheckBox)
424 .addComponent(pathSeparatorInfoLabel))
425 .addComponent(pathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)))
426 .addGroup(layout.createSequentialGroup()
427 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
428 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
429 .addComponent(ruleNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
431 .addGroup(layout.createSequentialGroup()
432 .addComponent(jLabel1)
433 .addGap(27, 27, 27)))
434 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
435 .addGroup(layout.createSequentialGroup()
436 .addComponent(filesRadioButton)
437 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
438 .addComponent(dirsRadioButton)
439 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
440 .addComponent(filesAndDirsRadioButton))
441 .addComponent(ruleNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)))
442 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
false)
443 .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
444 .addComponent(jLabel2)
445 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
446 .addComponent(nameTextField))
447 .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
449 .addComponent(fullNameRadioButton)
450 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
451 .addComponent(extensionRadioButton, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
452 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
453 .addComponent(nameRegexCheckbox)))
454 .addComponent(jLabel4)
455 .addComponent(jLabel5))
456 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
458 layout.setVerticalGroup(
459 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
460 .addGroup(layout.createSequentialGroup()
461 .addComponent(jLabel5)
462 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 19, Short.MAX_VALUE)
463 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
464 .addComponent(jLabel1)
465 .addComponent(dirsRadioButton)
466 .addComponent(filesRadioButton)
467 .addComponent(filesAndDirsRadioButton))
468 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
469 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
470 .addComponent(jLabel2)
471 .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
472 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
473 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
474 .addComponent(fullNameRadioButton)
475 .addComponent(extensionRadioButton)
476 .addComponent(nameRegexCheckbox))
477 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
478 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
479 .addComponent(pathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
480 .addComponent(jLabel3))
481 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
482 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
483 .addComponent(pathSeparatorInfoLabel)
484 .addComponent(pathRegexCheckBox))
485 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
486 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
487 .addComponent(ruleNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
488 .addComponent(ruleNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
489 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
490 .addComponent(jLabel4))
494 private void filesAndDirsRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
495 setComponentsForSearchType();
498 private void dirsRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
499 setComponentsForSearchType();
502 private void filesRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
503 setComponentsForSearchType();
507 private javax.swing.JRadioButton dirsRadioButton;
508 private javax.swing.JRadioButton extensionRadioButton;
509 private javax.swing.JRadioButton filesAndDirsRadioButton;
510 private javax.swing.JRadioButton filesRadioButton;
511 private javax.swing.JRadioButton fullNameRadioButton;
512 private javax.swing.JLabel jLabel1;
513 private javax.swing.JLabel jLabel2;
514 private javax.swing.JLabel jLabel3;
515 private javax.swing.JLabel jLabel4;
516 private javax.swing.JLabel jLabel5;
517 private javax.swing.ButtonGroup nameButtonGroup;
518 private javax.swing.JCheckBox nameRegexCheckbox;
519 private javax.swing.JTextField nameTextField;
520 private javax.swing.JCheckBox pathRegexCheckBox;
521 private javax.swing.JLabel pathSeparatorInfoLabel;
522 private javax.swing.JTextField pathTextField;
523 private javax.swing.JLabel ruleNameLabel;
524 private javax.swing.JTextField ruleNameTextField;
525 private javax.swing.ButtonGroup typeButtonGroup;
static Logger getLogger(String name)