Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

Introduction

XML special characters like & are getting correctly encoded by the Flat File Adapter. ASCII characters less than hex 20 do not get replaced with some dummy characters set by a codepage so the mapping fails at runtime. This document is a guide to remove escape sequences from a file by using a java mapping.

This guide is rewritten from blog: Implementing a Java Mapping in SAP PI: Implementing a Java Mapping in SAP PI


Overall solution: Implements a Java class for mapping purpose require the next steps:

  1.- Development Tool like SAP Netweaver Developer Studio, NetBeans, Eclipse ....

  2.- Import aii_map_api.jar library.

  3.- Write the Source code for the mapping program. (We need to know DOM or SAX parsing proccesing)

  4.- Generate .jar file with the compiled class.

  5.- Import Jar File in SAP PI.

  6.- Use the program mapping in Interface Mapping. 


1.- Open a Development Tool.

Firstly, we are going to open the SAP Netweaver Developer Studio for create a new java project.

 



2.- Next, we must to import the aii_map_api.jar library. The file aii_map_api.jar is located in
     oolkitJava MappingSoftwareaii_map_api.jar directory.

 

 

 

2 a.- Create a package

 

 

 

2 b. - Create a class

 

 

 

3.-  Write the Java Class Source. (copy this code into the java class Xi_test_class)

This code is reprogrammed from forum thread: File with ASCII Control Characters causes runtime exception in mapping:
https://forums.sdn.sap.com/click.jspa?searchID=11198782&messageID=5026757

 

 

package xi_test_package;

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.util.HashMap;

import java.util.Map;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

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

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

 

public class Xi_test_class implements StreamTransformation {

      private Map param = null;

 

            public void setParameter (Map param) {

                  this.param = param;

                  if (param == null) {

                        this.param = new HashMap();

                  }

            }

     

            public void execute(InputStream inStream, OutputStream outStream) throws StreamTransformationException{

                  try{

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

                        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outStream));

     

                        // The pattern matches control characters

                        Pattern p = Pattern.compile("[^ -~]");

     

                        Matcher m = p.matcher("");

                        String aLine = null;

                        while((aLine = in.readLine()) != null) {

                             m.reset(aLine);

                             //Replaces control characters with an empty string.

                             String result = m.replaceAll("?");      

                             out.write(result);

                             out.newLine();

                        }

                        in.close();

                        out.close();                            

                  }catch(Exception e){

                        e.printStackTrace();

                  }

            }

      }

  

4.- Generate JAR file.

Go to File->Export in SAP Netweaver Developer Studio a choose the project where you have developed the mapping program and you must export it to a file.

 

5.- Import JAR File.

Go to Integration Repository, Mapping Object -> Imported Archives. You must to create a new Imported Archives and import the file that you had generated in SAP NDS.

 

 

6.- Use the program mapping in Interface Mapping

Finally, you have to use the java class in an Interface Mapping. Use the java mapping before your regular mapping to remove ASCII characters less than hex 20 to replace them with a space so your regular mapping will not fail at runtime.