arnor
arnor

Reputation: 41

How can I configure a transformer in a sling rewriting pipeline?

I want to set targets to every <a>-tag on our site but instead of editing the components, I wrote a transformer. How can I configure it?

We already have a transformer and a corresponding factory. The factory is of pipeline type content-fragments and registered in a configuration file as per the documentation:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
          jcr:primaryType="nt:unstructured"
          contentTypes="text/html"
          enabled="{Boolean}true"
          generatorType="htmlparser"
          order="1000"
          paths="[/content]"
          serializerType="htmlwriter"
          transformerTypes="[link-transformer,content-fragments,linkchecker]">
    <generator-htmlparser
        jcr:primaryType="nt:unstructured"
        includeTags="[A]"/>
</jcr:root>

I both tried to give the new transformer the same pipeline type and a new pipeline type, linktransformer, and tried each of the following combinations:

The configuration is active in the Sling Rewriter Console:

Configuration content-fragments-rewrite

Name : content-fragments-rewrite
Content Types : [text/html]
Paths : [/content]
Order : 1000
Active : true
Valid : true
Process Error Response : true
Pipeline : 
    Generator : 
        htmlparser : {includeTags=[Ljava.lang.String;@23d079e2}
    Transformers : 
        link-transformer
        content-fragments
        linkchecker
    Serializer : 
        htmlwriter

but neither does the pipeline transform the HTML on the publisher nor on the author instance.

Here is the old transformer:

@Component(
    property = "pipeline.type=" + ContentFragmentLinkTransformerFactory.PIPELINE_TYPE,
    service = TransformerFactory.class
)
public class ContentFragmentLinkTransformerFactory implements TransformerFactory {

    public static final String PIPELINE_TYPE = "content-fragments";

    @Override
    public Transformer createTransformer() {
        return new ContentFragmentLinkTransformer();
    }

}

and the new one:

@Component(
    property = "pipeline.type=" + LinkTransformerFactory.PIPELINE_TYPE,
    service = TransformerFactory.class
)
public class LinkTransformerFactory implements TransformerFactory {

    public static final String PIPELINE_TYPE = "link-transformer";

    @Override
    public Transformer createTransformer() {
        return new LinkTransformer();
    }

}

I put breakpoints into both of the createTransforemer() methods as well as the transformers' startElement() method but on neither of the instances does the program halt when debugging. I also tried to add an activate method into which I put a breakpoint where the program did indeed halt as expected.

I expect the transformer to transform the publisher HTML.

Upvotes: 1

Views: 1593

Answers (1)

arnor
arnor

Reputation: 41

Our problem was that we had another rewriter config with a higher order:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="nt:unstructured"
    contentTypes="[text/html]"
    enabled="{Boolean}true"
    generatorType="htmlparser"
    order="1001"
    serializerType="htmlwriter"
    transformerTypes="[linkchecker,versioned-clientlibs]">
    <transformer-mobile
        jcr:primaryType="nt:unstructured"
        component-optional="{Boolean}true"/>
    <transformer-mobiledebug
        jcr:primaryType="nt:unstructured"
        component-optional="{Boolean}true"/>
    <transformer-contentsync
        jcr:primaryType="nt:unstructured"
        component-optional="{Boolean}true"/>
</jcr:root>

The quick fix was to merge these two configs into one:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
          jcr:primaryType="nt:unstructured"
          contentTypes="text/html"
          enabled="{Boolean}true"
          generatorType="htmlparser"
          order="1010"
          paths="[/content]"
          serializerType="htmlwriter"
          transformerTypes="[linkchecker,link-transformer,versioned-clientlibs]">
    <transformer-mobile
        jcr:primaryType="nt:unstructured"
        component-optional="{Boolean}true"/>
    <transformer-mobiledebug
        jcr:primaryType="nt:unstructured"
        component-optional="{Boolean}true"/>
    <transformer-contentsync
        jcr:primaryType="nt:unstructured"
        component-optional="{Boolean}true"/>
    <generator-htmlparser
        jcr:primaryType="nt:unstructured"
        includeTags="[A,LINK,SCRIPT]"/>
</jcr:root>

Furthermore, I added a check to the transformer to transform only <a>-tags.

Upvotes: 2

Related Questions