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

org.zodiac.template.base.impl.TemplateServiceDefinitionParser Maven / Gradle / Ivy

The newest version!
package org.zodiac.template.base.impl;

import static org.zodiac.core.context.ext.util.DomUtil.*;
import static org.zodiac.core.context.ext.util.SpringExtUtil.*;

import java.util.Map;

import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;
import org.zodiac.core.constants.ServicesNamespaceConstants;
import org.zodiac.core.context.ext.ConfigurationPoint;
import org.zodiac.core.context.ext.Contribution;
import org.zodiac.core.context.ext.ContributionAware;
import org.zodiac.core.context.ext.support.parser.AbstractNamedBeanDefinitionParser;
import org.zodiac.sdk.toolkit.util.AssertUtil;
import org.zodiac.sdk.toolkit.util.file.FileToolUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
import org.zodiac.template.base.constants.TemplateNamespaceConstants;

public class TemplateServiceDefinitionParser extends AbstractNamedBeanDefinitionParser
    implements ContributionAware {
    private ConfigurationPoint templateEnginesConfigurationPoint;

    public void setContribution(Contribution contrib) {
        this.templateEnginesConfigurationPoint = getSiblingConfigurationPoint(String.format("%s/%s/%s",
            ServicesNamespaceConstants.SERVICES_NAMESPACE, TemplateNamespaceConstants.TEMPLATE_NAMESPACE,
            TemplateNamespaceConstants.ENGINES_NAMESPACE), contrib);
    }

    @Override
    protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
        parseBeanDefinitionAttributes(element, parserContext, builder);

        Map engines = createManagedMap(element, parserContext);
        Map mappings = createManagedMap(element, parserContext);

        ElementSelector engineSelector = ns(templateEnginesConfigurationPoint.getNamespaceUri());
        ElementSelector mappingSelector = and(sameNs(element), name("template-mapping"));

        for (Element subElement : subElements(element)) {
            if (engineSelector.accept(subElement)) {
                /*engine*/
                BeanDefinitionHolder engine =
                    parseConfigurationPointBean(subElement, templateEnginesConfigurationPoint, parserContext, builder);
                engines.put(engine.getBeanName(), engine);
            } else if (mappingSelector.accept(subElement)) {
                /*mapping*/
                String ext = FileToolUtil.normalizeExtension(subElement.getAttribute("extension"));
                String engineName = AssertUtil.assertNotNull(StrUtil.trimToNull(subElement.getAttribute("engine")), "engine");

                AssertUtil.assertNotNull(ext, "extension");
                AssertUtil.assertTrue(!mappings.containsKey(ext), "duplicated extension: %s", ext);

                mappings.put(ext, engineName);
            }
        }
        builder.addPropertyValue("engines", engines);
        builder.addPropertyValue("engineNameMappings", mappings);

        attributesToProperties(element, builder, "defaultExtension", "searchExtensions", "searchLocalizedTemplates",
            "cacheEnabled");
    }

    @Override
    protected String getDefaultName() {
        return "templateService";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy