Search in sources :

Example 1 with ProcessingComponentConfiguration

use of org.apache.sling.rewriter.ProcessingComponentConfiguration in project sling by apache.

the class PipelineImpl method init.

/**
     * @see org.apache.sling.rewriter.Processor#init(org.apache.sling.rewriter.ProcessingContext, org.apache.sling.rewriter.ProcessorConfiguration)
     */
public void init(ProcessingContext processingContext, ProcessorConfiguration c) throws IOException {
    LOGGER.debug("Setting up pipeline...");
    final PipelineConfiguration config = (PipelineConfiguration) c;
    final ProcessingComponentConfiguration[] transformerConfigs = config.getTransformerConfigurations();
    // create components and initialize them
    // lets get custom rewriter transformers
    final Transformer[][] rewriters = this.factoryCache.getGlobalTransformers(processingContext);
    final ProcessingComponentConfiguration generatorConfig = config.getGeneratorConfiguration();
    this.generator = this.getPipelineComponent(Generator.class, generatorConfig.getType(), false);
    LOGGER.debug("Using generator type {}: {}.", generatorConfig.getType(), generator);
    generator.init(processingContext, generatorConfig);
    final int transformerCount = (transformerConfigs == null ? 0 : transformerConfigs.length) + rewriters[0].length + rewriters[1].length;
    int index = 0;
    if (transformerCount > 0) {
        // add all pre rewriter transformers
        transformers = new Transformer[transformerCount];
        for (int i = 0; i < rewriters[0].length; i++) {
            transformers[index] = rewriters[0][i];
            LOGGER.debug("Using pre transformer: {}.", transformers[index]);
            transformers[index].init(processingContext, ProcessingComponentConfigurationImpl.EMPTY);
            index++;
        }
        if (transformerConfigs != null) {
            for (int i = 0; i < transformerConfigs.length; i++) {
                transformers[index] = this.getPipelineComponent(Transformer.class, transformerConfigs[i].getType(), transformerConfigs[i].getConfiguration().get(ProcessingComponentConfiguration.CONFIGURATION_COMPONENT_OPTIONAL, false));
                if (transformers[index] != null) {
                    LOGGER.debug("Using transformer type {}: {}.", transformerConfigs[i].getType(), transformers[index]);
                    transformers[index].init(processingContext, transformerConfigs[i]);
                    index++;
                } else {
                    LOGGER.debug("Skipping missing optional transformer of type {}", transformerConfigs[i].getType());
                }
            }
        }
        for (int i = 0; i < rewriters[1].length; i++) {
            transformers[index] = rewriters[1][i];
            LOGGER.debug("Using post transformer: {}.", transformers[index]);
            transformers[index].init(processingContext, ProcessingComponentConfigurationImpl.EMPTY);
            index++;
        }
    } else {
        transformers = EMPTY_TRANSFORMERS;
    }
    final ProcessingComponentConfiguration serializerConfig = config.getSerializerConfiguration();
    this.serializer = this.getPipelineComponent(Serializer.class, serializerConfig.getType(), false);
    LOGGER.debug("Using serializer type {}: {}.", serializerConfig.getType(), serializer);
    serializer.init(processingContext, serializerConfig);
    ContentHandler pipelineComponent = serializer;
    // now chain pipeline
    for (int i = index; i > 0; i--) {
        transformers[i - 1].setContentHandler(pipelineComponent);
        pipelineComponent = transformers[i - 1];
    }
    this.firstContentHandler = pipelineComponent;
    generator.setContentHandler(this.firstContentHandler);
    LOGGER.debug("Finished pipeline setup.");
}
Also used : ProcessingComponentConfiguration(org.apache.sling.rewriter.ProcessingComponentConfiguration) Transformer(org.apache.sling.rewriter.Transformer) PipelineConfiguration(org.apache.sling.rewriter.PipelineConfiguration) ContentHandler(org.xml.sax.ContentHandler) Generator(org.apache.sling.rewriter.Generator) Serializer(org.apache.sling.rewriter.Serializer)

Example 2 with ProcessingComponentConfiguration

use of org.apache.sling.rewriter.ProcessingComponentConfiguration in project sling by apache.

the class ProcessorConfigurationImpl method getComponentConfigs.

protected ProcessingComponentConfiguration[] getComponentConfigs(final Resource configResource, final String propertyName, final String prefix) {
    final ValueMap properties = ResourceUtil.getValueMap(configResource);
    final String[] types = properties.get(propertyName, String[].class);
    if (types != null && types.length > 0) {
        final ProcessingComponentConfiguration[] configs = new ProcessingComponentConfiguration[types.length];
        for (int i = 0; i < types.length; i++) {
            // there are two possible ways for a component configuration:
            // 1. {prefix}-{type}, like generator-html
            // 2. {prefix}-{index}, like generator-1 (with the index starting at 1)
            // while usually the first way is sufficient, the second one is required if the
            // same transformer is used more than once in a pipeline.
            final String resourceName = prefix + '-' + types[i];
            Resource childResource = configResource.getResourceResolver().getResource(configResource, resourceName);
            if (childResource == null) {
                final String secondResourceName = prefix + '-' + (i + 1);
                childResource = configResource.getResourceResolver().getResource(configResource, secondResourceName);
            }
            final ValueMap config;
            if (childResource != null) {
                final ValueMap childProps = ResourceUtil.getValueMap(childResource);
                config = childProps;
            } else {
                config = null;
            }
            configs[i] = new ProcessingComponentConfigurationImpl(types[i], config);
        }
        return configs;
    }
    return null;
}
Also used : ProcessingComponentConfiguration(org.apache.sling.rewriter.ProcessingComponentConfiguration) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource)

Aggregations

ProcessingComponentConfiguration (org.apache.sling.rewriter.ProcessingComponentConfiguration)2 Resource (org.apache.sling.api.resource.Resource)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 Generator (org.apache.sling.rewriter.Generator)1 PipelineConfiguration (org.apache.sling.rewriter.PipelineConfiguration)1 Serializer (org.apache.sling.rewriter.Serializer)1 Transformer (org.apache.sling.rewriter.Transformer)1 ContentHandler (org.xml.sax.ContentHandler)1