X-Git-Url: https://juplo.de/gitweb/?p=maven-thymeleaf-skin;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fthymeleaf%2FImportVariablesAttrProcessor.java;h=fe2725ecf35a7614813386666764c9f76cbdec78;hp=63963faa32f7ed32db26af71d90153815b77575e;hb=3502d6bbe03436d377ed095a7251c1f14a5fba83;hpb=53d411dcd6f52a22ec4513a9d43b3b5f737655a1 diff --git a/src/main/java/de/juplo/thymeleaf/ImportVariablesAttrProcessor.java b/src/main/java/de/juplo/thymeleaf/ImportVariablesAttrProcessor.java index 63963fa..fe2725e 100644 --- a/src/main/java/de/juplo/thymeleaf/ImportVariablesAttrProcessor.java +++ b/src/main/java/de/juplo/thymeleaf/ImportVariablesAttrProcessor.java @@ -12,24 +12,9 @@ import java.time.format.DateTimeFormatter; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; -import java.util.Locale; import java.util.Map; 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.context.VariablesMap; -import org.thymeleaf.dom.Element; -import org.thymeleaf.processor.ProcessorResult; -import org.thymeleaf.processor.attr.AbstractAttrProcessor; -import org.thymeleaf.resourceresolver.IResourceResolver; -import org.thymeleaf.templateresolver.ITemplateResolver; -import org.thymeleaf.templateresolver.TemplateResolution; @@ -37,16 +22,8 @@ import org.thymeleaf.templateresolver.TemplateResolution; * * @author kai */ -@Configurable -public class ImportVariablesAttrProcessor extends AbstractAttrProcessor +public class ImportVariablesAttrProcessor { - 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 = @@ -55,65 +32,8 @@ public class ImportVariablesAttrProcessor extends AbstractAttrProcessor private static final JsonFactory FACTORY = new JsonFactory(); - @Autowired - MessageSource messageSource; - @Autowired - Locale defaultLocale; - - - public ImportVariablesAttrProcessor() + public Object processAttribute(InputStream is) { - super("variables"); - } - - - @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, - new IContext() // << We will not execute the template, hence we need no context - { - @Override - public VariablesMap getVariables() - { - return new VariablesMap<>(); - } - - @Override - public Locale getLocale() - { - return Locale.getDefault(); - } - - @Override - public void addContextExecutionInfo(String templateName) - { - } - }); - - 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, resolution.getResourceName()); - if (is == null) - continue; - try { /** Read the JSON and create the variables */ @@ -123,71 +43,22 @@ public class ImportVariablesAttrProcessor extends AbstractAttrProcessor if (token == null) { - LOG.warn("found empty content for {}", templateName); - break; + LOG.warn("empty input-stream"); + return null; } - if (!JsonToken.START_OBJECT.equals(token)) - { - 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!" - ); - } - - token = parser.nextToken(); - if (token == null) - fail(parser, "unexpected EOF"); - if (JsonToken.END_OBJECT.equals(token)) - { - LOG.warn("found empty object for {}", templateName); - break; - } - - do - { - if (!JsonToken.FIELD_NAME.equals(token)) - fail(parser, "expected a field-name"); - - String var_name = parser.getText(); - parser.nextToken(); - Object var_value = convert(parser); - - LOG.debug( - "defining variable {} of type {}", - var_name, - var_value == null ? "NULL" : var_value.getClass().getSimpleName() - ); - element.setNodeLocalVariable(var_name, var_value); - - token = parser.nextToken(); - if (token == null) - fail(parser, "unexpected EOF"); - } - while (!JsonToken.END_OBJECT.equals(token)); + Object result = convert(parser); if (parser.nextToken() != null) fail(parser, "unexpected data after parsed variables"); + + return result; } catch (IOException e) { - LOG.error("cannot parse {} as JSON: {}", templateName, e.getMessage()); + LOG.error("cannot parse input-stream as JSON: {}", e.getMessage()); throw new RuntimeException(e); } - } - - element.removeAttribute(name); - - return ProcessorResult.OK; - } - - - @Override - public int getPrecedence() - { - return ATTR_PRECEDENCE; }