[java-shib-attribute] 02/02: Kick the Wheels on Dialects

Rod Widdowson rdw at steadingsoftware.com
Sun Jan 21 16:21:01 UTC 2024


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch dev/IDP-226
in repository java-shib-attribute.

View the commit online:
http://git.shibboleth.net/view/?p=java-shib-attribute.git;a=commit;h=4e1e88f09ba66852983d45dbf15663d3bbe4cb63

commit 4e1e88f09ba66852983d45dbf15663d3bbe4cb63
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sun Jan 21 14:42:47 2024 +0000

    Kick the Wheels on Dialects
    
    We add ourr own
      Dialect
      Inliner
      TemplateBoundaryProcessor
      TextProcessor
    
    And magical things happen.
---
 .../resolver/dc/impl/ThymeLeafSandbox.java         | 135 ++++++++++++++++++++-
 1 file changed, 134 insertions(+), 1 deletion(-)

diff --git a/shib-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/dc/impl/ThymeLeafSandbox.java b/shib-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/dc/impl/ThymeLeafSandbox.java
index f8ed4bedd..17f5ea008 100644
--- a/shib-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/dc/impl/ThymeLeafSandbox.java
+++ b/shib-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/dc/impl/ThymeLeafSandbox.java
@@ -16,18 +16,39 @@ import static org.testng.Assert.assertEquals;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 
 import org.testng.annotations.Test;
+import org.thymeleaf.IEngineConfiguration;
 import org.thymeleaf.TemplateEngine;
 import org.thymeleaf.TemplateSpec;
 import org.thymeleaf.context.Context;
+import org.thymeleaf.context.ITemplateContext;
+import org.thymeleaf.dialect.AbstractProcessorDialect;
+import org.thymeleaf.engine.EngineEventUtils;
+import org.thymeleaf.exceptions.TemplateProcessingException;
+import org.thymeleaf.inline.IInliner;
+import org.thymeleaf.inline.NoOpInliner;
+import org.thymeleaf.model.ITemplateEnd;
+import org.thymeleaf.model.ITemplateStart;
+import org.thymeleaf.model.IText;
+import org.thymeleaf.processor.IProcessor;
+import org.thymeleaf.processor.templateboundaries.AbstractTemplateBoundariesProcessor;
+import org.thymeleaf.processor.templateboundaries.ITemplateBoundariesStructureHandler;
+import org.thymeleaf.processor.text.AbstractTextProcessor;
+import org.thymeleaf.processor.text.ITextStructureHandler;
+import org.thymeleaf.standard.StandardDialect;
+import org.thymeleaf.standard.inline.AbstractStandardInliner;
 import org.thymeleaf.templatemode.TemplateMode;
 import org.thymeleaf.templateresolver.StringTemplateResolver;
 
+import net.shibboleth.shared.collection.CollectionSupport;
+
 public class ThymeLeafSandbox {
 
+    private final String templateStr = "Nibble a happy [[${warthog}]] says [[${thing.thing1}]] or [[${thing.thing2}]]";
+
 	@Test public void testEngine() {
-		final String templateStr = "Nibble a happy [(${warthog})] says [(${thing.thing1})] or [( ${thing.thing2})]";
 		final Map<String, Object> map = new HashMap<>(2);
 		map.put("warthog","warthog");
 		var obj = new MyObject();
@@ -42,8 +63,120 @@ public class ThymeLeafSandbox {
 		assertEquals(output, "Nibble a happy " + map.get("warthog") + " says " + obj.getThing1() + " or " +  obj.getThing2());
 	}
 
+    @Test public void testDialect() {
+        final Map<String, Object> map = new HashMap<>(2);
+        map.put("warthog","jellyfish");
+        var obj = new MyObject();
+        map.put("thing", obj);
+
+        final Context ctx = new Context(null, map);
+        final TemplateEngine engine = new TemplateEngine();
+        engine.setAdditionalDialects(CollectionSupport.singleton(new SandBoxDialect()));
+        engine.setTemplateResolver(new StringTemplateResolver());
+        final TemplateSpec spec = new TemplateSpec(templateStr, TemplateMode.TEXT);
+
+        final String output = engine.process(spec, ctx);
+        assertEquals(output, "Nibble a happy " + map.get("warthog") + " says " + obj.getThing1() + " or " +  obj.getThing2());
+    }
+
 	private class MyObject {
 		public String getThing1() { return "Thing1"; }
 		public String getThing2() { return "Thing2"; }
 	}
+
+	class SandBoxProcessor extends AbstractTextProcessor  {
+
+        public SandBoxProcessor(TemplateMode templateMode, int precedence) {
+            super(templateMode, precedence);
+        }
+
+        @Override
+        protected void doProcess(ITemplateContext context, IText text, ITextStructureHandler structureHandler) {
+
+            if (EngineEventUtils.isWhitespace(text)) {
+                return;
+            }
+
+            final IInliner inliner = context.getInliner();  // OVERRIDE POINT
+
+            if (inliner == null || inliner == NoOpInliner.INSTANCE) {
+                return;
+            }
+
+            final CharSequence inlined = inliner.inline(context, text);
+            if (inlined != null && inlined != text) {
+                structureHandler.setText(inlined);
+            }
+        }
+	}
+	
+	public final class SandboxTemplateBoundariesProcessor extends AbstractTemplateBoundariesProcessor {
+
+	    public static final int PRECEDENCE = 11;
+
+	    public SandboxTemplateBoundariesProcessor(final TemplateMode templateMode) {
+	        super(templateMode, PRECEDENCE);
+	    }
+
+
+	    @Override
+	    public void doProcessTemplateStart(
+	            final ITemplateContext context,
+	            final ITemplateStart templateStart, final ITemplateBoundariesStructureHandler structureHandler) {
+
+	        switch (getTemplateMode()) {
+
+            case TEXT:
+                structureHandler.setInliner(new MyInliner(context.getConfiguration()));
+                break;
+
+	            default:
+	                throw new TemplateProcessingException(
+	                        "Unrecognized template mode: " + getTemplateMode() + ", cannot initialize inlining!");
+
+	        }
+
+	    }
+
+
+	    @Override
+	    public void doProcessTemplateEnd(
+	            final ITemplateContext context,
+	            final ITemplateEnd templateEnd, final ITemplateBoundariesStructureHandler structureHandler) {
+
+	        // Empty - nothing to be done on template end
+
+	    }
+	}
+	
+	private class MyInliner extends AbstractStandardInliner {
+
+        protected MyInliner(IEngineConfiguration configuration) {
+            super(configuration, TemplateMode.TEXT);
+            // TODO Auto-generated constructor stub
+        }
+
+        @Override
+        protected String produceEscapedOutput(Object input) {
+            // TODO Auto-generated method stub
+            return null;
+        }
+	    
+	}
+
+	private class SandBoxDialect extends AbstractProcessorDialect {
+
+	    private static final String DIALECT_NAME = "SANDBOX Dialect";
+
+
+	    public SandBoxDialect() {
+	        super(DIALECT_NAME, "sandbox", StandardDialect.PROCESSOR_PRECEDENCE);
+	    }
+
+	    public Set<IProcessor> getProcessors(final String dialectPrefix) {
+	        return CollectionSupport.setOf(
+	                new SandBoxProcessor(TemplateMode.TEXT, getDialectProcessorPrecedence()),
+	                new SandboxTemplateBoundariesProcessor(TemplateMode.TEXT));
+	    }
+	}
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list