Expand my Community achievements bar.

SOLVED

Transformer Factory options

Avatar

Level 5

We've implemented a TransformerFactory in our project to help re-write some links from "/content/project_name" to just "/project_name".

The only downfall is that this Factory IGNORES any comments.  For a number of reasons, we need to have the COMMENTS updated as well.

Is anyone aware of a way to update those comments as well?

Thank you in advance.

-Dean

1 Accepted Solution

Avatar

Correct answer by
Employee

Here's a trivial example of a Transfomer which adds "HELLO" inside comments: https://gist.github.com/justinedelson/cdcf502413740bef9539faf74385a599

View solution in original post

8 Replies

Avatar

Level 4

Most of the use cases I have seen with this class is modifying links: 

https://sling.apache.org/documentation/bundles/output-rewriting-pipelines-org-apache-sling-rewriter....

What comments are you referring to? 

Avatar

Level 5

We have something like this:

...

<!-- add="ASSOCIATE_1"
 <div class="image parbase section">
  <img src="/content/business/test-pages/components/abc/image.img.png" alt="image" class="img-responsive"/>
 </div>
-->
...

So we comment out the html for certain associates.  They can then uncomment and use accordingly ....

I want to use the Transformer Factory to change the following

original : <img src="/content/business/test-pages/components/abc/image.img.png" alt="image" class="img-responsive"/>

desired output :  <img src="/business/test-pages/components/abc/image.img.png" alt="image" class="img-responsive"/>

However, since it's part of a comment it does not get picked up for processing.

Any thoughts?

-Dean

Avatar

Level 10

HI Dean - i have not personally heard about using the Transformer to modify comments in code like this., I will check within Adobe. 

Avatar

Employee

Hi,

This would theoretically be possible but it requires your Transformer implement the characters() method and parse the contents of that text yourself. If the contents of these comments are regular, you might be able to handle this with regex, but if the HTML is "normal" HTML, it is far from regular enough to be handled in this way.

If you are going to go down this path, I'd highly recommend paying attention to slides 19 through 22 of http://www.slideshare.net/justinedelson/mastering-the-sling-rewriter (blatant self-promotion)

Regards,

Justin

Avatar

Level 5

Justin,

I'm open to trying anything ...

When I print out the attributes in the startElement method, I never see the comments. 

Is it just ignoring them?

The contents of the comments are extremely regular ... I'd like to be able to use regex.

I've reviewed the slides but I'm still not sure how I can change these things without the comments coming into the transformer.

Thanks,

-Dean

Avatar

Level 5

Justin,

I've added code to the Transformer, but I'm still not able to see the comments in the transformer.

<!-- add="ASSOCIATE_1"
  <div class="image parbase section">
   <img src="/content/business/test-pages/components/abc/image.img.png" alt="image" class="img-responsive"/>
  </div>
-->

Any suggestions on how to implement the characters() method to obtain the comments is GREATLY appreciated?

Thank you for all your help.

-Dean

Avatar

Employee

Right, like I said, you wouldn't see the commented elements in the startElement(). You will see them (as text) in the characters() method. startElement() is useless for this use case as those aren't actually elements. They are text (character data in XML parlance).

Avatar

Correct answer by
Employee

Here's a trivial example of a Transfomer which adds "HELLO" inside comments: https://gist.github.com/justinedelson/cdcf502413740bef9539faf74385a599