cancel
Showing results for 
Search instead for 
Did you mean: 

Assign HTML character entities

Former Member
0 Kudos

Hello,

I have a scenario where a message should be sent to an HTTP server using the HTTP receiver CC. In the message itself no special HTML characters such as <, > or / should be used but they should be replaced with special character entities such as &lt for < .

Is there an easy way to do this as it is quite a common requirement? Is there an adapter module available for this or must this be done in the mapping itself?

Thank you for your advice.

Edited by: Florian Guppenberger on Aug 11, 2009 6:54 PM

Edited by: Florian Guppenberger on Aug 11, 2009 6:55 PM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Florian,

Can you check stefans comments in this thread:

Regards,

---Satish

Answers (1)

Answers (1)

Former Member
0 Kudos

I also checked the link to the other trhead. So obviously the conversion to HTML character entities, just like &lt has to be done using a Java mapping.

Is there any sample code available? I think this is a common feature and there should be some kind of java template available.

This would be very helpful.

stefan_grube
Active Contributor
0 Kudos
public void execute(InputStream in, OutputStream out)
		throws StreamTransformationException {
try{    
int c;
    while ((c = in.read()) != -1) {
      switch (c){       
         case '&': out.write("&amp;".getBytes()); break;    
         case '\"': out.write("&quot;".getBytes()); break;
         case '\'': out.write("&apos;".getBytes()); break;
         case '<': out.write("&lt;".getBytes()); break;
         case '>': out.write("&gt;".getBytes()); break;
         default: out.write(c);
       }

    }
  } catch (IOException e) {
    throw new StreamTransformationException(e.getMessage());
  }


}

If that works for you, you can put this to wiki

Former Member
0 Kudos

Thank you very much for the sample. I will try the code and let you know afterwards (will take some days / weeks though as I have to finish all other requirements first)

stefan_grube
Active Contributor
0 Kudos

> public void execute(InputStream in, OutputStream out)

> throws StreamTransformationException {

> try{

> int c;

> while ((c = in.read()) != -1) {

> switch (c){

> case '&': out.write("&amp ;".getBytes()); break;

> case '&lt;': out.write("&lt ;".getBytes()); break;

> case '>': out.write("&gt ;".getBytes()); break;

> default: out.write(c);

> }

>

> }

> } catch (IOException e) {

> throw new StreamTransformationException(e.getMessage());

> }

>

>

> }

The SDN forum does not allow html escape sequences, so I put a space before the ; which you have to remove in your code.

' and " need not to be escaped, so I removed that lines.

Edited by: Stefan Grube on Aug 16, 2009 11:06 PM

Former Member
0 Kudos

Thank you for the code sample! What I was just thinking about is, that this should be done automatically when receiving the proxy, shouldn't it?

Otherwise it would not be valid XML on the integration server I guess.

And how would the code distinguish between "real" XML tags and content of elements? We will make a test and I let you know about the outcome.