WIP: variables-dialect
authorKai Moritz <kai@juplo.de>
Sat, 18 Jun 2016 13:14:27 +0000 (15:14 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 18 Jun 2016 13:14:27 +0000 (15:14 +0200)
src/main/java/de/juplo/thymeleaf/ImportVariablesAttrProcessor.java
src/main/java/de/juplo/thymeproxy/Application.java

index b4c3b30..63963fa 100644 (file)
@@ -9,7 +9,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import java.io.IOException;
 import java.io.InputStream;
 import java.time.format.DateTimeFormatter;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale;
@@ -23,6 +23,7 @@ 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;
@@ -62,7 +63,7 @@ public class ImportVariablesAttrProcessor extends AbstractAttrProcessor
 
   public ImportVariablesAttrProcessor()
   {
-    super("crumb");
+    super("variables");
   }
 
 
@@ -80,8 +81,25 @@ public class ImportVariablesAttrProcessor extends AbstractAttrProcessor
         new TemplateProcessingParameters(
             config,
             templateName,
-            (IContext)null // << We will not execute the template, hence we need no context
-            );
+            new IContext() // << We will not execute the template, hence we need no context
+            {
+              @Override
+              public VariablesMap<String, Object> getVariables()
+              {
+                return new VariablesMap<>();
+              }
+
+              @Override
+              public Locale getLocale()
+              {
+                return Locale.getDefault();
+              }
+
+              @Override
+              public void addContextExecutionInfo(String templateName)
+              {
+              }
+            });
 
     for (ITemplateResolver t_resolver : config.getTemplateResolvers())
     {
@@ -91,7 +109,8 @@ public class ImportVariablesAttrProcessor extends AbstractAttrProcessor
       if (!"JSON".equals(resolution.getTemplateMode()))
         continue;
       IResourceResolver r_resolver = resolution.getResourceResolver();
-      InputStream is = r_resolver.getResourceAsStream(params, templateName);
+      InputStream is =
+          r_resolver.getResourceAsStream(params, resolution.getResourceName());
       if (is == null)
         continue;
 
@@ -121,7 +140,7 @@ public class ImportVariablesAttrProcessor extends AbstractAttrProcessor
         token = parser.nextToken();
         if (token == null)
           fail(parser, "unexpected EOF");
-        if (!JsonToken.END_OBJECT.equals(token))
+        if (JsonToken.END_OBJECT.equals(token))
         {
           LOG.warn("found empty object for {}", templateName);
           break;
@@ -133,6 +152,7 @@ public class ImportVariablesAttrProcessor extends AbstractAttrProcessor
             fail(parser, "expected a field-name");
 
           String var_name = parser.getText();
+          parser.nextToken();
           Object var_value = convert(parser);
 
           LOG.debug(
@@ -173,7 +193,7 @@ public class ImportVariablesAttrProcessor extends AbstractAttrProcessor
 
   static Object convert(JsonParser parser) throws IOException
   {
-    JsonToken token = parser.nextToken();
+    JsonToken token = parser.getCurrentToken();
     if (token == null)
       fail(parser, "unexpected EOF");
 
@@ -199,14 +219,17 @@ public class ImportVariablesAttrProcessor extends AbstractAttrProcessor
     if (token == null)
       fail(parser, "unexpected EOF");
 
-    Map<String, Object> map = new HashMap<>();
+    Map<String, Object> map = new LinkedHashMap<>();
 
     while (!JsonToken.END_OBJECT.equals(token))
     {
       if (!JsonToken.FIELD_NAME.equals(token))
         fail(parser, "expected a field-name");
 
-      map.put(parser.getText(), convert(parser));
+      String name = parser.getText();
+      parser.nextToken();
+      Object value = convert(parser);
+      map.put(name, value);
 
       token = parser.nextToken();
       if (token == null)
index d7aa61d..eb2ddbb 100644 (file)
@@ -1,5 +1,6 @@
 package de.juplo.thymeproxy;
 
+import de.juplo.thymeleaf.JuploDialect;
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.http.impl.client.CloseableHttpClient;
@@ -71,12 +72,34 @@ public class Application
     return resolver;
   }
 
+  @Bean
+  public TemplateResolver jsonTemplateResolver(
+      IResourceResolver resources,
+      Environment env
+      )
+  {
+    TemplateResolver resolver = new TemplateResolver();
+    resolver.setResourceResolver(resources);
+    resolver.setPrefix("http://localhost:8080/thymeleaf/");
+    resolver.setSuffix(".json");
+    resolver.setTemplateMode("JSON");
+    resolver.setCharacterEncoding("UTF-8");
+    resolver.setCacheable(Boolean.valueOf(env.getProperty("thymeproxy.cacheable")));
+    return resolver;
+  }
+
   @Bean
   public UrlResourceResolver thymeleafResourceResolver()
   {
     return new UrlResourceResolver();
   }
 
+  @Bean
+  public JuploDialect juploDialect()
+  {
+    return new JuploDialect();
+  }
+
 
   public static void main(String[] args)
   {