package de.morphbit.thymeleaf.dialect;

import java.util.HashSet;
import java.util.Set;

import org.thymeleaf.dialect.AbstractProcessorDialect;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.standard.StandardDialect;

import de.morphbit.thymeleaf.model.ThymeleafComponent;
import de.morphbit.thymeleaf.processor.ComponentNamedElementProcessor;
import de.morphbit.thymeleaf.processor.OnceAttributeTagProcessor;

public class ComponentDialect extends AbstractProcessorDialect {

	public static final String NAME = "Component Dialect";
	public static final String PREFIX = "tc";
	public static final int PRECEDENCE = 1000;

	private final Set<ThymeleafComponent> components;

	public ComponentDialect() {
		this(null);
	}

	public ComponentDialect(Set<ThymeleafComponent> components) {
		super(NAME, PREFIX, PRECEDENCE);
		this.components = components;
	}

	@Override
	public Set<IProcessor> getProcessors(String dialectPrefix) {
		Set<IProcessor> processors = new HashSet<>();
		processors.add(new OnceAttributeTagProcessor(dialectPrefix));

		if (this.components != null) {
			for (ThymeleafComponent comp : this.components) {
				processors.add(new ComponentNamedElementProcessor(dialectPrefix,
				    comp.getName(), comp.getFragmentTemplate()));
			}
		}
		return processors;
	}

}

@@ -50,22 +50,73 @@
 va.util.
-HashSe
+ArrayList;%0Aimport java.util.HashSet;%0Aimport java.util.Lis
 t;%0Aimpor
@@ -234,55 +234,8 @@
 sor;
-%0Aimport org.thymeleaf.standard.StandardDialect;
 %0A%0Aim
@@ -423,16 +423,80 @@
 ocessor;
+%0Aimport de.morphbit.thymeleaf.service.IThymeleafComponentParser;
 %0A%0Apublic
@@ -749,16 +749,92 @@
 ponents;
+%0A%09private final List%3CIThymeleafComponentParser%3E parsers = new ArrayList%3C%3E();
 %0A%0A%09publi
@@ -1416,31 +1416,555 @@
 %09%09%7D%0A
-%09%09return processors;%0A%09%7D%0A
+%0A%09%09for (ThymeleafComponent comp : parseComponents()) %7B%0A%09%09%09processors.add(new ComponentNamedElementProcessor(dialectPrefix,%0A%09%09%09    comp.getName(), comp.getFragmentTemplate()));%0A%09%09%7D%0A%0A%09%09return processors;%0A%09%7D%0A%0A%09private Set%3CThymeleafComponent%3E parseComponents() %7B%0A%09%09Set%3CThymeleafComponent%3E parsedComponents = new HashSet%3C%3E();%0A%09%09for (IThymeleafComponentParser parser : this.parsers) %7B%0A%09%09%09parsedComponents.addAll(parser.parse());%0A%09%09%7D%0A%0A%09%09return parsedComponents;%0A%09%7D%0A%0A%09public void addParser(IThymeleafComponentParser parser) %7B%0A%09%09this.parsers.add(parser);%0A%09%7D
 %0A%7D%0A

Add possibility to register parser