Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

airnavin30
Level 2
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
raj_mandalapu
Correct answer by
Community Advisor
Community Advisor

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

0 Replies
raj_mandalapu
Correct answer by
Community Advisor
Community Advisor

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