Autopsy  4.21.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;
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;
31 import org.sleuthkit.datamodel.AbstractFile;
32 import org.sleuthkit.datamodel.BlackboardArtifact;
33 import org.sleuthkit.datamodel.Content;
34 import org.sleuthkit.datamodel.TskCoreException;
35 
36 class KeywordSearchUtil {
37 
38  private static final String SNIPPET_DELIMITER = String.valueOf(Character.toChars(171));
39 
40  public enum DIALOG_MESSAGE_TYPE {
41 
42  ERROR, WARN, INFO
43  };
44  private static final Logger logger = Logger.getLogger(KeywordSearchUtil.class.getName());
45 
53  public static String quoteQuery(String query) {
54  //ensure a single pair of quotes around the query
55  final int length = query.length();
56  if (length > 1 && query.charAt(0) == '"'
57  && query.charAt(length - 1) == '"') {
58  return query;
59  }
60 
61  return "\""+query+"\"";
62  }
63 
73  public static String escapeLuceneQuery(String query) {
74  String queryEscaped = null;
75  String inputString = query.trim();
76 
77  if (inputString.length() == 0) {
78  return inputString;
79  }
80 
81  final String ESCAPE_CHARS = "/+-&|!(){}[]^\"~*?:\\";
82  StringBuilder sb = new StringBuilder();
83  final int length = inputString.length();
84 
85  //see if the quoery is quoted
86  boolean quotedQuery = false;
87  if (length > 1 && inputString.charAt(0) == '"' && inputString.charAt(length - 1) == '"') {
88  quotedQuery = true;
89  }
90 
91  for (int i = 0; i < length; ++i) {
92  final char c = inputString.charAt(i);
93 
94  if (ESCAPE_CHARS.contains(Character.toString(c))) {
95  //escape if not outter quotes
96  if (quotedQuery == false || (i > 0 && i < length - 1)) {
97  sb.append("\\");
98  }
99  }
100  sb.append(c);
101  }
102  queryEscaped = inputString = sb.toString();
103 
104  return queryEscaped;
105  }
106 
107  public static void displayDialog(final String title, final String message, final DIALOG_MESSAGE_TYPE type) {
108  int messageType;
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;
113  } else {
114  messageType = JOptionPane.INFORMATION_MESSAGE;
115  }
116 
117  final Component parentComponent = WindowManager.getDefault().getMainWindow();
118  JOptionPane.showMessageDialog(
119  parentComponent,
120  message,
121  title,
122  messageType);
123  }
124 
125  public static boolean displayConfirmDialog(final String title, final String message, final DIALOG_MESSAGE_TYPE type) {
126  int messageType;
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;
131  } else {
132  messageType = JOptionPane.INFORMATION_MESSAGE;
133  }
134  if (JOptionPane.showConfirmDialog(WindowManager.getDefault().getMainWindow(), message, title, JOptionPane.YES_NO_OPTION, messageType) == JOptionPane.YES_OPTION) {
135  return true;
136  } else {
137  return false;
138  }
139  }
140 
141  static KeywordSearchQuery getQueryForKeyword(Keyword keyword, KeywordList keywordList) {
142  KeywordSearchQuery query = null;
143  if (keyword.searchTermIsLiteral() && keyword.searchTermIsWholeWord()) {
144  // literal, exact match
145  query = new LuceneQuery(keywordList, keyword);
146  query.escape();
147  } // regexp and literal substring match
148  else {
149  query = new RegexQuery(keywordList, keyword);
150  if (keyword.searchTermIsLiteral()) {
151  query.escape();
152  }
153  }
154  return query;
155  }
156 
170  static String makeSnippet(String content, Matcher hitMatcher, String hit) {
171  // Get the snippet from the document.
172  final int end = hitMatcher.end();
173  final int start = hitMatcher.start();
174 
175  return makeSnippet(content, start, end, hit);
176  }
177 
178  static String makeSnippet(String content, int startOffset, int endOffset, String hit) {
179  // Get the snippet from the document.
180  int maxIndex = content.length() - 1;
181 
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));
185  }
186 
194  static boolean isXMLList(String absPath) {
195  //TODO: make this more robust, if necessary
196  return new File(absPath).getName().endsWith(".xml"); //NON-NLS
197  }
198 
199  static Reader getReader(Content content) throws TextExtractorFactory.NoTextExtractorFound, TextExtractor.InitReaderException{
200  return getReader(content, null);
201  }
202 
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) {
208  /*
209  * Artifact indexing is only supported for artifacts that use
210  * negative artifact ids to avoid overlapping with the object
211  * ids of other types of Content.
212  */
213  return null;
214  }
215  TextExtractor blackboardExtractor = TextExtractorFactory.getExtractor(content, null);
216  reader = blackboardExtractor.getReader();
217 
218  } else if (content instanceof AbstractFile) {
219  TextExtractor stringsExtractor = TextExtractorFactory.getStringsExtractor( content, stringsExtractionContext);
220  reader = stringsExtractor.getReader();
221  } else {
222  try {
223  TextExtractor contentExtractor = TextExtractorFactory.getExtractor(content, null);
224  reader = contentExtractor.getReader();
225  } catch (TextExtractorFactory.NoTextExtractorFound | TextExtractor.InitReaderException ex) {
226  // Try the StringsTextExtractor if Tika extractions fails.
227  TextExtractor stringsExtractor = TextExtractorFactory.getStringsExtractor(content, null);
228  reader = stringsExtractor.getReader();
229  }
230  }
231 
232  return reader;
233  }
234 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.