summaryrefslogtreecommitdiff
path: root/removed-code/FileFinder.java
blob: 2d6527cd2580abee1e16fc7a9ee7a13e84133b15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/***********************************************************************
 *
 * Copyright (c) 2006 Manuel Traut and Volker Dahnke
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 *
 * Contributors: Manuel Traut and Volker Dahnke
 *
 ***********************************************************************/
package YalpServer;

import java.io.*;
import java.util.*;

/************************************************************************
 * 
 * Class FileFinder
 * 
 * <em>algorithm for scanning files recursively</em>
 * 
 * @author Volker Dahnke / Manuel Traut
 * 
 * @version 0.6 14-12-2005<br>
 * 
 * @see FileBrowser
 * 
 ************************************************************************/

public class FileFinder
{
  private ArrayList<File> files;

 /**
  * Constructor: scans subdirectories of commited directory
  * 			 uses only files with allowed extensions
  * @param start
  * 	 directory to scan
  * @param extensions
  * 	 allowed extensions
  */
  public FileFinder( String start, String extensions[] ) {
	  
	  this.files = new ArrayList<File>();
	  Stack<File> dirs = new Stack<File>();
	  File startdir = new File(start);

	  // push startdir to stack
	  if (startdir.isDirectory()) dirs.push(startdir);
	  // startdir is File
	  else {
		  if (match(startdir.getName(), extensions)) this.files.add(startdir);
		  return;
	  }
	  // for each dir on stack
	  while (dirs.size() > 0) {
		  // contents of dir on stack
		  for (File file : dirs.pop().listFiles()){
            try {  
			  // add subdirectory to stack
			  if (file.isDirectory()) dirs.push(file);
			  // if file is of correct filetype add it to filelist
			  else if (match(file.getName(), extensions)) this.files.add(file);
		    } catch (NullPointerException e) {
			  System.out.println("FileFinder: "+ file.getName() +"Premission denied");
		    }
		  }
	  }
  }

 /**
  * returns and prints out all Medias found
  * @return ArrayList<File> 
  * 	all found Medias
  */ 
  public ArrayList<File> getFiles(){
	  print();
	  return this.files;
  }
  
 /**
  * prints out found medias
  *
  */ 
  public void print() {
    System.out.println( "Found " + files.size() + " file" + (files.size() == 1 ? "." : "s.") );
    for ( File f : files ) System.out.println( f.getAbsolutePath() );
  }

 /**
  * checks if file extension matches or not
  * 
  * @param s
  * 	 file to check
  * @param suffixes
  * 	 allowed extensions
  * 
  * @return boolean: true if it's a media, yalp can handle
  */ 
  private static boolean match( String s, String suffixes[] ) {
    for ( String suffix : suffixes ) {
      int huhu = s.length();
      int huhu2 = suffix.length();
      int huhu3 = huhu - huhu2;
      if ( s.length() >= suffix.length() && s.substring(huhu3, s.length()).equalsIgnoreCase(suffix) ) return true;
    }
    return false;
  }
}