Autopsy  4.8.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
KeywordSearchUtil.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 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.keywordsearch;
20 
21 import java.awt.Component;
22 import java.io.File;
24 import javax.swing.JOptionPane;
25 import org.openide.windows.WindowManager;
26 
27 class KeywordSearchUtil {
28 
29  public enum DIALOG_MESSAGE_TYPE {
30 
31  ERROR, WARN, INFO
32  };
33  private static final Logger logger = Logger.getLogger(KeywordSearchUtil.class.getName());
34 
42  public static String quoteQuery(String query) {
43  //ensure a single pair of quotes around the query
44  final int length = query.length();
45  if (length > 1 && query.charAt(0) == '"'
46  && query.charAt(length - 1) == '"') {
47  return query;
48  }
49 
50  return "\""+query+"\"";
51  }
52 
62  public static String escapeLuceneQuery(String query) {
63  String queryEscaped = null;
64  String inputString = query.trim();
65 
66  if (inputString.length() == 0) {
67  return inputString;
68  }
69 
70  final String ESCAPE_CHARS = "/+-&|!(){}[]^\"~*?:\\";
71  StringBuilder sb = new StringBuilder();
72  final int length = inputString.length();
73 
74  //see if the quoery is quoted
75  boolean quotedQuery = false;
76  if (length > 1 && inputString.charAt(0) == '"' && inputString.charAt(length - 1) == '"') {
77  quotedQuery = true;
78  }
79 
80  for (int i = 0; i < length; ++i) {
81  final char c = inputString.charAt(i);
82 
83  if (ESCAPE_CHARS.contains(Character.toString(c))) {
84  //escape if not outter quotes
85  if (quotedQuery == false || (i > 0 && i < length - 1)) {
86  sb.append("\\");
87  }
88  }
89  sb.append(c);
90  }
91  queryEscaped = inputString = sb.toString();
92 
93  return queryEscaped;
94  }
95 
96  public static void displayDialog(final String title, final String message, final DIALOG_MESSAGE_TYPE type) {
97  int messageType;
98  if (type == DIALOG_MESSAGE_TYPE.ERROR) {
99  messageType = JOptionPane.ERROR_MESSAGE;
100  } else if (type == DIALOG_MESSAGE_TYPE.WARN) {
101  messageType = JOptionPane.WARNING_MESSAGE;
102  } else {
103  messageType = JOptionPane.INFORMATION_MESSAGE;
104  }
105 
106  final Component parentComponent = WindowManager.getDefault().getMainWindow();
107  JOptionPane.showMessageDialog(
108  parentComponent,
109  message,
110  title,
111  messageType);
112  }
113 
114  public static boolean displayConfirmDialog(final String title, final String message, final DIALOG_MESSAGE_TYPE type) {
115  int messageType;
116  if (type == DIALOG_MESSAGE_TYPE.ERROR) {
117  messageType = JOptionPane.ERROR_MESSAGE;
118  } else if (type == DIALOG_MESSAGE_TYPE.WARN) {
119  messageType = JOptionPane.WARNING_MESSAGE;
120  } else {
121  messageType = JOptionPane.INFORMATION_MESSAGE;
122  }
123  if (JOptionPane.showConfirmDialog(WindowManager.getDefault().getMainWindow(), message, title, JOptionPane.YES_NO_OPTION, messageType) == JOptionPane.YES_OPTION) {
124  return true;
125  } else {
126  return false;
127  }
128  }
129 
130  static KeywordSearchQuery getQueryForKeyword(Keyword keyword, KeywordList keywordList) {
131  KeywordSearchQuery query = null;
132  if (keyword.searchTermIsLiteral() && keyword.searchTermIsWholeWord()) {
133  // literal, exact match
134  query = new LuceneQuery(keywordList, keyword);
135  query.escape();
136  } // regexp and literal substring match
137  else {
138  query = new RegexQuery(keywordList, keyword);
139  if (keyword.searchTermIsLiteral()) {
140  query.escape();
141  }
142  }
143  return query;
144  }
145 
153  static boolean isXMLList(String absPath) {
154  //TODO: make this more robust, if necessary
155  return new File(absPath).getName().endsWith(".xml"); //NON-NLS
156  }
157 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2018 Basis Technology. Generated on: Thu Oct 4 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.