summaryrefslogtreecommitdiffstats
path: root/index/IndexCreator.java
blob: 76f8ebc4380ee2e1b37212997b68537b368fb271 (plain) (blame)
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
package scruf.index;

import java.io.*;
import java.util.regex.*;
import scruf.io.*;
import scruf.conversion.*;

public class IndexCreator {
    private File directory;
    private File index;
    private StringBuilder indexContent;
    // set to true, if index file is modified.
    boolean modified = false;
    public IndexCreator(File directory) {
	this.directory = directory;
	index = new File(directory,"index");
	indexContent = new StringBuilder();
	if(index.exists()) {
	    indexContent.append(new ReadFile(index).
				getContent());
	}
    }
    public void add(File file) {
	String fileName = file.getName();
	if(shouldAdd(fileName)) {
	    System.out.println("New Entry: "+fileName);
	    indexContent.append("[[./");
	    indexContent.append(fileName+".html");
	    indexContent.append("|");
	    indexContent.append(PresentFile.name);
	    indexContent.append("]]\n");
	    modified=true;
	}
    }
    public boolean write() {
	if(modified)
	    new WriteFile(index,indexContent.toString()).write();
	return modified;
    }
    public File indexFile() {
	return index;
    }
    private  boolean shouldAdd(String fileName) {
	String regex = ".*"+fileName+".*";
	// checks if fileName is already there in index.
	boolean check1 = !(Pattern.compile(regex).
			matcher(indexContent.toString()).find());
	// checks if fileName is index itself.
	boolean check2 = !(Pattern.matches(fileName,"index"));
	boolean add = (check1 && check2);
	return add;
    }
}