package org.mustangproject.validator;

import org.mustangproject.XMLTools;

/***
 * an entry in a validation result, usually a warning or an error, along with some data like location
 */
public class ValidationResultItem {
	protected String message, location;
	protected int section = -1;
	protected String id = ""; // e.g. "FX-SCH-A-000026"


	private ESeverity severity = ESeverity.error;
	private String criterion;


	private String stacktrace;
	private boolean hasBeenOutputted;


	private EPart part;
	private XMLTools xt;

	public ValidationResultItem(ESeverity sev, String msg) {
		setSeverity(sev);
		setMessage(msg);
		xt = new XMLTools();
	}
	public ValidationResultItem setMessage(String msg) {
		message = msg;
		return this;
	}

	public ValidationResultItem setSection(int sec) {
		section = sec;
		return this;
	}
	public ValidationResultItem setLocation(String loc) {
		location = loc;
		return this;
	}
	public ValidationResultItem setPart(EPart loc) {
		part = loc;
		return this;
	}
	public EPart getPart() {
		return part;
	}
	public ValidationResultItem setSeverity(ESeverity sev) {
		severity = sev;
		return this;
	}
	public ValidationResultItem setStacktrace(String stack) {
		stacktrace = stack;
		return this;
	}


	public String getXML() {
		String tagname = "error";
		if (severity == ESeverity.exception) {
			tagname = "exception";
		} else if (severity == ESeverity.warning) {
			tagname = "warning";
		} else if (severity == ESeverity.notice) {
			tagname = "notice";
		}
		String additionalAttributes = "";
		String additionalContents = "";
		if (section != -1) {
			additionalAttributes += " type=\"" + section + "\"";
		}
		if (location != null) {
			additionalAttributes += " location=\"" + xt.escapeAttributeEntities(location) + "\"";
		}
		if (criterion != null) {
			additionalAttributes += " criterion=\"" + xt.escapeAttributeEntities(criterion) + "\"";
		}
		if (stacktrace != null) {
			additionalContents += "<stacktrace>" + xt.escapeAttributeEntities(stacktrace) + "</stacktrace>";
		}
		hasBeenOutputted = true;
		return "<" + tagname + additionalAttributes + ">" + xt.escapeElementEntities(message + additionalContents) + "</" + tagname + ">";
	}

	public String getXMLOnce() {
		if (!hasBeenOutputted) {
			return getXML();
		} else {
			return "";
		}
	}

	public ValidationResultItem setCriterion(String test) {
		 criterion = test;
		 return this;
	}
	public ESeverity getSeverity() {
		return severity;
	}

	public ValidationResultItem setID(String id) {
		this.id = id;
		return this;
	}

	public String getID() {
		return id;
	}

	public int getSection() {
		return section;
	}

	public String getMessage() {
		return message;
	}

	public String getLocation() {
		return location;
	}

}
