Skip to Content
avatar image
Former Member

Relace segment with segment = "1" in IDOC.

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Apr 25 at 04:10 PM

    Hi Evgeniy,

    Thankyou very much , working.

    Regards,

    Vijay.

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 25 at 08:22 AM

    Hi Vijay!

    And what exactly doesn't work?

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded

    • 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.