package pete.metrics.adaptability;

import java.nio.file.InvalidPathException;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import pete.executables.AnalysisException;
import de.uniba.wiai.lspi.ws1213.ba.application.BPMNReferenceValidator;
import de.uniba.wiai.lspi.ws1213.ba.application.BPMNReferenceValidatorImpl;
import de.uniba.wiai.lspi.ws1213.ba.application.ValidationResult;
import de.uniba.wiai.lspi.ws1213.ba.application.ValidatorException;

public final class BpmnInspector {

	private static final String BPMN_NAMESPACE = "http://www.omg.org/spec/BPMN/20100524/MODEL";

	private BPMNReferenceValidator referenceValidator;

	public BpmnInspector(){
		try {
			referenceValidator = new BPMNReferenceValidatorImpl();
		} catch (ValidatorException e) {
			throw new AnalysisException(e);
		}
	}

	String isCorrectNamespace(Document dom) {
		Element root = null;
		try {
			root = dom.getDocumentElement();
		} catch (NullPointerException e) {
			return "false";
		}
		if (BPMN_NAMESPACE.equals(root.getNamespaceURI())) {
			return "true";
		} else {
			return "false";
		}
	}

	String isExecutable(Node process) {
		NamedNodeMap attributes = process.getAttributes();

		for (int j = 0; j < attributes.getLength(); j++) {
			Node attribute = attributes.item(j);
			if ("isExecutable".equals(attribute.getLocalName())
					&& "true".equals(attribute.getNodeValue())) {
				return "true";
			}
		}

		return "false";
	}

	String hasReferenceIssues(String path){
		List<ValidationResult> results;
		try {
			results = referenceValidator.validate(path);
		} catch (ValidatorException | InvalidPathException | NullPointerException e) {
			System.err.println(e.getMessage());
			return "false";
		}

		if(results.size() > 0){
			return "false";
		} else{
			return "true";
		}
	}

	int getNumberOfChildren(Node node) {
		if (node.getLocalName() == null) {
			return 0;
		}

		NodeList children = node.getChildNodes();
		int result = 1;

		if (children == null) {
			// do noting, result is zero
		} else {
			for (int i = 0; i < children.getLength(); i++) {
				result += getNumberOfChildren(children.item(i));
			}
		}
		return result;
	}

	public Node getProcess(Document dom) throws AnalysisException {
		NodeList nodes = getChildrenOfDefinitions(dom);
		if (nodes == null) {
			throw new AnalysisException(
					"File cannot be analyzed: no definitions element or definitions element is empty");
		}
		for (int i = 0; i < nodes.getLength(); i++) {
			Node node = nodes.item(i);
			if ("process".equals(node.getLocalName())) {
				return node;
			}
		}
		throw new AnalysisException(
				"File cannot be analyzed: no process element found");
	}

	private NodeList getChildrenOfDefinitions(Document dom) {
		NodeList nodes = null;
		try {
			nodes = dom.getChildNodes();
		} catch (NullPointerException e) {
			return null;
		}
		for (int i = 0; i < nodes.getLength(); i++) {
			Node node = nodes.item(i);
			if ("definitions".equals(node.getLocalName())) {
				return node.getChildNodes();
			}
		}
		return null;
	}

}
@@ -75,31 +75,8 @@
 ion;
-%0Aimport java.util.List;
 %0A%0Aim
@@ -1565,13 +1565,8 @@
 %7B%0A%09%09
-List%3C
 Vali
@@ -1581,17 +1581,15 @@
 sult
-%3E
  result
-s
 ;%0A%09%09
@@ -1603,17 +1603,16 @@
 %09%09result
-s
  = refer
@@ -1633,16 +1633,26 @@
 validate
+SingleFile
 (path);%0A
@@ -1804,26 +1804,25 @@
 %09if(
+!
 result
-s.size() %3E 0
+.isValid()
 )%7B%0A%09

Use single file validation only