package eu.dnetlib.cnr.contract;

import java.io.StringReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import eu.dnetlib.contract.cp.CheckPointEvaluationException;
import eu.dnetlib.contract.cp.ICheckPoint;
import eu.dnetlib.contract.cp.eval.module.IEvaluatorModule;
import eu.dnetlib.contract.event.IContractEvent;
import eu.dnetlib.contract.node.EvaluationResult;
import eu.dnetlib.contract.node.EvaluationResult.Status;

public class ValidProfileEvaluationModule implements IEvaluatorModule<IContractEvent> {

	private static final Log log = LogFactory.getLog(ValidProfileEvaluationModule.class); // NOPMD by marko on 11/24/08 5:02 PM
	
	public EvaluationResult evaluate(Object inspObj, IContractEvent context, ICheckPoint<IContractEvent> checkPoint) throws CheckPointEvaluationException {
		if (inspObj!=null && inspObj instanceof String) {
			try {
				SAXReader xmlReader = new SAXReader();
				Document doc = xmlReader.read(new StringReader((String) inspObj));
				if (doc.valueOf("//RESOURCE_IDENTIFIER") != null) {
					return new EvaluationResult(Status.OK, checkPoint, context);
				}
			} catch (DocumentException e) {
				log.debug("Error parsing object: " + inspObj, e);
			}
		} 
		return new EvaluationResult(Status.FAIL, checkPoint, context, "Invalid and unsupported object value: " + inspObj);
	}
}
