| Home >> All |
Source code: DirectoryIndexer/DirectoryIndexer.java
1 package DirectoryIndexer; 2 3 import java.io.*; 4 import java.util.*; 5 6 /* Example directories.cfg: 7 8 9 10 */ 11 12 public class DirectoryIndexer 13 implements Serializable 14 { 15 private ArrayList resource_array = null; 16 17 // Create a DirectoryIndexer object. Use file as the config file. 18 public DirectoryIndexer(String file) throws Exception 19 { 20 resource_array = new ArrayList(); 21 22 BufferedReader in = new BufferedReader(new FileReader(file)); 23 24 while(in.ready()) 25 { 26 String line = in.readLine(); 27 28 if(line.startsWith("#")) continue; 29 30 StringTokenizer st = new StringTokenizer(line, "\t"); 31 32 if(st.countTokens() != 4) continue; // If the line doesn't break into the 4 correct categories correctly, continue. 33 else 34 { 35 // If the entry looks okay, add it to the resource_array ArrayList. 36 resource_array.add(new Resource(st.nextToken(), st.nextToken(), st.nextToken(), Integer.parseInt(st.nextToken()))); 37 } 38 } 39 40 in.close(); 41 } 42 43 public void add(Resource r) 44 { 45 resource_array.add(r); 46 } 47 48 public boolean contains(Resource r) 49 { 50 boolean contains = false; 51 52 for(int i = 0; i < size(); i++) 53 { 54 if(get(i).equals(r)) contains = true; 55 } 56 57 return contains; 58 } 59 60 public Resource get(int n) 61 { 62 return (Resource) resource_array.get(n); 63 } 64 65 // Merge di into this DirectoryIndexer object. 66 public void merge(DirectoryIndexer directory_index) 67 { 68 for(int i = 0; i < directory_index.resource_array.size(); i++) 69 { 70 Resource r = (Resource) directory_index.resource_array.get(i); 71 72 if(contains(r) == false) add(r); 73 } 74 } 75 76 public int size() { return resource_array.size(); } 77 78 public String toString() 79 { 80 StringBuffer sb = new StringBuffer(); 81 82 sb.append("Number of Resources: " + size() + "\n"); 83 84 int i = 0; 85 for(i = 0; i < size() - 1; i++) 86 { 87 sb.append(((Resource) get(i)).toString() + "\n"); 88 } 89 90 if(size() > 0) sb.append(((Resource) get(i)).toString()); 91 92 return sb.toString(); 93 } 94 }