cancel
Showing results for 
Search instead for 
Did you mean: 

Relace segment with segment = "1" in IDOC.

Former Member
0 Kudos

Hi Experts,

In PO , for IDOC mappings we need to pass "1" for Segment , begin and IDOC. I just need to do pass through the Java mapping , only change required is segment and IDOC with 1. I know we can do one to one mapping, but we have jumbled segments with mapping. So tried the java mapping like below but not working. Can some help here.


import java.io.*;
import com.sap.aii.mapping.api.*;
public class WellformedXML_JavaMapping extends AbstractTransformation {
@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {
try {
InputStream inputstream = transformationInput.getInputPayload().getInputStream();
OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();

byte[] b = new byte[inputstream.available()];
inputstream.read(b);
String inputContent = new String(b);

inputContent = inputContent.replaceAll("SEGMENT", "SEGMENT='1'");


outputstream.write(inputContent.getBytes());
} catch (Exception exception) {
getTrace().addDebugMessage(exception.getMessage());
throw new StreamTransformationException(exception.toString());
}
}
}

Regards,

Ram.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Evgeniy,

Thankyou very much , working.

Regards,

Vijay.

Answers (1)

Answers (1)

former_member190293
Active Contributor
0 Kudos

Hi Vijay!

And what exactly doesn't work?

Regards, Evgeniy.

Former Member
0 Kudos

HI Evgeniy,

Segment is not getting replaced with segment = "1". that is the actual requirement. please help me with the actual code.

Regards,

Vijay.

former_member190293
Active Contributor
0 Kudos

Hi Vijay!

It's hard to say what's wrong with your code at first sight. Here is my class doing the same:

public class StringReplacer extends AbstractTransformation {
@Override
public void transform(TransformationInput in, TransformationOutput out)throws StreamTransformationException {
executeMapping(in.getInputPayload().getInputStream(), out.getOutputPayload().getOutputStream());
}

public void executeMapping(InputStream is, OutputStream os) throws StreamTransformationException {
try {
byte[] payloadBytes = getByteArrayFromInputStream(is);
String payloadString = new String(payloadBytes, "UTF-8");
payloadString = payloadString.replaceAll("SEGMENT", "SEGMENT=\"1\"");
os.write(payloadString.getBytes("UTF-8"));
}
catch (Exception e) {
throw new StreamTransformationException(e.getMessage());
}
}

public byte[] getByteArrayFromInputStream(InputStream is) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1)
  buffer.write(data, 0, nRead);
buffer.flush();
is.close();
return buffer.toByteArray();
}
}

It works as expected.

Regards, Evgeniy.