package eu.dnetlib.data.utility.cleaner;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.perf4j.StopWatch;
import org.perf4j.commonslog.CommonsLogStopWatch;
import org.springframework.beans.factory.annotation.Required;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

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

public abstract class XPATHCleaningRule {
	private static final Log log = LogFactory.getLog(XPATHCleaningRule.class); // NOPMD by marko on 11/24/08 5:02 PM

	private String xpath;

	public void applyXpathRule(Document doc, String context) throws CleanerException {
		StopWatch stopWatch = new CommonsLogStopWatch(log);

		XPath xp = XPathFactory.newInstance().newXPath();
		NodeList nodes;
		try {
			nodes = (NodeList) xp.evaluate(xpath, doc, XPathConstants.NODESET);

			stopWatch.stop("xpath.execution");

			for (int i = 0; i < nodes.getLength(); i++) {
				Node node = nodes.item(i);
				if (node instanceof Attr) {
					node.setNodeValue(calculateNewValue(node.getNodeValue().trim(), context));
				} else if (node instanceof Element) {
					node.setTextContent(calculateNewValue(node.getTextContent().trim(), context));
				} else {
					// boh
				}
			}
		} catch (XPathExpressionException e) {
			throw new IllegalStateException(e);
		}

	}

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

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

}
