23 package org.sleuthkit.autopsy.recentactivity;
 
   27 import java.io.FileInputStream;
 
   28 import java.io.IOException;
 
   29 import java.nio.MappedByteBuffer;
 
   30 import java.nio.channels.FileChannel;
 
   31 import java.nio.charset.Charset;
 
   32 import java.sql.ResultSet;
 
   33 import java.text.SimpleDateFormat;
 
   34 import java.util.Date;
 
   35 import java.util.List;
 
   36 import java.util.logging.Level;
 
   38 import java.util.regex.Matcher;
 
   39 import java.util.regex.Pattern;
 
   53     private static Logger logger = Logger.getLogger(Util.class.getName());
 
   58     public static boolean pathexists(String path) {
 
   59         File file = 
new File(path);
 
   60         boolean exists = file.exists();
 
   64     public static String utcConvert(String utc) {
 
   65         SimpleDateFormat formatter = 
new SimpleDateFormat(
"MM-dd-yyyy HH:mm");
 
   66         String tempconvert = formatter.format(
new Date(Long.parseLong(utc)));
 
   70     public static String readFile(String path) 
throws IOException {
 
   71         FileInputStream stream = 
new FileInputStream(
new File(path));
 
   73             FileChannel fc = stream.getChannel();
 
   74             MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
 
   78             return Charset.defaultCharset().decode(bb).toString();
 
   84     public static String getFileName(String value) {
 
   86         String filematch = 
"^([a-zA-Z]\\:)(\\\\[^\\\\/:*?<>\"|]*(?<!\\[ \\]))*(\\.[a-zA-Z]{2,6})$"; 
 
   88         Pattern p = Pattern.compile(filematch, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.COMMENTS);
 
   89         Matcher m = p.matcher(value);
 
   91             filename = m.group(1);
 
   94         int lastPos = value.lastIndexOf(
'\\');
 
   95         filename = (lastPos < 0) ? value : value.substring(lastPos + 1);
 
   96         return filename.toString();
 
   99     public static String getPath(String txt) {
 
  103         String drive = 
"([a-z]:\\\\\\S.+)"; 
 
  104         Pattern p = Pattern.compile(drive, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
 
  105         Matcher m = p.matcher(txt);
 
  111             String network = 
"(\\\\(?:\\\\[^:\\s?*\"<>|]+)+)";    
 
  113             Pattern p2 = Pattern.compile(network, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
 
  114             Matcher m2 = p2.matcher(txt);
 
  122     public static long findID(Content dataSource, String path) {
 
  123         String parent_path = path.replace(
'\\', 
'/'); 
 
  124         if (parent_path.length() > 2 && parent_path.charAt(1) == 
':') {
 
  125             parent_path = parent_path.substring(2); 
 
  127         int index = parent_path.lastIndexOf(
'/');
 
  128         String name = parent_path.substring(++index);
 
  129         parent_path = parent_path.substring(0, index);
 
  130         List<AbstractFile> files = null;
 
  132             FileManager fileManager = Case.getCurrentCaseThrows().getServices().getFileManager();
 
  133             files = fileManager.findFiles(dataSource, name, parent_path);
 
  134         } 
catch (TskCoreException | NoCurrentCaseException ex) {
 
  135             logger.log(Level.WARNING, 
"Error fetching 'index.data' files for Internet Explorer history."); 
 
  138         if (files == null || files.isEmpty()) {
 
  141         return files.get(0).getId();
 
  144     public static boolean checkColumn(String column, String tablename, String connection) {
 
  145         String query = 
"PRAGMA table_info(" + tablename + 
")"; 
 
  146         boolean found = 
false;
 
  148         SQLiteDBConnect tempdbconnect = null;
 
  150             tempdbconnect = 
new SQLiteDBConnect(
"org.sqlite.JDBC", 
"jdbc:sqlite:" + connection); 
 
  151             temprs = tempdbconnect.executeQry(query);
 
  152             while (temprs.next()) {
 
  153                 if (temprs.getString(
"name") == null ? column == null : temprs.getString(
"name").equals(column)) { 
 
  157         } 
catch (Exception ex) {
 
  158             logger.log(Level.WARNING, 
"Error while trying to get columns from sqlite db." + connection, ex); 
 
  161             if (tempdbconnect != null) {
 
  162                 tempdbconnect.closeConnection();
 
  168     public static ResultSet runQuery(String query, String connection) {
 
  169         ResultSet results = null;
 
  171             SQLiteDBConnect tempdbconnect = 
new SQLiteDBConnect(
"org.sqlite.JDBC", 
"jdbc:sqlite:" + connection); 
 
  172             results = tempdbconnect.executeQry(query);
 
  173             tempdbconnect.closeConnection();
 
  174         } 
catch (Exception ex) {
 
  175             logger.log(Level.WARNING, 
"Error while trying to run sql query: " + query + 
" : " + connection, ex);