-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrote own dialect to bypass restrictions
- Loading branch information
1 parent
52b98f4
commit 742f21b
Showing
7 changed files
with
189 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Service/src/main/java/de/synyx/cl/learning/spring/thymeleaf/SvgService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package de.synyx.cl.learning.spring.thymeleaf; | ||
|
||
import de.synyx.cl.learning.spring.thymeleaf.svg.dialect.SvgDialect; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.stereotype.Service; | ||
import org.thymeleaf.context.IContext; | ||
import org.thymeleaf.spring5.ISpringTemplateEngine; | ||
import org.thymeleaf.spring5.SpringTemplateEngine; | ||
import org.thymeleaf.templatemode.TemplateMode; | ||
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; | ||
import org.thymeleaf.templateresolver.ITemplateResolver; | ||
|
||
@Service | ||
public class SvgService { | ||
|
||
/** | ||
* this is the entrypoint-prefix where the {@link #svgTemplateEngine} searches for the | ||
* given template name. e.g. blabla/helloWorld -> ${PREFIX}/blabla/helloWorld | ||
*/ | ||
private static final String RESOURCES_TEMPLATES_DIR_PREFIX = "templates/"; | ||
|
||
private final ISpringTemplateEngine svgTemplateEngine; | ||
|
||
@Autowired | ||
public SvgService(final MessageSource messageSource) { | ||
this.svgTemplateEngine = this.svgTemplateEngine(messageSource); | ||
} | ||
|
||
public String createSvg(final String templateName, final IContext templateContext) { | ||
return this.svgTemplateEngine.process(templateName, templateContext); | ||
} | ||
|
||
private ISpringTemplateEngine svgTemplateEngine(final MessageSource messageSource) { | ||
final SpringTemplateEngine engine = new SpringTemplateEngine(); | ||
engine.setTemplateResolver(this.svgXmlTemplateResolver()); | ||
engine.addDialect(new SvgDialect()); | ||
engine.setMessageSource(messageSource); | ||
return engine; | ||
} | ||
|
||
private ITemplateResolver svgXmlTemplateResolver() { | ||
final ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); | ||
resolver.setPrefix(RESOURCES_TEMPLATES_DIR_PREFIX); | ||
resolver.setCacheable(false); | ||
resolver.setTemplateMode(TemplateMode.HTML); | ||
return resolver; | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
Service/src/main/java/de/synyx/cl/learning/spring/thymeleaf/config/SvgConfig.java
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
.../java/de/synyx/cl/learning/spring/thymeleaf/svg/dialect/AttributeTagProcessorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package de.synyx.cl.learning.spring.thymeleaf.svg.dialect; | ||
|
||
import org.thymeleaf.IEngineConfiguration; | ||
import org.thymeleaf.context.ITemplateContext; | ||
import org.thymeleaf.engine.AttributeName; | ||
import org.thymeleaf.model.IProcessableElementTag; | ||
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor; | ||
import org.thymeleaf.processor.element.IElementTagStructureHandler; | ||
import org.thymeleaf.standard.expression.IStandardExpression; | ||
import org.thymeleaf.standard.expression.IStandardExpressionParser; | ||
import org.thymeleaf.standard.expression.StandardExpressions; | ||
import org.thymeleaf.templatemode.TemplateMode; | ||
|
||
public class AttributeTagProcessorFactory extends AbstractAttributeTagProcessor { | ||
|
||
public static AbstractAttributeTagProcessor create(String dialectPrefix, String attributeName) { | ||
return new AttributeTagProcessorFactory(dialectPrefix, attributeName); | ||
} | ||
|
||
private final String attributeNameToHandle; | ||
private static final int PRECEDENCE = 10000; | ||
|
||
|
||
public AttributeTagProcessorFactory(final String dialectPrefix, String attributeName) { | ||
super( | ||
TemplateMode.HTML, // This processor will apply only to HTML mode | ||
dialectPrefix, // Prefix to be applied to name for matching | ||
null, // No tag name: match any tag name | ||
false, // No prefix to be applied to tag name | ||
attributeName, // Name of the attribute that will be matched | ||
true, // Apply dialect prefix to attribute name | ||
PRECEDENCE, // Precedence (inside dialect's precedence) | ||
true); // Remove the matched attribute afterwards | ||
|
||
this.attributeNameToHandle = attributeName; | ||
} | ||
|
||
|
||
protected void doProcess( | ||
final ITemplateContext context, final IProcessableElementTag tag, | ||
final AttributeName attributeName, final String attributeValue, | ||
final IElementTagStructureHandler structureHandler) { | ||
|
||
final IEngineConfiguration configuration = context.getConfiguration(); | ||
|
||
final IStandardExpressionParser parser = | ||
StandardExpressions.getExpressionParser(configuration); | ||
|
||
final IStandardExpression expression = parser.parseExpression(context, attributeValue); | ||
|
||
final String output = (String) expression.execute(context); | ||
|
||
structureHandler.setAttribute(attributeNameToHandle, output); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Service/src/main/java/de/synyx/cl/learning/spring/thymeleaf/svg/dialect/SvgDialect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package de.synyx.cl.learning.spring.thymeleaf.svg.dialect; | ||
|
||
import org.thymeleaf.dialect.AbstractProcessorDialect; | ||
import org.thymeleaf.processor.IProcessor; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Needed to replace attributes that have no special name, that usually are addressed by th:attr. | ||
* | ||
* th:attr got some restrictions on thymeleaf 3.0.12 | ||
* see https://github.com/thymeleaf/thymeleaf/issues/809 | ||
* new is not allowed | ||
* static access to code is not allowed | ||
* | ||
* this is the workaround inspired by | ||
* https://www.thymeleaf.org/doc/articles/sayhelloextendingthymeleaf5minutes.html | ||
* https://www.thymeleaf.org/doc/articles/sayhelloagainextendingthymeleafevenmore5minutes.html | ||
* | ||
* will replace | ||
* svg:x1="${variable}" by x1="variableContent" | ||
* | ||
*/ | ||
public class SvgDialect extends AbstractProcessorDialect { | ||
|
||
public SvgDialect() { | ||
super( | ||
"SVG-Dialect", // Dialect name | ||
"svg", // Dialect prefix (hello:*) | ||
1000); // Dialect precedence | ||
} | ||
|
||
|
||
/* | ||
* Initialize the dialect's processors. | ||
* | ||
* Note the dialect prefix is passed here because, although we set | ||
* "svg" to be the dialect's prefix at the constructor, that only | ||
* works as a default, and at engine configuration time the user | ||
* might have chosen a different prefix to be used. | ||
*/ | ||
public Set<IProcessor> getProcessors(final String dialectPrefix) { | ||
final Set<IProcessor> processors = new HashSet<>(); | ||
// here we can identify all attributes that should be replaced dynamically | ||
processors.add(AttributeTagProcessorFactory.create(dialectPrefix, "y1")); | ||
processors.add(AttributeTagProcessorFactory.create(dialectPrefix, "y2")); | ||
processors.add(AttributeTagProcessorFactory.create(dialectPrefix, "stroke")); | ||
return processors; | ||
} | ||
|
||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters