Search in sources :

Example 1 with ItemProcessingException

use of org.craftercms.core.exception.ItemProcessingException in project core by craftercms.

the class TemplateProcessor method process.

/**
 * Processes the content of certain nodes (found by the {@code NodeScanner} in the item's descriptor as templates,
 * by compiling the node text templates through the {@code templateCompiler} and then processing the compiled
 * template with a model returned by {@code modelFactory}.
 *
 * @throws ItemProcessingException if an error occurred while processing a template
 */
public Item process(Context context, CachingOptions cachingOptions, Item item) throws ItemProcessingException {
    String descriptorUrl = item.getDescriptorUrl();
    Document descriptorDom = item.getDescriptorDom();
    if (descriptorDom != null) {
        List<Node> templateNodes = templateNodeScanner.scan(descriptorDom);
        if (CollectionUtils.isNotEmpty(templateNodes)) {
            for (Node templateNode : templateNodes) {
                String templateNodePath = templateNode.getUniquePath();
                if (logger.isDebugEnabled()) {
                    logger.debug("Template found in " + descriptorUrl + " at " + templateNodePath);
                }
                String templateId = templateNodePath + "@" + descriptorUrl;
                String template = templateNode.getText();
                IdentifiableStringTemplateSource templateSource = new IdentifiableStringTemplateSource(templateId, template);
                Object model = modelFactory.getModel(item, templateNode, template);
                StringWriter output = new StringWriter();
                try {
                    CompiledTemplate compiledTemplate = templateCompiler.compile(templateSource);
                    compiledTemplate.process(model, output);
                } catch (TemplateException e) {
                    throw new ItemProcessingException("Unable to process the template " + templateId, e);
                }
                templateNode.setText(output.toString());
            }
        }
    }
    return item;
}
Also used : StringWriter(java.io.StringWriter) TemplateException(org.craftercms.core.exception.TemplateException) Node(org.dom4j.Node) IdentifiableStringTemplateSource(org.craftercms.core.util.template.impl.IdentifiableStringTemplateSource) ItemProcessingException(org.craftercms.core.exception.ItemProcessingException) Document(org.dom4j.Document) CompiledTemplate(org.craftercms.core.util.template.CompiledTemplate)

Example 2 with ItemProcessingException

use of org.craftercms.core.exception.ItemProcessingException in project search by craftercms.

the class InheritedDescriptorsItemProcessor method process.

@Override
public Item process(final Context context, final CachingOptions cachingOptions, final Item item) throws ItemProcessingException {
    if (item.getDescriptorDom() != null) {
        DescriptorMergeStrategy mergeStrategy = mergeStrategyResolver.getStrategy(item.getDescriptorUrl(), item.getDescriptorDom());
        if (mergeStrategy != null) {
            List<MergeableDescriptor> inheritedDescriptors = mergeStrategy.getDescriptors(context, cachingOptions, item.getDescriptorUrl(), item.getDescriptorDom());
            if (CollectionUtils.isNotEmpty(inheritedDescriptors)) {
                inheritedDescriptors.stream().filter(descriptor -> !StringUtils.equals(descriptor.getUrl(), item.getDescriptorUrl())).forEach(descriptor -> {
                    Element inheritedFromElement = DocumentHelper.createElement(inheritsFromElementName);
                    inheritedFromElement.setText(descriptor.getUrl());
                    item.getDescriptorDom().getRootElement().add(inheritedFromElement);
                });
            }
        }
    }
    return item;
}
Also used : Item(org.craftercms.core.service.Item) CachingOptions(org.craftercms.core.service.CachingOptions) DescriptorMergeStrategyResolver(org.craftercms.core.xml.mergers.DescriptorMergeStrategyResolver) DocumentHelper(org.dom4j.DocumentHelper) StringUtils(org.apache.commons.lang3.StringUtils) Context(org.craftercms.core.service.Context) List(java.util.List) CollectionUtils(org.apache.commons.collections.CollectionUtils) MergeableDescriptor(org.craftercms.core.xml.mergers.MergeableDescriptor) ItemProcessingException(org.craftercms.core.exception.ItemProcessingException) Element(org.dom4j.Element) DescriptorMergeStrategy(org.craftercms.core.xml.mergers.DescriptorMergeStrategy) ItemProcessor(org.craftercms.core.processors.ItemProcessor) DescriptorMergeStrategy(org.craftercms.core.xml.mergers.DescriptorMergeStrategy) Element(org.dom4j.Element) MergeableDescriptor(org.craftercms.core.xml.mergers.MergeableDescriptor)

Aggregations

ItemProcessingException (org.craftercms.core.exception.ItemProcessingException)2 StringWriter (java.io.StringWriter)1 List (java.util.List)1 CollectionUtils (org.apache.commons.collections.CollectionUtils)1 StringUtils (org.apache.commons.lang3.StringUtils)1 TemplateException (org.craftercms.core.exception.TemplateException)1 ItemProcessor (org.craftercms.core.processors.ItemProcessor)1 CachingOptions (org.craftercms.core.service.CachingOptions)1 Context (org.craftercms.core.service.Context)1 Item (org.craftercms.core.service.Item)1 CompiledTemplate (org.craftercms.core.util.template.CompiledTemplate)1 IdentifiableStringTemplateSource (org.craftercms.core.util.template.impl.IdentifiableStringTemplateSource)1 DescriptorMergeStrategy (org.craftercms.core.xml.mergers.DescriptorMergeStrategy)1 DescriptorMergeStrategyResolver (org.craftercms.core.xml.mergers.DescriptorMergeStrategyResolver)1 MergeableDescriptor (org.craftercms.core.xml.mergers.MergeableDescriptor)1 Document (org.dom4j.Document)1 DocumentHelper (org.dom4j.DocumentHelper)1 Element (org.dom4j.Element)1 Node (org.dom4j.Node)1