All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.yadaframework.web.dialect.YadaSrcAttrProcessor Maven / Gradle / Ivy

The newest version!
package net.yadaframework.web.dialect;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

import net.yadaframework.core.YadaConfiguration;

/**
 * Questa classe aggiunge il tag yada:src identico a yada:href.
 * It is needed only when linking static images from the resources folder.
 * When images are in the contents folder, the file name should be versioned instead, using YadaAttachedFile
 * @see YadaHrefAttrProcessor
 */
@Deprecated // Just use th:href because YadaLinkBuilder takes care of everything
public class YadaSrcAttrProcessor extends AbstractAttributeTagProcessor {
	private final Logger log = LoggerFactory.getLogger(getClass());

	public static final int ATTR_PRECEDENCE = 10000;
    public static final String ATTR_NAME = "src";

    private final YadaDialectUtil yadaDialectUtil;

	/**
	 *
	 * @param resFolder Folder contenente le risorse da "versionare", per esempio "/res/"
	 * @param config
	 */
	public YadaSrcAttrProcessor(final String dialectPrefix, YadaConfiguration config) {
        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
                ATTR_PRECEDENCE,   // Precedence (inside dialect's own precedence)
                true);             // Remove the matched attribute afterwards
		yadaDialectUtil = new YadaDialectUtil(config);
	}


//	@Override
//	public int getPrecedence() {
//	    // A value of 10000 is higher than any attribute in the
//        // SpringStandard dialect. So this attribute will execute
//        // after all other attributes from that dialect, if in the
//        // same tag.
//        return ATTR_PRECEDENCE;
//	}
//
//    @Override
//    protected String getTargetAttributeName(
//            final Arguments arguments, final Element element, final String attributeName) {
//        return ATTR_NAME;
//    }

    @Override
    protected void doProcess(
            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 String semiurl = (String) expression.execute(context);

        String resultUrl = yadaDialectUtil.getVersionedAttributeValue(semiurl);

        /*
         * Set the new value into the 'href' attribute
         */
        if (resultUrl != null) {
        	structureHandler.setAttribute(ATTR_NAME, resultUrl);
        }
    }

//    /**
//     *
//     */
//    @Override
//    protected String getTargetAttributeValue(final Arguments arguments, final Element element, final String attributeName) {
//    	String url = super.getTargetAttributeValue(arguments, element, attributeName);
//    	String resultUrl = yadaDialectUtil.getResAttributeValue(arguments, url);
//        return RequestDataValueProcessorUtils.processUrl(arguments.getConfiguration(), arguments, resultUrl);
//    }

//	@Override
//	protected ModificationType getModificationType(Arguments arguments, Element element, String attributeName, String newAttributeName) {
//		return ModificationType.SUBSTITUTION;
//	}
//
//	@Override
//	protected boolean recomputeProcessorsAfterExecution(Arguments arguments, Element element, String attributeName) {
//		return false;
//	}
//
//	@Override
//	protected boolean removeAttributeIfEmpty(Arguments arguments, Element element, String attributeName, String newAttributeName) {
//		return false;
//	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy