package eu.dnetlib.data.utility.cleaner;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Node;
import org.springframework.beans.factory.annotation.Required;

import eu.dnetlib.data.utility.cleaner.rmi.CleanerException;

public abstract class XPATHCleaningRule {

	private String xpath;

	public void applyXpathRule(Document doc, String context) throws CleanerException {
//		StopWatch stopWatch = new CommonsLogStopWatch(log);
		@SuppressWarnings("rawtypes")
		final List nodes = doc.selectNodes(xpath);
//		stopWatch.stop("xpath.execution");
		
		for (Object o : nodes) {
			Node node = (Node) o;
			node.setText(calculateNewValue(node.getText().trim(), context));
		}
	}

	protected abstract String calculateNewValue(String oldValue, String context) throws CleanerException;

	@Required
	public void setXpath(String xpath) {
		this.xpath = xpath;
	}

}
