Expand my Community achievements bar.

SOLVED

How to Rewrite Plain text output not <HTML attribute>

Avatar

Level 4

com.day.cq.rewriter.pipeline.RequestRewriter or org.apache.sling.rewriter.Transformer helps to rewrite any html tag's attribute value that i am able to do it.

But I am trying to do rewrite plain text like {string.token) with any value.

for example :

Here is content in CRX when it would be rendered , {string.token} should be replaced with some dynamic value..... if i place that token as attribute in <p> i can do easily but i want that as plain text not as attribute of html tag. any thought?

<p> Hello world {string.token) , I am good<p>

Thanks Chandra

1 Accepted Solution

Avatar

Correct answer by
Employee

Chandra-

I don't understand your reference to i18n in the second post.

But regarding the overall question, this is possible with a Transformer pipeline component. In SAX, characters are passed to the characters() method.

View solution in original post

11 Replies

Avatar

Level 4

on i18n appraoch, i want this to be authorable <fmt:message key="xxx"> Can you suggest something so that it's not fixed in template or component and content author is able to do it from edit Dialog etc?

Avatar

Level 4

Please refer to the API..http://dev.day.com/docs/en/cq/current/javadoc/com/day/cq/rewriter/pipeline/RequestRewriter.html

Can we only rewrite only attribute of Element? not value of element?

Thanks Chandra

Avatar

Correct answer by
Employee

Chandra-

I don't understand your reference to i18n in the second post.

But regarding the overall question, this is possible with a Transformer pipeline component. In SAX, characters are passed to the characters() method.

Avatar

Level 4

Right Justin,

Here is piece of code it might be useful for other.

 public void characters(char[] ac, int i, int j) throws SAXException{
            
            String str = new String (ac, i, j);
            LOG.info(" Str)
            if(str.contains("{chandra}")){                
                
                String newStr=str.replace("{key}", "replaced dsnfb fdsfbmsdbfmbds fm bdsf mbfmbsdm fbsm htttp://rrrrv" );
                getContentHandler().characters( newStr.toCharArray(), 0, newStr.length()) ;
            }     else{
                getContentHandler().characters( ac,  i,  j) ;
                
            }      
       }

Avatar

Level 2

How to rewrite XML response with correct configuration?

i have custom transformer( that is global one name="pipeline.mode" value="global") which has the logic to rewrite public urls from html and xml respose but it is working for html now i want to extend this for xml as well how to do that?
I need right configuration.

I am overriding characters(), but i have to do configuration to trigger the handler then how?

Avatar

Level 1

Hi Chandra/Justin

I have the similar kind of business to implement like Chandra mentioned, But small difference I need to update the content in complete HTML of the page by searching the pattern of given String something like $$SEARCH_STR$$ under elements like <p>,<nav> etc and in any kind of html element which as Chandra mentioned Transformer's start element would never retrieve those elements line content to update it.

But the Characters() method helps me here its retrieves the complete HTML lines from content and updating.

My question to Justin here, Is it possible to achieve the same just by Pipeline Component Configuration under the apps without a Customer Transformer Java which overriding the Characters method(What Me & Chandra achieved for content update) ?

If so, In my case the Search Key and Replace value have defined under ACS Commons generic list, How I will retrieve those ACS generic list and provide in Pipeline component configuration.

Kindly suggest your thoughts. Thanks!

Avatar

Employee

As far as I know, there's no existing Transformer which does what you are describing, so you would need to write one.

That said, if you are just doing String substitution across the entire HTML document and don't care about the semantic meaning of specific HTML elements, then it is possible that the Rewriter is not the right solution for you and a simple Servlet Filter would be more appropriate (and be slightly simpler to write).

Avatar

Level 1

Thanks @justin_at_adobe

For you quick response!

Avatar

Level 1

@justin_at_adobe

As suggested i have created the Custom transformer like below which works fine in Author and Publish. But my html not getting rewritten by the Transformer in Dispatcher.

I dont have clue here!

Not sure whether the transformer is getting called in dispatcher or not.

or

May be the content with my search [[keys]] getting cached in dispathcer while publish/activate the page, hence not able to replace the [[keys]] in html content with replace value in dispatcher as like in Author and Publish.

Do you have any idea to invoke the transformer in dispatcher to rewrite the html content using custom transformer ?

/**
* {@inheritDoc}
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {

  String pageContent = new String(ch, start, length);

   boolean tokenReplaced = false;

   LOG.info("TokenReplacementTransformer called here - 22222");

   try {

   if (validSearchPath) {

  Pattern pattern = Pattern.compile("\\[\\[(.*?)\\]\\]");

  Matcher matcher = pattern.matcher(pageContent);

   while (matcher.find()) {

   LOG.info("TokenReplacementTransformer called here - 33333");

  String key = matcher.group();

  pageContent = replace(pageContent, key, searchTokenMap.get(key));

  tokenReplaced = true;

  }

  }

  } catch (PatternSyntaxException e) {

   LOG.error("Error while parsing the key", e);

  }

   if (tokenReplaced) {

  getContentHandler().characters(pageContent.toCharArray(), 0, pageContent.length());

  } else {

  getContentHandler().characters(ch, start, length);

  }

}

Avatar

Level 1

@justin_at_adobe Sorry, the transformer called in Dispatcher. it's my bad not verified the logs properly, kindly ignore the original message. Thanks!