--- /dev/null
+package de.juplo.thymeleaf;
+
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonToken;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import java.io.InputStream;
+import java.time.format.DateTimeFormatter;
+import java.util.Locale;
+import java.util.Map.Entry;
+import java.util.logging.Level;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Configurable;
+import org.springframework.context.MessageSource;
+import org.thymeleaf.Arguments;
+import org.thymeleaf.Configuration;
+import org.thymeleaf.TemplateProcessingParameters;
+import org.thymeleaf.context.IContext;
+import org.thymeleaf.dom.Element;
+import org.thymeleaf.processor.ProcessorResult;
+import org.thymeleaf.processor.attr.AbstractAttrProcessor;
+import org.thymeleaf.resourceresolver.IResourceResolver;
+import org.thymeleaf.standard.expression.IStandardExpression;
+import org.thymeleaf.templateresolver.ITemplateResolver;
+import org.thymeleaf.templateresolver.TemplateResolution;
+import org.thymeleaf.util.StringUtils;
+
+
+
+/**
+ *
+ * @author kai
+ */
+@Configurable
+public class ImportVariablesAttrProcessor extends AbstractAttrProcessor
+{
+ public static final int ATTR_PRECEDENCE = 200;
+ public static final String ATTR_VAR_NAME =
+ JuploDialect.DIALECT_PREFIX + ":var";
+ public static final String ATTR_LOCALE_NAME =
+ JuploDialect.DIALECT_PREFIX + ":locale";
+ public static final String DEFAULT_VAR_NAME = "crumb";
+
+ private static final Logger LOG =
+ LoggerFactory.getLogger(ImportVariablesAttrProcessor.class);
+ private static final DateTimeFormatter FORMATTER =
+ DateTimeFormatter.ofPattern("dd.MM");
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+ private static final JsonFactory FACTORY = new JsonFactory();
+
+
+ @Autowired
+ MessageSource messageSource;
+ @Autowired
+ Locale defaultLocale;
+
+
+ public ImportVariablesAttrProcessor()
+ {
+ super("crumb");
+ }
+
+
+ @Override
+ public final ProcessorResult processAttribute(
+ final Arguments arguments,
+ final Element element,
+ final String name
+ )
+ {
+ Configuration config = arguments.getConfiguration();
+ String templateName = element.getAttributeValue(name);
+
+ TemplateProcessingParameters params =
+ new TemplateProcessingParameters(
+ config,
+ templateName,
+ (IContext)null // << We will not execute the template, hence we need no context
+ );
+
+ for (ITemplateResolver t_resolver : config.getTemplateResolvers())
+ {
+ TemplateResolution resolution = t_resolver.resolveTemplate(params);
+ if (resolution == null)
+ continue;
+ if (!"JSON".equals(resolution.getTemplateMode()))
+ continue;
+ IResourceResolver r_resolver = resolution.getResourceResolver();
+ InputStream is = r_resolver.getResourceAsStream(params, templateName);
+ if (is == null)
+ continue;
+
+ try
+ {
+ /** Read the JSON and create the variables */
+ JsonParser parser = FACTORY.createParser(is);
+
+ JsonToken root = parser.nextToken();
+
+
+ JsonNode root = MAPPER.readTree(is);
+
+ if (root == null)
+ {
+ LOG.warn("found empty content for {}", templateName);
+ break;
+ }
+
+ if (!root.isObject())
+ {
+ LOG.error("{} must contain an object as root-element", templateName);
+ throw new RuntimeException(
+ "The root-element of " +
+ templateName +
+ " has to be an object, that contains the variable-definitions!"
+ );
+ }
+root.
+ for (Entry<String, JsonNode> entry : root.fields())
+ {
+ Object var = ImportVariablesAttrProcessor.convert(root.get(i));
+ element.setNodeLocalVariable(name, var);
+ }
+ }
+ catch (IOException e)
+ {
+ LOG.error("cannot parse {} as JSON: {}", templateName, e.getMessage());
+ throw new RuntimeException(e);
+ }
+ }
+
+ element.removeAttribute(name);
+
+ return ProcessorResult.OK;
+ }
+
+
+ @Override
+ public int getPrecedence()
+ {
+ return ATTR_PRECEDENCE;
+ }
+
+
+ public static Object convert(JsonNode node)
+ {
+ return null;
+ }
+}