19 package org.sleuthkit.autopsy.keywordsearch;
21 import java.awt.Component;
23 import java.io.Reader;
24 import java.util.regex.Matcher;
26 import javax.swing.JOptionPane;
27 import org.openide.util.Lookup;
28 import org.openide.windows.WindowManager;
36 class KeywordSearchUtil {
38 private static final String SNIPPET_DELIMITER = String.valueOf(Character.toChars(171));
53 public static String quoteQuery(String query) {
55 final int length = query.length();
56 if (length > 1 && query.charAt(0) ==
'"'
57 && query.charAt(length - 1) ==
'"') {
61 return "\""+query+
"\"";
73 public static String escapeLuceneQuery(String query) {
74 String queryEscaped = null;
75 String inputString = query.trim();
77 if (inputString.length() == 0) {
81 final String ESCAPE_CHARS =
"/+-&|!(){}[]^\"~*?:\\";
82 StringBuilder sb =
new StringBuilder();
83 final int length = inputString.length();
86 boolean quotedQuery =
false;
87 if (length > 1 && inputString.charAt(0) ==
'"' && inputString.charAt(length - 1) ==
'"') {
91 for (
int i = 0; i < length; ++i) {
92 final char c = inputString.charAt(i);
94 if (ESCAPE_CHARS.contains(Character.toString(c))) {
96 if (quotedQuery ==
false || (i > 0 && i < length - 1)) {
102 queryEscaped = inputString = sb.toString();
107 public static void displayDialog(
final String title,
final String message,
final DIALOG_MESSAGE_TYPE type) {
109 if (type == DIALOG_MESSAGE_TYPE.ERROR) {
110 messageType = JOptionPane.ERROR_MESSAGE;
111 }
else if (type == DIALOG_MESSAGE_TYPE.WARN) {
112 messageType = JOptionPane.WARNING_MESSAGE;
114 messageType = JOptionPane.INFORMATION_MESSAGE;
117 final Component parentComponent = WindowManager.getDefault().getMainWindow();
118 JOptionPane.showMessageDialog(
125 public static boolean displayConfirmDialog(
final String title,
final String message,
final DIALOG_MESSAGE_TYPE type) {
127 if (type == DIALOG_MESSAGE_TYPE.ERROR) {
128 messageType = JOptionPane.ERROR_MESSAGE;
129 }
else if (type == DIALOG_MESSAGE_TYPE.WARN) {
130 messageType = JOptionPane.WARNING_MESSAGE;
132 messageType = JOptionPane.INFORMATION_MESSAGE;
134 if (JOptionPane.showConfirmDialog(WindowManager.getDefault().getMainWindow(), message, title, JOptionPane.YES_NO_OPTION, messageType) == JOptionPane.YES_OPTION) {
141 static KeywordSearchQuery getQueryForKeyword(Keyword keyword, KeywordList keywordList) {
142 KeywordSearchQuery query = null;
143 if (keyword.searchTermIsLiteral() && keyword.searchTermIsWholeWord()) {
145 query =
new LuceneQuery(keywordList, keyword);
149 query =
new RegexQuery(keywordList, keyword);
150 if (keyword.searchTermIsLiteral()) {
170 static String makeSnippet(String content, Matcher hitMatcher, String hit) {
172 final int end = hitMatcher.end();
173 final int start = hitMatcher.start();
175 return makeSnippet(content, start, end, hit);
178 static String makeSnippet(String content,
int startOffset,
int endOffset, String hit) {
180 int maxIndex = content.length() - 1;
182 return content.substring(Integer.max(0, startOffset - 20), Integer.max(0, startOffset))
183 + SNIPPET_DELIMITER + hit + SNIPPET_DELIMITER
184 + content.substring(Integer.min(maxIndex, endOffset), Integer.min(maxIndex, endOffset + 20));
194 static boolean isXMLList(String absPath) {
196 return new File(absPath).getName().endsWith(
".xml");
199 static Reader getReader(Content content)
throws TextExtractorFactory.NoTextExtractorFound, TextExtractor.InitReaderException{
200 return getReader(content, null);
203 static Reader getReader(Content content, Lookup stringsExtractionContext)
throws TextExtractorFactory.NoTextExtractorFound, TextExtractor.InitReaderException{
204 Reader reader = null;
205 if (content instanceof BlackboardArtifact) {
206 BlackboardArtifact artifact = (BlackboardArtifact) content;
207 if (artifact.getArtifactID() > 0) {
215 TextExtractor blackboardExtractor = TextExtractorFactory.getExtractor(content, null);
216 reader = blackboardExtractor.getReader();
218 }
else if (content instanceof AbstractFile) {
219 TextExtractor stringsExtractor = TextExtractorFactory.getStringsExtractor( content, stringsExtractionContext);
220 reader = stringsExtractor.getReader();
223 TextExtractor contentExtractor = TextExtractorFactory.getExtractor(content, null);
224 reader = contentExtractor.getReader();
225 }
catch (TextExtractorFactory.NoTextExtractorFound | TextExtractor.InitReaderException ex) {
227 TextExtractor stringsExtractor = TextExtractorFactory.getStringsExtractor(content, null);
228 reader = stringsExtractor.getReader();
synchronized static Logger getLogger(String name)