Search in sources :

Example 1 with Generator

use of org.apache.sling.rewriter.Generator 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)

Aggregations

Generator (org.apache.sling.rewriter.Generator)1 PipelineConfiguration (org.apache.sling.rewriter.PipelineConfiguration)1 ProcessingComponentConfiguration (org.apache.sling.rewriter.ProcessingComponentConfiguration)1 Serializer (org.apache.sling.rewriter.Serializer)1 Transformer (org.apache.sling.rewriter.Transformer)1 ContentHandler (org.xml.sax.ContentHandler)1