cancel
Showing results for 
Search instead for 
Did you mean: 

Java Mapping expert advice

Former Member
0 Kudos

Hi all

I am using Java mapping to replace a string with another string in my outputed xml file. the code i am using is

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("ns0:");

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();

}

}

}

I picked this code from a different blog,This code is basically replacing "ns0" with "" . I am now required to tweak it so that the logic is like this .

replace all "ns0:" with "" and replace all "ns1:" with "" and replace all "ns2:" with "com:" and replace all occurenaces except the first one of "ns4" with "" and all occureance except the first one of "ns5" with "main"

How can i tweak the code to do all of these replacements in a single go

your expert java advice is well appricited.

Thank you

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Try this.. I have just removed few lines related to pattern and used another piece of code here..

let me know if any issues..

>

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

>

> >

> String aLine = null;

> int count = 0;

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

>

>

if ( count > 0 )

> out.write( aLine.replaceAll( "ns0:","" ).replaceAll("ns1:","").replaceAll("ns2:","com:").replaceAll("ns4","") );

else

out.write( aLine.replaceAll( "ns0:","" ).replaceAll("ns1:","").replaceAll("ns2:","com:") );

>

> out.newLine();

> if ( aLine.indexOf( "ns4") > 0 ) count++;

> }

>

Former Member
0 Kudos

Hi Anand ,

I added your piece of code , and it seems to be working as the way i want , I need to fine tune my requirements further and will let you know how it goes, thanks for the quick and efficient solution . Great stuff.

Former Member
0 Kudos

Achieved my requirement by following Anand;s suggestion, Great stuff , thanks a lot to all who responded.

Answers (2)

Answers (2)

former_member187563
Contributor
0 Kudos

hi,

I know you have got your result using java mapping but actually for your information in your case java mapping is not required.

This can be easily obtained using a already existing adapter module XMLAnonymizerBean.

you can refer to:

http://help.sap.com/saphelp_nw04/helpdata/en/2e/bf37423cf7ab04e10000000a1550b0/frameset.htm

regards,

ujjwal kumar

Former Member
0 Kudos

Hi Ujjwal

I tried the adapter module to remove the prefixes, But the results are not exactly how i wanted , the adapter module is pretty inflexible , you can only specify valid namespaces and prefixes in it. Where as XI is creating a namespace decleration for each new XML node which is not necessary. If i used adapter module then it either deletes all of the namespaces or keeps all of them. So I chose to go with java mapping , I also tried XSLT but that proved complicated too. Thanks for your advice anyway.

Shabarish_Nair
Active Contributor
0 Kudos

i would take this approach;

convert the input stream to a string say input.

use the replace all in succession to achieve your result.

String output = input.replaceAll("<source string1>", <"target string1">);
String output = input.replaceAll("<source string2>", <"target string2">);
String output = input.replaceAll("<source string3>", <"target string3">);

assign output to your outputstream.