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

org.magicwerk.brownies.html.tools.HtmlContextPrinter Maven / Gradle / Ivy

The newest version!
/**
 * 
 */
package org.magicwerk.brownies.html.tools;

import java.util.function.Consumer;

import org.jdom2.Attribute;
import org.magicwerk.brownies.collections.IList;
import org.magicwerk.brownies.core.CheckTools;
import org.magicwerk.brownies.core.collections.GridSelection;
import org.magicwerk.brownies.core.collections.Tree;
import org.magicwerk.brownies.core.collections.TreeTools;
import org.magicwerk.brownies.core.context.Context;
import org.magicwerk.brownies.core.files.FilePath;
import org.magicwerk.brownies.core.function.Predicates;
import org.magicwerk.brownies.core.status.Severity;
import org.magicwerk.brownies.core.status.Status;
import org.magicwerk.brownies.core.strings.StringFormatter;
import org.magicwerk.brownies.core.types.Type;
import org.magicwerk.brownies.core.values.ITableModel;
import org.magicwerk.brownies.core.values.Record;
import org.magicwerk.brownies.core.values.RecordType;
import org.magicwerk.brownies.core.values.RecordTypeBuilder;
import org.magicwerk.brownies.core.values.TableTools;
import org.magicwerk.brownies.html.HtmlConst;
import org.magicwerk.brownies.html.HtmlDoclet;
import org.magicwerk.brownies.html.HtmlReport;
import org.magicwerk.brownies.html.HtmlResource;
import org.magicwerk.brownies.html.HtmlTable;
import org.magicwerk.brownies.html.StyleResource;
import org.magicwerk.brownies.html.content.HtmlAwesome;
import org.magicwerk.brownies.html.content.HtmlFormatters;
import org.magicwerk.brownies.html.content.HtmlFormatters.ConditionalFormatter;
import org.magicwerk.brownies.html.content.HtmlFormatters.CssClassFormatter;
import org.magicwerk.brownies.html.content.HtmlTableFormatter;
import org.magicwerk.brownies.html.content.HtmlTreeTableFormatter;

/**
 * Class {@link HtmlContextPrinter} renders the output of the context as HTML.
 * 
 * @author Thomas Mauch
 */
public class HtmlContextPrinter implements Consumer {

	/** Mode how context output should be rendered */
	public enum Mode {
		TREE_TABLE,
		TABLE
	}

	//@formatter:off
	static final String CSS_STYLE = StringFormatter.format(
			//".INFO {{ background-color: {} !important; } \n" +
			".WARN {{ background-color: {} !important; } \n" +
			".ERROR {{ background-color: {} !important; } \n",
			//HtmlAwesome.BACKGROUND_COLOR_INFO, 
			HtmlAwesome.BACKGROUND_COLOR_WARNING, 
			HtmlAwesome.BACKGROUND_COLOR_ERROR
	);
	//@formatter:on

	// Configuration
	/** Mode how context output should be rendered */
	Mode mode = Mode.TREE_TABLE;

	boolean showHtmlFile;
	/** File to use for HTML report, null for creating a temporary file */
	FilePath htmlFile;

	// State
	RecordType recordType;

	//

	/** Setter for {@link #mode} */
	public HtmlContextPrinter setMode(Mode mode) {
		this.mode = mode;
		return this;
	}

	/** Setter for {@link #showHtmlFile} */
	public HtmlContextPrinter setShowHtmlFile(boolean showHtmlFile) {
		this.showHtmlFile = showHtmlFile;
		return this;
	}

	/** Setter for {@link #htmlFile} */
	public HtmlContextPrinter setHtmlFile(FilePath htmlFile) {
		this.htmlFile = htmlFile;
		return this;
	}

	@Override
	public void accept(Context ctx) {
		HtmlReport report = new HtmlReport();

		HtmlResource hr = new HtmlResource("");
		hr.getHead().addStyleCss(CSS_STYLE);
		report.add(hr);

		report.add(StyleResource.INSTANCE);

		recordType = createRecordType();

		if (mode == Mode.TABLE) {
			IList tt = ctx.getAllStatusList();
			ITableModel t = TableTools.toTable(tt, this::createRecord);

			ConditionalFormatter cf = new ConditionalFormatter();
			cf.add(c -> {
				int row = c.getRow();
				if (row == -1) {
					return false; // header row
				}
				Status s = tt.get(row);
				return s.getSeverity() == Severity.ERROR || s.getSeverity() == Severity.WARN;
			}, c -> {
				Status s = tt.get(c.getRow());
				return new Attribute(HtmlConst.ATTR_CLASS, s.getSeverity().toString());
			});

			HtmlFormatters hf = new HtmlFormatters();
			hf.addFormatter(GridSelection.Table(), cf);

			HtmlTableFormatter htf = new HtmlTableFormatter();
			htf.setFormatters(hf);
			htf.setActive(true);
			htf.format(t);
			HtmlTable ttt = htf.format(t);

			report.add(HtmlTableFormatter.getHtmlResources());
			report.add(ttt);

		} else if (mode == Mode.TREE_TABLE) {
			Tree t = ctx.allStatuses();
			Tree tt = TreeTools.convert(t, this::createRecord);
			IList> list = tt.getDescendantNodes(true);

			ConditionalFormatter cf = new ConditionalFormatter();
			cf.add(Predicates.allow(), c -> {
				Tree t2 = list.get(c.getRow());
				String s = t2.getValue().getObject("Severity").toString();
				return new CssClassFormatter(s).format(c);
			});

			HtmlFormatters hf = new HtmlFormatters();
			hf.addFormatter(GridSelection.Table(), cf);

			HtmlTreeTableFormatter fmt = new HtmlTreeTableFormatter();
			fmt.setFormatters(hf);
			fmt.setTree(tt);

			HtmlDoclet hd = fmt.getTable();
			report.add(hd);

		} else {
			CheckTools.error("Unsupported mode: {}", mode);
		}

		if (htmlFile != null) {
			report.setHtmlFile(htmlFile.getFilePath());
		}
		if (showHtmlFile) {
			report.showHtml();
		} else {
			report.createHtml();
		}
	}

	RecordType createRecordType() {
		RecordTypeBuilder rtb = new RecordTypeBuilder();
		if (mode == Mode.TREE_TABLE) {
			rtb.add("", Type.NULL_TYPE);
		}
		rtb.add("Severity", Type.fromClass(Severity.class));
		rtb.add("Message", Type.STRING_TYPE);
		return rtb.toRecordType();
	}

	Record createRecord(Status status) {
		int col = (mode == Mode.TABLE) ? 0 : 1;
		Record rec = new Record(recordType);
		rec.setObject(col + 0, status.getSeverity());
		rec.setObject(col + 1, status.getMessage());
		return rec;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy