Skip to Content

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

6 Comments
You must be Logged on to comment or reply to a post.