publicWorkFocusDialect(){ // We will set this dialect the same "dialect processor" precedence as // the Standard Dialect, so that processor executions can interleave. super(DIALECT_NAME, PREFIX, PROCESSOR_PRECEDENCE); } @Override public Set<IProcessor> getProcessors(final String dialectPrefix){ final Set<IProcessor> processors = new HashSet<IProcessor>(); ... 在这里增加自定义的处理器 processors.add(new SampleAttributeTagProcessor(dialectPrefix)); processors.add(new SampleElementTagProcessor(dialectPrefix)); return processors; } }
publicSampleAttributeTagProcessor(final String dialectPrefix){ 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 ATTR_NAME, // Name of the attribute that will be matched true, // Apply dialect prefix to attribute name PRECEDENCE, // Precedence (inside dialect's own precedence) true); // Remove the matched attribute afterwards }
@Override protectedvoiddoProcess( final ITemplateContext context, final IProcessableElementTag tag, final AttributeName attributeName, final String attributeValue, final IElementTagStructureHandler structureHandler){
final IEngineConfiguration configuration = context.getConfiguration(); /* * Obtain the Thymeleaf Standard Expression parser */ final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
/* * Parse the attribute value as a Thymeleaf Standard Expression */ final IStandardExpression expression = parser.parseExpression(context, attributeValue); /* * Execute the expression just parsed */ final Integer position = (Integer) expression.execute(context);
publicSample3ElementTagProcessor(final String dialectPrefix){ super( TemplateMode.HTML, // This processor will apply only to HTML mode dialectPrefix, // Prefix to be applied to name for matching TAG_NAME, // Tag name: match specifically this tag true, // Apply dialect prefix to tag name null, // No attribute name: will match by tag name false, // No prefix to be applied to attribute name PRECEDENCE); // Precedence (inside dialect's own precedence) }
@Override protectedvoiddoProcess( final ITemplateContext context, final IProcessableElementTag tag, final IElementTagStructureHandler structureHandler){
/* * Read the 'order' attribute from the tag. This optional attribute in our tag * will allow us to determine whether we want to show a random headline or * only the latest one ('latest' is default). */ final String statusValue = tag.getAttributeValue("status");
final IEngineConfiguration configuration = context.getConfiguration(); /* * Obtain the Thymeleaf Standard Expression parser */ final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
final IStandardExpression expression = parser.parseExpression(context, statusValue);
final Integer parseStatus = (Integer) expression.execute(context); /* * Create the DOM structure that will be substituting our custom tag. */ final IModelFactory modelFactory = context.getModelFactory(); final IModel model = modelFactory.createModel(); if(parseStatus.equals(0)) { model.add(modelFactory.createOpenElementTag("td", "style", "background:green")); model.add(modelFactory.createText(HtmlEscape.escapeHtml5("停用"))); }else { model.add(modelFactory.createOpenElementTag("td", "style", "background:red")); model.add(modelFactory.createText(HtmlEscape.escapeHtml5("启用"))); } model.add(modelFactory.createCloseElementTag("td"));
/* * Instruct the engine to replace this entire element with the specified model. */ structureHandler.replaceWith(model, false); }