Autopsy  4.1
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 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  StringBuilder sb = new StringBuilder();
51  sb.append("\"").append(query).append("\"");
52  return sb.toString();
53  }
54 
64  public static String escapeLuceneQuery(String query) {
65  String queryEscaped = null;
66  String inputString = query.trim();
67 
68  if (inputString.length() == 0) {
69  return inputString;
70  }
71 
72  final String ESCAPE_CHARS = "/+-&|!(){}[]^\"~*?:\\";
73  StringBuilder sb = new StringBuilder();
74  final int length = inputString.length();
75 
76  //see if the quoery is quoted
77  boolean quotedQuery = false;
78  if (length > 1 && inputString.charAt(0) == '"' && inputString.charAt(length - 1) == '"') {
79  quotedQuery = true;
80  }
81 
82  for (int i = 0; i < length; ++i) {
83  final char c = inputString.charAt(i);
84 
85  if (ESCAPE_CHARS.contains(Character.toString(c))) {
86  //escape if not outter quotes
87  if (quotedQuery == false || (i > 0 && i < length - 1)) {
88  sb.append("\\");
89  }
90  }
91  sb.append(c);
92  }
93  queryEscaped = inputString = sb.toString();
94 
95  return queryEscaped;
96  }
97 
98  public static void displayDialog(final String title, final String message, final DIALOG_MESSAGE_TYPE type) {
99  int messageType;
100  if (type == DIALOG_MESSAGE_TYPE.ERROR) {
101  messageType = JOptionPane.ERROR_MESSAGE;
102  } else if (type == DIALOG_MESSAGE_TYPE.WARN) {
103  messageType = JOptionPane.WARNING_MESSAGE;
104  } else {
105  messageType = JOptionPane.INFORMATION_MESSAGE;
106  }
107 
108  final Component parentComponent = WindowManager.getDefault().getMainWindow();
109  JOptionPane.showMessageDialog(
110  parentComponent,
111  message,
112  title,
113  messageType);
114  }
115 
116  public static boolean displayConfirmDialog(final String title, final String message, final DIALOG_MESSAGE_TYPE type) {
117  int messageType;
118  if (type == DIALOG_MESSAGE_TYPE.ERROR) {
119  messageType = JOptionPane.ERROR_MESSAGE;
120  } else if (type == DIALOG_MESSAGE_TYPE.WARN) {
121  messageType = JOptionPane.WARNING_MESSAGE;
122  } else {
123  messageType = JOptionPane.INFORMATION_MESSAGE;
124  }
125  if (JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION, messageType) == JOptionPane.YES_OPTION) {
126  return true;
127  } else {
128  return false;
129  }
130  }
131 
139  static boolean isXMLList(String absPath) {
140  //TODO: make this more robust, if necessary
141  return new File(absPath).getName().endsWith(".xml"); //NON-NLS
142  }
143 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:161

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