cancel
Showing results for 
Search instead for 
Did you mean: 

handling special characters in SAP PI

abhay_aggarwal
Participant
0 Kudos

Hi Experts,

I have proxy to JMS scenario(CRM-PI-JMS). I am getting the some invalid data in payload from proxy.

<Delivery_Instructions>Deliver. Setup laundry, fridge and stove. Call TIM – 415-987-9998 1 hrb4 del. Unit #205</Delivery_Instructions><

<Delivery_Instructions>HRS 9A-3P CL SHERIDAN 707-747-4885 1HR B4 DEL TO CA3-110-01-01..UNCR PI &amp; INSPECT M/W… SGN RQD</Delivery_Instructions>

which is getting converted in junk  €“ characters.

I wrote the java class which is handling the the data correctly.


package specialCharHandler;

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.util.Map;

import com.sap.aii.mapping.api.StreamTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

public class SpecialCharHandler implements StreamTransformation {

    @Override

    public void execute(InputStream in, OutputStream out) throws StreamTransformationException {

        StringBuffer sb = new StringBuffer();

        try{

           BufferedReader br = new BufferedReader(new InputStreamReader(in));

           String strLine;

           String temp = null;

           while ((strLine = br.readLine()) != null) {

               if(strLine.indexOf("–") != -1){

                     temp = strLine.replace("–","-");

                     sb.append(temp);

                    }

               if(strLine.indexOf("… ") != -1){

                   temp = strLine.replace("… ",".");

                   sb.append(temp);

                  }

            else

                sb.append(strLine);

           }

           in.close();

           out.write(sb.toString().getBytes());

           out.flush();

           out.close();

          } catch (Exception e) {

              e.printStackTrace();

          }

    }

   

    @Override

    public void setParameter(Map arg0) {

       

    }}

code is working fine. I wanted to know is there any other way where I can handle this data using any bean in adapter module or I have to put all the possible condition in my java code.

Regards,

Accepted Solutions (0)

Answers (2)

Answers (2)

Bhargavakrishna
Active Contributor
0 Kudos

Hi Abhay,

Since you are working with proxyies, you can add ABAP script in sender Proxy for eliminating or replacing with NULL or empty.

or

create mapping and use this UDF

String b = "";

b = a.replaceAll(",", "0");

b = a.replaceAll("@", "0");

Like all the special characters you can take and replace with zero.

...........

return b;

Regards

Bhargava krishna

abhay_aggarwal
Participant
0 Kudos

Hi Bhargava,

replacing the characters is the only solution ?

Can we use the below code:

package com.sap.encoding.example;

import java.io.*;

import java.util.*;

import com.sap.aii.mapping.api.*;

public class UTFISO implements StreamTransformation {

private final String latin = "ISO-8859-1";

private final String utf ="UTF-8";

public void execute(InputStream in, OutputStream out) {

try {

DataInputStream stdin = new DataInputStream(in);

int length = getLengthFromStream(stdin);

stdin.reset();

byte[] buffer = getBytesFromStream(stdin);

String str = new String(buffer, utf);

str = str.replaceAll(utf, latin);

out.write(str.getBytes(latin));

out.close();

} catch (IOException e) {

}

}

public int getLengthFromStream(InputStream is ) throws

IOException{

int i = 0;

int length = 0;

try{

while ((i = is.read())> 0){

length ++;

}

}catch (ArrayIndexOutOfBoundsException e) {

e.printStackTrace();

}

return length;

}

public byte[] getBytesFromStream(InputStream is) throws

IOException {

// Create the byte array to hold the data

byte[] bytes = new byte[getLengthFromStream(is)];

is.reset();

// Read in the bytes

int offset = 0;

int tmp = 0;

while (offset < bytes.length

&& (is.read(bytes, offset, offset +

(tmp = is.available()))) > 0) {

offset += tmp;

}

// Ensure all the bytes have been read in

if (offset < bytes.length) {

throw new IOException("Could not

completely read Inputstream");

}

// Close the input stream a

is.close();

return bytes;

}

public void setParameter(Map param) {

}

}

Can we take complete file and convert the unicode file into UTF 8

Regards,

iaki_vila
Active Contributor
0 Kudos
abhay_aggarwal
Participant
0 Kudos

Hi Vila

Thanks for the document I will check with Java code which is in PDF.

Regards,