Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to handle the content with a Non-breaking hypen for example company name like V-John

Avatar

Level 2

I have a issue related to one name display in a content while authoring or rendering like for a company name like V-John,

In the rendered content every where the name should display as V-John and not like V John or V (new line)

John or V ‑  John So all such occurrences should be taken always same as V-John.

 

What could be more generic solution (Preferred any backend/java utility method ) here that can be applicable for all text like components like RTE etc  

1 Accepted Solution

Avatar

Correct answer by
Level 8

We can use sling transformers to rewrite V John to V-John, because we don't know in which component this text comes, it is difficult to go and modify every component and also when it comes to maintenance it is tough.

With this TransformerFactory you can rewrite URLs, modify HTML elements content, add attributes to HTML elements, etc. you can refer below the sample code, it is reading image src attribute and changing it is CDN based URL.

  public void startElement(String uri, String loc, String raw, Attributes attributes) throws SAXException {
        AttributesImpl attrs = new AttributesImpl(attributes);
            for (int i = 0; i < attrs.getLength(); i++) {
                String name = attrs.getLocalName(i);
                if(name.equals("src"))
                {
                    String url = attrs.getValue(i);
                    url = "http://cdn.coderss.in" + url;
                    attrs.setValue(i, url);
                }
            }
        super.startElement(uri, loc, raw, attrs);
    }

in your case, you need to read the text of the paragraph inside the startElement method and look for V John and change it to V-John wherever you find. 

For more information, you can refer to this 

http://www.coderss.in/apache-sling-rewriter-how-to-rewrite-urls-in-aemcq5/

https://www.cognifide.com/our-blogs/zen-garden/aem-transformers

You can also configure this to run only certain elements and only for specific paths.

<custom-pipeline
   jcr:primaryType="nt:unstructured"
   contentTypes="text/html"
   generatorType="htmlparser"
   order="1"
   paths="[/content/aemtookit]"
   serializerType="htmlwriter"
   transformerTypes="[linkchecker,preserve-wcmmode-transformer]">
   <generator-htmlparser
      jcr:primaryType="nt:unstructured"
      includeTags="[A,/A,IMG,AREA,FORM,BASE,LINK,SCRIPT,BODY,/BODY,DIV,/DIV]" />
</custom-pipeline>

 

View solution in original post

1 Reply

Avatar

Correct answer by
Level 8

We can use sling transformers to rewrite V John to V-John, because we don't know in which component this text comes, it is difficult to go and modify every component and also when it comes to maintenance it is tough.

With this TransformerFactory you can rewrite URLs, modify HTML elements content, add attributes to HTML elements, etc. you can refer below the sample code, it is reading image src attribute and changing it is CDN based URL.

  public void startElement(String uri, String loc, String raw, Attributes attributes) throws SAXException {
        AttributesImpl attrs = new AttributesImpl(attributes);
            for (int i = 0; i < attrs.getLength(); i++) {
                String name = attrs.getLocalName(i);
                if(name.equals("src"))
                {
                    String url = attrs.getValue(i);
                    url = "http://cdn.coderss.in" + url;
                    attrs.setValue(i, url);
                }
            }
        super.startElement(uri, loc, raw, attrs);
    }

in your case, you need to read the text of the paragraph inside the startElement method and look for V John and change it to V-John wherever you find. 

For more information, you can refer to this 

http://www.coderss.in/apache-sling-rewriter-how-to-rewrite-urls-in-aemcq5/

https://www.cognifide.com/our-blogs/zen-garden/aem-transformers

You can also configure this to run only certain elements and only for specific paths.

<custom-pipeline
   jcr:primaryType="nt:unstructured"
   contentTypes="text/html"
   generatorType="htmlparser"
   order="1"
   paths="[/content/aemtookit]"
   serializerType="htmlwriter"
   transformerTypes="[linkchecker,preserve-wcmmode-transformer]">
   <generator-htmlparser
      jcr:primaryType="nt:unstructured"
      includeTags="[A,/A,IMG,AREA,FORM,BASE,LINK,SCRIPT,BODY,/BODY,DIV,/DIV]" />
</custom-pipeline>