All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.bpmn.model.io.AbstractErrorReportBuilder Maven / Gradle / Ivy

There is a newer version: 5.0-alpha6
Show newest version
package jadex.bpmn.model.io;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jadex.collection.MultiCollection;
import jadex.common.SUtil;
import jadex.common.Tuple;
import jadex.model.IErrorReport;

/**
 *  Helper class for building error reports.
 */
public abstract class AbstractErrorReportBuilder
{
	//-------- attributes --------
	
	/** The unqualified (model/file) name. */
	protected String	name;
	
	/** The file name with path (optional). */
	protected String	filename;
	
	/** The element categories. */
	protected String[] categories;
	
	/** The parse errors (tuple(elements) -> {error messages}). */
	protected MultiCollection	entries;
	
	/** The external documents for links in html error reports (id -> html text). */
	protected Map	externals;
		
	//-------- constructors --------
	
	/**
	 *  Build the error based on the given entries (if any).
	 *  Entries represent error messages mapped by the path to the
	 *  xml element (as a tuple of stack elements).
	 *  @param name	The unqualified (model/file) name.
	 *  @param filename	The file name with path (optional).
	 *  @param categories	The element categories.
	 *  @param entries	The parse errors (tuple(stack elements) -> {error messages}).
	 *  @param externals	The external documents for links in html error reports, if any (id -> html text).
	 */
	public AbstractErrorReportBuilder(String name, String filename, String[] categories, MultiCollection entries, Map externals)
	{
		this.name	= name;
		this.filename	= filename;
		this.categories	= categories;
		this.entries	= entries;
		this.externals	= externals;
	}
	
	//-------- methods --------
	
	/**
	 *  Build the error based on the given entries (if any).
	 *  Entries represent error messages mapped by the path to the
	 *  xml element (as a tuple of stack elements).
	 *  @return The error report.
	 */
	public IErrorReport	buildErrorReport()
	{
		IErrorReport report = entries==null || entries.size()==0 ? null
			: new ErrorReport(generateErrorText(), generateErrorHTML(), externals);
		return report;
	}
	
	//-------- template methods --------
	
	/**
	 *  Get the object of a path element
	 *  @param obj	An item (entry) of a tuple in the multi collection.
	 *  @return	The object corresponding to the entry.
	 */
	public abstract Object	getPathElementObject(Object element);
	
	/**
	 *  Test if an object belongs to a category.
	 *  @param obj	An item (entry) of a tuple in the multi collection.
	 *  @param category	the category name.
	 *  @return	True, when the object belongs to the category.
	 */
	public abstract boolean	isInCategory(Object obj, String category);
	
	/**
	 *  Get the name of an object.
	 *  @param obj	An object having an error.
	 *  @return	A human readable name of the object. 
	 */
	public abstract String	getObjectName(Object obj);

	//-------- helper methods --------
	
	/**
	 *  Get all invalid elements.
	 */
	protected Tuple[]	getElements()
	{
		if(entries==null)
			return new Tuple[0];
		else
			return (Tuple[])entries.getKeys(Tuple.class);
	}

	/**
	 *  Get the messages for a given element.
	 */
	protected String[]	getMessages(Tuple path)
	{
		if(entries==null)
		{
			return SUtil.EMPTY_STRING_ARRAY;
		}
		else
		{
			Collection	ret	= entries.getCollection(path);
			return (String[])ret.toArray(new String[ret.size()]);
		}
	}

	/**
	 *  Generate a string representation of the report.
	 */
	protected String generateErrorText()
	{
		StringBuffer buf = new StringBuffer();

		buf.append("Report for ");
		buf.append(name);
		buf.append("\n");
		if(filename!=null)
		{
			buf.append("File: ");
			buf.append(filename);
			buf.append("\n");
		}
		
		buf.append("\n");

		Tuple[]	elements	= getElements();
		for(int i=0; iReport for ");
		buf.append(name);
		buf.append("\n");
		if(filename!=null)
		{
			buf.append("File: ");
			buf.append(filename);
			buf.append("\n");
		}

		Set[]	catels	= new Set[categories.length];
		Set excludes	= new HashSet();
		for(int i=0; i	others	= getOtherErrors(excludes);

		
		// Summaries.
		buf.append("

Summary

\n