Java Mapping: To change the encoding type or replacing special characters.
This document is intended to change the encoding type of any document using java mapping. I have written a code which is commented in the below code which can help to remove the special characters also.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class EncodFile extends AbstractTransformation {
String strData = null;
@Override
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
getTrace().addInfo(“File Reading started “);
String strData = convertStreamToString(arg0.getInputPayload().getInputStream());
getTrace().addInfo(“File Reading successfully completed “);
// If special characters needs to be removed un-comment next two lines.
// strData = strData.replaceAll(“[^\\p{ASCII}]”, “”);
// getTrace().addInfo(“Bugs removed”);
try {
getTrace().addInfo(“Generating XML started”);
// Specify the Encoding type you would like to have in Below line(un comment one you like).
// arg1.getOutputPayload().getOutputStream().write(strData.getBytes(“ISO-8859-1”));
arg1.getOutputPayload().getOutputStream().write(strData.getBytes(“UTF-8”));
getTrace().addInfo(“Generating XML started”);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String convertStreamToString(InputStream in){
StringBuffer sb = new StringBuffer();
try
{
InputStreamReader isr = new InputStreamReader(in);
Reader reader =
new BufferedReader(isr);
int ch;
while((ch = in.read()) > -1) {
sb.append((char)ch);}
reader.close();
}
catch(Exception exception) { }
return sb.toString();
}
}
thanks and regards,
Praveen T
Hello Praveen,
For Operation mapping, there are 3 styles: message mapping, JAVA mapping and XSL. Message mapping is common for most of PI projects. To be honest, so far , I never use JAVA mapping in PI project. Do you have some articles about JAVA mapping? How to use it. It refer to the NWDS or Eclipse?
Thank you for your support in advance!
Thanks & Best regards,
Hubery
Hi Yao,
you can refer the below links to understand the java mapping through NWDS.
Change existing JAVA mapping using NWDS in SAP PI
http://scn.sap.com/community/pi-and-soa-middleware/blog/2015/02/08/how-to-create-java-mapping-in-sap-pi-po
Implementing a Java Mapping in SAP PI
Regards,
Srikanth Mavuri.
It is useful articles. Thank you so much for your reply, Srikanth.
Many thanks & best regards,
Hubery
Hi Praveen,
This is going to work when ur FCC is converted you file content in CC and it comes to MM.Them we can add this in OM to remove special character.
What if our FCC cant work properly due to some special character which is violating xml norms and data cant even come upto MM.
Thanks,
Apu Das
Hi Apu,
Valid point but there is a work around.You simply remove the FCC and use the java mapping in operation mapping. It will not throw any error.
Hope this answers your query.
thanks and regards,
Praveen T
Hi All,
I have implemented same java mapping for handle special characters, when i test in EST it’s working as expected. But, when i do 2e2 testing, special characters are coming in different format
Test In ESR
in e2e Test:
Please suggest what i need to configure, in sender & receiver channel I am adding any module parameters
Appreciate your time,
Binod