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

To report this post you need to login first.

5 Comments

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

  1. Hubery Yao

    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

    (0) 
  2. Apu Das

    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

    (0) 
    1. Praveen Tirumareddy Post author

      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

      (0) 

Leave a Reply