themeinstaller/source/src/com/nokia/tools/themeinstaller/localisation/FileSearch.java
branchRCL_3
changeset 32 fe49e33862e2
parent 31 b685c59de105
child 33 04b7640f6fb5
equal deleted inserted replaced
31:b685c59de105 32:fe49e33862e2
     1 /*
       
     2 * Copyright (c) 2007 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:  Searches files from given directories
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.localisation;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.FileFilter;
       
    23 import java.util.Vector;
       
    24 import java.util.regex.Matcher;
       
    25 import java.util.regex.Pattern;
       
    26 
       
    27 /**
       
    28  * Searches files that's name matches the regular expression.
       
    29  * Search operations can be executed in one or multiple directories
       
    30  * and also including subfolders in the search can activated.
       
    31  *
       
    32  * There are two versions for searching: recursive and non-recursive.
       
    33  * Recursive version is used when flooding the search to subfolders
       
    34  */
       
    35 public class FileSearch
       
    36     {
       
    37     private static Vector iFiles;
       
    38 
       
    39 
       
    40     /**
       
    41      * Find files in directories.
       
    42      *
       
    43      * @param aRootDirs The root directories where the search is initiated
       
    44      * @param aFileNameExpression The file name expression
       
    45      * @param aIncludeSubfolders Include subfolders in the search or not
       
    46      * @param aAllowSameName Allow same filenames
       
    47      *
       
    48      * @return Found files in a vector
       
    49      */
       
    50     public static Vector findFiles( Vector aRootDirs,
       
    51             String aFileNameExpression, boolean aIncludeSubfolders,
       
    52             boolean aAllowSame )
       
    53         {
       
    54         Vector allFiles = new Vector();
       
    55         Vector fileFromOneSearch = new Vector();
       
    56 
       
    57         for ( int i = 0; i < aRootDirs.size(); i++ )
       
    58             {
       
    59             fileFromOneSearch = findFiles( ( String ) aRootDirs.elementAt( i ),
       
    60                     aFileNameExpression, aIncludeSubfolders, aAllowSame );
       
    61 
       
    62             for ( int j = 0; j < fileFromOneSearch.size(); j++ )
       
    63                 {
       
    64                 if ( aAllowSame && !allFiles.contains( fileFromOneSearch.get( j ) ) )
       
    65                     {
       
    66                     allFiles.add( fileFromOneSearch.get( j ) );
       
    67                     }
       
    68                 if ( !aAllowSame
       
    69                         && !hasSameName( allFiles, ( File ) fileFromOneSearch.get( j ) ) )
       
    70                     {
       
    71                     allFiles.add( fileFromOneSearch.get( j ) );
       
    72 
       
    73                     }
       
    74                 }
       
    75             }
       
    76 
       
    77         return allFiles;
       
    78         }
       
    79 
       
    80     /**
       
    81      * Find files in directories.
       
    82      *
       
    83      * @param aRootDirs The root directories where the search is initiated
       
    84      * @param aFileNameExpression The file name expression
       
    85      * @param aIncludeSubfolders Include subfolders in the search or not
       
    86      *
       
    87      * @return Found files in a vector
       
    88      */
       
    89     public static Vector findFiles( Vector aRootDirs,
       
    90             String aFileNameExpression, boolean aIncludeSubfolders )
       
    91         {
       
    92         return findFiles( aRootDirs, aFileNameExpression, aIncludeSubfolders,
       
    93                 true );
       
    94         }
       
    95 
       
    96     /**
       
    97      * Find one file in directories. Returns the first one found.
       
    98      *
       
    99      * @param aRootDirs The root directories where the search is initiated
       
   100      * @param aFileNameExpression The file name expression
       
   101      * @param aIncludeSubfolders Include sub folders in the search or not
       
   102      *
       
   103      * @return Found file
       
   104      */
       
   105     public static File findFile( Vector aRootDirs,
       
   106             String aFileNameExpression, boolean aIncludeSubfolders )
       
   107         {
       
   108         Vector results = findFiles( aRootDirs, aFileNameExpression, aIncludeSubfolders,
       
   109                 true );
       
   110 
       
   111         if( results.size() == 0 )
       
   112             {
       
   113             throw new IllegalArgumentException( "Localisation: File Search: " +
       
   114             		"File not found: " + aFileNameExpression );
       
   115             }
       
   116 
       
   117         return ( File ) results.elementAt( 0 );
       
   118         }
       
   119 
       
   120     /**
       
   121      * Find files in directory.
       
   122      *
       
   123      * @param aRootDir The root directory where the search is initiated
       
   124      * @param aFileNameExpression The file name expression
       
   125      * @param aIncludeSubfolders Include subfolders in the search or not
       
   126      * @param aAllowSameName Allow same filenames
       
   127      *
       
   128      * @return Found files in a vector
       
   129      */
       
   130     public static Vector findFiles( String aRootDir,
       
   131             String aFileNameExpression, boolean aIncludeSubfolders,
       
   132             boolean aAllowSameName )
       
   133         {
       
   134 
       
   135         iFiles = new Vector();
       
   136         // If subfolders included, use recursive version
       
   137         if ( aIncludeSubfolders )
       
   138             {
       
   139             findFilesInSubfolder( new File( aRootDir ), aFileNameExpression,
       
   140                     aAllowSameName );
       
   141             }
       
   142         // If no subfolders included, use non-recursive version
       
   143         if ( !aIncludeSubfolders )
       
   144             {
       
   145             findFilesInFolder( new File( aRootDir ), aFileNameExpression,
       
   146                     aAllowSameName );
       
   147             }
       
   148 
       
   149         return iFiles;
       
   150 
       
   151         }
       
   152 
       
   153     /**
       
   154      * Find files in directory.
       
   155      *
       
   156      * @param aRootDir The root directory where the search is initiated
       
   157      * @param aFileNameExpression The file name expression
       
   158      * @param aIncludeSubfolders Include subfolders in the search or not
       
   159      *
       
   160      * @return Found files in a vector
       
   161      */
       
   162     public static Vector findFiles( String aRootDir,
       
   163             String aFileNameExpression, boolean aIncludeSubfolders )
       
   164         {
       
   165         return findFiles( aRootDir, aFileNameExpression, aIncludeSubfolders, true );
       
   166         }
       
   167 
       
   168     /**
       
   169      * Find files in folder. Non-recursive version
       
   170      *
       
   171      * @param dir Directory to search in
       
   172      * @param aFileNameExpression The file name expression
       
   173      * @param aAllowSameName Allow same filename
       
   174      */
       
   175     private static void findFilesInFolder( File dir,
       
   176             final String aFileNameExpression, boolean aAllowSameName )
       
   177         {
       
   178         // Condition that file must fulfill
       
   179         FileFilter fileFilter = new FileFilter()
       
   180             {
       
   181             public boolean accept( File file )
       
   182                 {
       
   183                 return ( !file.isDirectory() && match( aFileNameExpression,
       
   184                         file.getName() ) );
       
   185                 }
       
   186             };
       
   187 
       
   188         // Files that match the condition
       
   189         File[] files = dir.listFiles( fileFilter );
       
   190 
       
   191         for ( int i = 0; i < files.length; i++ )
       
   192             {
       
   193             if ( aAllowSameName )
       
   194                 {
       
   195                 iFiles.add( files[ i ] );
       
   196                 }
       
   197             if ( !aAllowSameName && !hasSameName( iFiles, files[ i ] ) )
       
   198                 {
       
   199                 iFiles.add( files[ i ] );
       
   200                 }
       
   201             }
       
   202 
       
   203         }
       
   204 
       
   205     /**
       
   206      * Find files in folder and it's subfolder. Recursive version
       
   207      *
       
   208      * @param dir Root directory to start search
       
   209      * @param aFileNameExpression the a file name expression
       
   210      * @param aAllowSameName Allow same filenames
       
   211      */
       
   212     private static void findFilesInSubfolder( File dir,
       
   213             final String aFileNameExpression, boolean aAllowSameName )
       
   214         {
       
   215 
       
   216         // Flood the search to subdirectories:
       
   217         if ( dir.isDirectory() )
       
   218             {
       
   219 
       
   220             File[] files = dir.listFiles();
       
   221 
       
   222             for ( int i = 0; i < files.length; i++ )
       
   223                 {
       
   224                 findFilesInSubfolder( files[ i ], aFileNameExpression,
       
   225                         aAllowSameName );
       
   226                 }
       
   227             }
       
   228         // If not directory, add files that match conditions
       
   229         else
       
   230             {
       
   231             if ( match( aFileNameExpression, dir.getName() ) )
       
   232                 {
       
   233                 if ( aAllowSameName )
       
   234                     {
       
   235                     iFiles.add( dir );
       
   236                     }
       
   237                 if ( !aAllowSameName && !hasSameName( iFiles, dir ) )
       
   238                     {
       
   239                     iFiles.add( dir );
       
   240                     }
       
   241 
       
   242                 }
       
   243             }
       
   244         }
       
   245 
       
   246     /**
       
   247      * Match regular expression with string.
       
   248      *
       
   249      * @param patternStr Regular expression for match
       
   250      * @param input String to compare the regular expression
       
   251      *
       
   252      * @return true, if input matches the regular expression
       
   253      */
       
   254     private static boolean match( String patternStr, CharSequence input )
       
   255         {
       
   256         Pattern pattern = Pattern.compile( patternStr );
       
   257         Matcher matcher = pattern.matcher( input );
       
   258         if ( matcher.matches() )
       
   259             {
       
   260             return true;
       
   261             }
       
   262         return false;
       
   263         }
       
   264 
       
   265     /**
       
   266      * Checks if vector already has file with same name.
       
   267      *
       
   268      * @param aFiles Files to check
       
   269      * @param aFile Reference file
       
   270      *
       
   271      * @return true, if vector already has file with same name
       
   272      */
       
   273     private static boolean hasSameName( Vector aFiles, File aFile )
       
   274         {
       
   275         for ( int i = 0; i < aFiles.size(); i++ )
       
   276             {
       
   277             if ( ( ( File ) aFiles.get( i ) ).getName()
       
   278                     .equals( aFile.getName() ) )
       
   279                 {
       
   280                 return true;
       
   281                 }
       
   282             }
       
   283         return false;
       
   284         }
       
   285 
       
   286     }