sysperfana/memspyext/com.nokia.s60tools.memspy/src/com/nokia/s60tools/memspy/files/MemSpyFile.java
changeset 7 8e12a575a9b5
equal deleted inserted replaced
6:f65f740e69f9 7:8e12a575a9b5
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.s60tools.memspy.files;
       
    19 
       
    20 import java.io.File;
       
    21 import java.text.SimpleDateFormat;
       
    22 import java.util.ArrayList;
       
    23 import java.util.Date;
       
    24 
       
    25 import org.xml.sax.helpers.DefaultHandler;
       
    26 
       
    27 import com.nokia.s60tools.memspy.model.MemSpyFileOperations;
       
    28 
       
    29 /**
       
    30  * This is a base class for all MemSpy file types. 
       
    31  *
       
    32  */
       
    33 public class MemSpyFile extends DefaultHandler{
       
    34 
       
    35 	/* files path */
       
    36 	protected String filePath = "";
       
    37 	
       
    38 	/* files name */
       
    39 	protected String fileName = "";
       
    40 	
       
    41 	/* time code from file */
       
    42 	protected String time = "";
       
    43 
       
    44 	/* date time code from file */
       
    45 	private Date dateTime = null;
       
    46 	
       
    47 	/* type of the file  */
       
    48 	protected String fileType = "";
       
    49 	
       
    50 	/**
       
    51 	 * MemSpyFile.
       
    52 	 * constructor
       
    53 	 * @param filePath Path of file
       
    54 	 */
       
    55 	public MemSpyFile( String filePath ){
       
    56 		this.filePath = filePath;
       
    57 	}
       
    58 	
       
    59 	/**
       
    60 	 * doRead.
       
    61 	 * Read's file variables from file that is defined in filePath
       
    62 	 */
       
    63 	public void doRead(){
       
    64 		File f = new File(filePath);
       
    65 		
       
    66 		//if file exists
       
    67 		if (f.exists() && f.isFile()) {
       
    68 			
       
    69 			//Get date
       
    70 			Date date = new Date(f.lastModified());
       
    71 			setDateTime(date);
       
    72 			SimpleDateFormat formatter = new SimpleDateFormat ( MemSpyFileOperations.DATEFORMAT );
       
    73 			time = formatter.format(date);
       
    74 			
       
    75 			// get File name
       
    76 			fileName = f.getName();
       
    77 		} else {
       
    78 			fileName = "";
       
    79 		}
       
    80 	}
       
    81 	
       
    82 	/**
       
    83 	 * findFiles.
       
    84 	 * Finds all files with defined extension.
       
    85 	 * @param folder folder where from files are searched
       
    86 	 * @param extension file extension
       
    87 	 * @return arraylist that contains found file names
       
    88 	 */
       
    89 	protected static ArrayList<String> findFiles(String folder, String extension) {
       
    90 		
       
    91 		ArrayList<String> retVal = new ArrayList<String>();
       
    92 		if ( !extension.startsWith(".") ){
       
    93 			extension = "." + extension;
       
    94 		}
       
    95 		File cFolder = new File(folder);
       
    96 		if (cFolder.isDirectory() && cFolder.exists()) {
       
    97 			File[] files = cFolder.listFiles();
       
    98 			if (files != null) {
       
    99 				for (int i = 0; i < files.length; i++) {
       
   100 					File file = files[i];
       
   101 					if (file.getName().endsWith(extension)) {
       
   102 						retVal.add( file.getAbsolutePath() );
       
   103 					}
       
   104 				}
       
   105 			}
       
   106 		}
       
   107 		
       
   108 		return retVal;
       
   109 	}
       
   110 	
       
   111 	/**
       
   112 	 * findFile
       
   113 	 * Finds file with defined extension.
       
   114 	 * @param folder folder where file is searched
       
   115 	 * @param extension file extension
       
   116 	 * @return fileName
       
   117 	 */
       
   118 	protected static String findFile(String folder, String extension) {
       
   119 		if ( !extension.startsWith(".") ){
       
   120 			extension = "." + extension;
       
   121 		}
       
   122 		File cFolder = new File(folder);
       
   123 		if (cFolder.isDirectory() && cFolder.exists()) {
       
   124 			File[] files = cFolder.listFiles();
       
   125 			if (files != null) {
       
   126 				for (int i = 0; i < files.length; i++) {
       
   127 					File file = files[i];
       
   128 					if (file.getName().endsWith(extension)) {
       
   129 						return file.getAbsolutePath();
       
   130 					}
       
   131 				}
       
   132 			}
       
   133 		}
       
   134 		
       
   135 		return null;
       
   136 	}
       
   137 	
       
   138 	
       
   139 	
       
   140 	/**
       
   141 	 * Get file name
       
   142 	 * @return file name
       
   143 	 */
       
   144 	public String getFileName() {
       
   145 		return fileName;
       
   146 	}
       
   147 	/**
       
   148 	 * Set file name
       
   149 	 * @param fileName
       
   150 	 */
       
   151 	public void setFileName(String fileName) {
       
   152 		this.fileName = fileName;
       
   153 	}
       
   154 	/**
       
   155 	 * Get time when log file was created
       
   156 	 * @return time
       
   157 	 */
       
   158 	public String getTime() {
       
   159 		return time;
       
   160 	}
       
   161 	/**
       
   162 	 * Set time when log file was created
       
   163 	 * @param time
       
   164 	 */
       
   165 	public void setTime(String time) {
       
   166 		this.time = time;
       
   167 	}
       
   168 	/**
       
   169 	 * Get type of log file
       
   170 	 * @return type
       
   171 	 */
       
   172 	public String getFileType() {
       
   173 		return fileType;
       
   174 	}
       
   175 	/**
       
   176 	 * Set type of log file
       
   177 	 * @param fileType
       
   178 	 */
       
   179 	public void setFileType(String fileType) {
       
   180 		this.fileType = fileType;
       
   181 	}
       
   182 
       
   183 	/**
       
   184 	 * Get absolute file path
       
   185 	 * @return file path
       
   186 	 */
       
   187 	public String getFilePath() {
       
   188 		return filePath;
       
   189 	}
       
   190 
       
   191 	/**
       
   192 	 * Set date time
       
   193 	 * @param dateTime the dateTime to set
       
   194 	 */
       
   195 	private void setDateTime(Date dateTime) {
       
   196 		this.dateTime = dateTime;
       
   197 	}
       
   198 
       
   199 	/**
       
   200 	 * Get date time read from file
       
   201 	 * @return the dateTime
       
   202 	 */
       
   203 	public Date getDateTime() {
       
   204 		return dateTime;
       
   205 	} 
       
   206 	
       
   207 	
       
   208 
       
   209 }