cancel
Showing results for 
Search instead for 
Did you mean: 

Adding BOM at beginning of the file in receiver side .txt file

0 Kudos

Hi all,

I want to generate .txt file at receiver  with file format UTF-16LE & BOM( beginning of the file).

for this i have written below logic in my custom adapter module, but i was generating file without BOM.

Could you please help me.It's very urgent.

//Read XML from payload

//        Adding BOM to output stream
          byte[] BOMByte = {(byte)0xFF,(byte)0xFF};
          out.write(BOMByte);
      TransformerFactory transformerFactory = TransformerFactory.newInstance();

      Transformer transformer = transformerFactory.newTransformer();

//Converting into UTF-16LE Format.

  transformer.setOutputProperty("encoding", "UTF-16LE");
  Result result = new StreamResult(out);

Regards,

Satish

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi

The code you have provided looks like for Java mapping not for the adapter module

are u using java mapping for your requirement?

0 Kudos

Hi Indrajit,

Thanks for your reply Indrajit.

we are implementing above requirement in PI7.0

Actually we have 2 option for my requirement.

1. adding BOM to the beginning of the file at message mapping level.

     - for this i have written above code(i am not expert in java), but file was not generating BOM        -at starting of the file.

     can you please correct my code if possible?

2. Custom adapter Module.

Could you please suggest which option is best to achieve above requirement.

Regards,

Satish

Former Member
0 Kudos

Hi Satish

I Think java mapping will be good enough for your requirement. Also UTF-16 requires BOM not UTF-16LE

try this code

Document newdoc = tbuilder.newDocument();

write logic and update the input data to this newdoc.

  byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };

  OutputStream out = arg1.getOutputPayload().getOutputStream();

  out.write(bom);

  Transformer transformer = TransformerFactory.newInstance()

  .newTransformer();

  Source source = new DOMSource(newdoc);

  Result output = new StreamResult(out);

  transformer.setOutputProperty("encoding", "UTF-16");

  transformer.transform(source, output);

0 Kudos

Hi Indrajit,

Still i am getting error at receiver communication channel monitoring from RWB

like below.

---------------

Message processing failed. Cause: com.sap.aii.af.ra.ms.api.RecoverableException: Exception in XML Parser (format problem?):'com.sap.engine.lib.xml.parser.NestedSAXParserException: Fatal Error: com.sap.engine.lib.xml.parser.ParserException: Unknown Byte-Order-Mark. XML MUST begin either with WhiteSpace or with '<?xml', and not: 'ÿÿþ'(:main:, row:1, col:0)(:main:, row=1, col=0) -> com.sap.engine.lib.xml.parser.ParserException: Unknown Byte-Order-Mark. XML MUST begin either with WhiteSpace or with '<?xml', and not: 'ÿÿþ'(:main:, row:1, col:0)': java.lang.Exception: Exception in XML Parser (format problem?):'com.sap.engine.lib.xml.parser.NestedSAXParserException: Fatal Error: com.sap.engine.lib.xml.parser.ParserException: Unknown Byte-Order-Mark. XML MUST begin either with WhiteSpace or with '<?xml', and not: 'ÿÿþ'(:main:, row:1, col:0)(:main:, row=1, col=0) -> com.sap.engine.lib.xml.parser.ParserException: Unknown Byte-Order-Mark. XML MUST begin either with WhiteSpace or with '<?xml', and not: 'ÿÿþ'(:main:, row:1, col:0)'

----------------

I have written my java mapping code like :

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

Document xml = documentBuilder.parse(in);

TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer = transformerFactory.newTransformer();

transformer.setOutputProperty("encoding", "UTF-16LE");

// Adding BOM to output stream

byte[] BOMByte = {(byte)0xFF,(byte)0xFF};

out.write(BOMByte);

Result result = new StreamResult(out);

Source domSource = new DOMSource(xml);

transformer.transform(domSource, result);

is it correct code??

Regards,

Satish

qws

Former Member
0 Kudos

Hi Satish

Change the encoding to UTF-16 and then test. If does not work , then try the below

byte[] BOMByte = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };


transformer.setOutputProperty("encoding", "UTF-16"); or transformer.setOutputProperty("encoding", "UTF-8");


0 Kudos

Hi Indrajit,

we are able to generating the file with :

byte[] BOMByte = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };

transformer.setOutputProperty("encoding", "UTF-8");

But when you open out file in hexadecimal editor showing Normal file only, it doesn't showing ff & fe at the

beginning of the file. see below screen shot.



we want to generate file with ff&fe at starting of the file.

Please find below screen shot.


@if you open test file which they provided us in notepad++.

    the encoding format as UCS-2 Little Endian.

Regards,

Satish

Former Member
0 Kudos

Hi

Change this line and see if it works or not.

byte[] BOMByte = new byte[] { (byte)0xFF, (byte)0xFE};

0 Kudos

Hi Indrajit,

Now we are facing below error :

---------------------------------------------

Message processing failed. Cause: com.sap.aii.af.ra.ms.api.RecoverableException: Exception in XML Parser (format problem?):'com.sap.engine.lib.xml.parser.NestedSAXParserException: Fatal Error: com.sap.engine.lib.xml.parser.ParserException: XMLParser: No data allowed here: (hex) 3f3c, 6d78, 206c(:main:, row:1, col:8)(:main:, row=1, col=8) -> com.sap.engine.lib.xml.parser.ParserException: XMLParser: No data allowed here: (hex) 3f3c, 6d78, 206c(:main:, row:1, col:8)': java.lang.Exception: Exception in XML Parser (format problem?):'com.sap.engine.lib.xml.parser.NestedSAXParserException: Fatal Error: com.sap.engine.lib.xml.parser.ParserException: XMLParser: No data allowed here: (hex) 3f3c, 6d78, 206c(:main:, row:1, col:8)(:main:, row=1, col=8) -> com.sap.engine.lib.xml.parser.ParserException: XMLParser: No data allowed here: (hex) 3f3c, 6d78, 206c(:main:, row:1, col:8)'

Regards,

Satish

gagandeep_batra
Active Contributor
0 Kudos

Hi Satish

What file type did you select at receiver CC "Text" or Binary

and also check the following blog that may help

http://scn.sap.com/community/pi-and-soa-middleware/blog/2012/04/27/receiver-channel-encoding-in-ansi...

0 Kudos

Hi Batra,

1. we are using PI7.0 and actual file requirement is below.


- UTF-16 Little endian
- preceded with a byte order marker
- Windows lineendings

- Tab delimiter
- use windows line endings (the line ending is shown by the two byte LF sequence 0x0A 0x00, it needs to be the four byte CRLF sequence 0x0D 0x00 0x0A 0x00)
- include a byte order marker (two bytes - 0xFF 0xFE - at the start of the file)

------

2. i have used below parameters in CC at receiver side

File Type   : Text

File Encoding : UTF-8

In Content Convertion Tab:

Field Separator : '0x09'

End Separator  : 'nl'

Can you Please advise whether we can achive above required format?

Regards,

Satish

0 Kudos

Hi Indrajit/Batra,

Can we achive below requirement using PI7.0?

Actual file requirement is below.


- UTF-16 Little endian
- preceded with a byte order marker
- Windows lineendings

- Tab delimiter
- windows line endings (the line ending is shown by the two byte LF sequence 0x0A 0x00, it needs to be the four byte CRLF sequence 0x0D 0x00 0x0A 0x00)
- include a byte order marker (two bytes - 0xFF 0xFE - at the start of the file)

Could you please advise me.

Regards,

Satish

Former Member
0 Kudos

Hi Satish

Do the following

Remove the encoding logic from Java mapping and pass the data as it is

In the receiver adapter choose the File Type as Text and encoding as UTF-16LE

Test and see if works or not.Then try this code in java mapping

byte[] BOMByte = new byte[] { (byte)0xFF, (byte)0xFE};

transformer.setOutputProperty("encoding", "UTF-16LE");


In the receiver adapter choose the File Type as Binary


The other parts you can handle it in the module tab

LF to CRLF you can use the module

http://blogs.prosoftgroup.com/common-utility-to-convert-end-of-line-in-sap-pi/

0 Kudos

Hi Indrajit,

I have done same way, still we are faceing below error message at receiver side CC.

Error_Mesg :

------------------

Message processing failed. Cause: com.sap.aii.af.ra.ms.api.RecoverableException: Exception in XML Parser (format problem?):'com.sap.engine.lib.xml.parser.NestedSAXParserException: Fatal Error: com.sap.engine.lib.xml.parser.ParserException: XMLParser: No data allowed here: (hex) feff, 3c, 3f(:main:, row:1, col:8)(:main:, row=1, col=8) -> com.sap.engine.lib.xml.parser.ParserException: XMLParser: No data allowed here: (hex) feff, 3c, 3f(:main:, row:1, col:8)': java.lang.Exception: Exception in XML Parser (format problem?):'com.sap.engine.lib.xml.parser.NestedSAXParserException: Fatal Error: com.sap.engine.lib.xml.parser.ParserException: XMLParser: No data allowed here: (hex) feff, 3c, 3f(:main:, row:1, col:8)(:main:, row=1, col=8) -> com.sap.engine.lib.xml.parser.ParserException: XMLParser: No data allowed here: (hex) feff, 3c, 3f(:main:, row:1, col:8)'

------

Is there any alternative solution?

Regards,

Satish

Former Member
0 Kudos

Hi

Have u removed the BOM code from java mapping. Please remove the code for BOM and encoding and then test.

Code :

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

Document xml = documentBuilder.parse(in);

TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer = transformerFactory.newTransformer();

Result result = new StreamResult(out);

Source domSource = new DOMSource(xml);

transformer.transform(domSource, result);

In the receiver adapter choose the File Type as Text and encoding as UTF-16LE

0 Kudos

Hi Indrajit,

Thanks for your quick reply.

Now we can able to generate the file, but if i open in Hex Editor it doesn't show

FF&FE at the starting of the file(without java mapping also we can generate this type of file).

How can we achive above requirement?

Regards,

Satish

0 Kudos

Hi All,

It's Very Urgent...Please

Can any one help me on this...

Can we do it using Custom adapter Module?

Regards,

Satish

praveen_b5
Explorer
0 Kudos

Hi Satish

Did you to meet your requirement, If so please share the correct code and process to add BOM to the file. Even I got the same requirement now to implement. Please help

Thanks

Praveen-