/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfrobot.engine.parser;
import java.io.File;
import java.io.Serializable;
import pdfrobot.engine.config.PdfFileNameFilter;
/**
*Represents a Pdf file rule.
* @author hedsttor
*/
public class PdfFileRule implements Serializable, Comparable<PdfFileRule> {
private int line;
private String pattern;
private File sourceFolder;
private File destinationSubFolder;
/**
* Constructor
* @param line The line where the pattern is found
* @param pattern The text pattern
* @param destinationSubFolder The destination sub folder
* @param sourceFolder The source folder
*/
public PdfFileRule(String line, String pattern, File destinationSubFolder, File sourceFolder) {
this.line = Integer.parseInt(line);
this.pattern = pattern;
this.destinationSubFolder = destinationSubFolder;
setSourceFolder(sourceFolder);
}
/**
* Returns the destination sub folder
* @return The destination sub folder
*/
public File getDestinationSubFolder() {
return destinationSubFolder;
}
/**
* Sets the destination sub folder
* @param destinationSubFolder The destination sub folder
*/
public void setDestinationSubFolder(File destinationSubFolder) {
this.destinationSubFolder = destinationSubFolder;
}
/**
* Returns the line where the pattern is found
* @return The line where the pattern is found
*/
public int getLine() {
return line;
}
/**
* Set the line where the pattern should be found
* @param line The line where the pattern should be found
*/
public void setLine(int line) {
this.line = line;
}
/**
* Returns the text pattern,
* @return The text pattern,
*/
public String getPattern() {
return pattern;
}
/**
* Sets the text pattern,
* @param pattern The text pattern,
*/
public void setPattern(String pattern) {
this.pattern = pattern;
}
/**
* Returns the source folder
* @return The source folder
*/
public File getSourceFolder() {
return sourceFolder;
}
/**
* Sets the source folder
* @param sourceFolder The source folder
*/
public void setSourceFolder(File sourceFolder) {
setAndValidateSourceFolder(sourceFolder);
}
/**
* Returns all pdf files from the sourcefolder connected to this rule.
* @return
*/
public File[] getFiles() {
return sourceFolder.listFiles(new PdfFileNameFilter());
}
public int compareTo(PdfFileRule o) {
boolean result = o.getLine() == this.line
& o.getPattern().equals(this.pattern)
& o.getDestinationSubFolder().equals(this.destinationSubFolder)
& o.getSourceFolder().equals(this.sourceFolder);
return result ? 0 : -1;
}
private void setAndValidateSourceFolder(File sourceFolder) {
if (!sourceFolder.isDirectory()) {
sourceFolder = sourceFolder.getParentFile();
}
this.sourceFolder = sourceFolder;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof PdfFileRule)) {
return false;
}
PdfFileRule comparedRule = (PdfFileRule) o;
return this.line == comparedRule.getLine() & this.pattern.equals(comparedRule.getPattern()) & this.sourceFolder.equals(comparedRule.getSourceFolder()) & this.destinationSubFolder.equals(comparedRule.getDestinationSubFolder());
}
@Override
public int hashCode() {
return line * pattern.hashCode() + sourceFolder.hashCode() + destinationSubFolder.hashCode();
}
@Override
public String toString() {
return line+"-"+pattern+"-"+sourceFolder+"-"+destinationSubFolder;
}
}