cancel
Showing results for 
Search instead for 
Did you mean: 

Java mapping for removeing the invalid characters in file adapter

Former Member
0 Kudos

Hi All,

i got mapping error while picking the flat file from third party system using the FCC.

error is like this :"Runtime exception occurred during application mapping com/sap/xi/tf/_MM_TRS_; com.sap.aii.utilxi.misc.api.BaseRuntimeException:Fatal Error: com.sap.engine.lib.xml.parser.ParserException: Invalid char #0x0 (:main:, row:5, col:1~"

for this we have to add javamapping for( "deleting the special charecters") in operation mapping before our original mapping.

i searched in forums i got the below code :

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;

public class RemoveBadCharacters extends AbstractTransformation {

public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {

getTrace().addInfo("JAVA program RemoveBadCharacters is Initiated");
String inputPayload = convertInputStreamToString(arg0.getInputPayload().getInputStream());
String outputPayload = cleanUp(inputPayload);

try {
arg1.getOutputPayload().getOutputStream().write(
outputPayload.getBytes("UTF-8"));
} catch (Exception exception1) {
}
}

public String convertInputStreamToString(InputStream in) {
StringBuffer sb = new StringBuffer();
try {
InputStreamReader isr = new InputStreamReader(in);
Reader reader = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
sb.append((char) ch);
}
reader.close();
} catch (Exception exception) {
}
return sb.toString();
}

public String cleanUp(String instructions){
String res = "";
String c = "";
int hexCode = 0;

if (instructions != null && instructions.length()>0){ 


for (int i=0; i<instructions.length(); i++){
c = instructions.substring(i, i+1);
hexCode = c.hashCode();
if (hexCode>31 && hexCode<127 && hexCode != 96){
res = res.concat(c);
}
}
}
return res;
}
}

but i am not awaire how to use this program in XI .

i tried to import this in XI through imported archives by naming the files with .jar

but it gives the error like its not a ".jar" file

pleasse let me know the basic process to use java mapping in XI.

like how to create the jar files ?

Thanks in advance

Accepted Solutions (1)

Accepted Solutions (1)

former_member200962
Active Contributor
0 Kudos

the code that you see above forms the .java file....you need to install a JAVA compiler (like NWDS) and then complie the above code into it....the file that you include in XI/ PI can be a .jar file or a .zip file (with the .class file included).

NWDS can be downloaded from service marketplace (need to have a S-user id).

Former Member
0 Kudos

Hi Abhishek,

thanks for your reply

can you please let me know whether the above code able to remove the special character like "&" "#" in the payload.

sender CC able to pick the data using the FCC but while sending to receiver i got the error like invalid char.

Please let me know your suggestion here how to proceed.

Thanks in advance

anupam_ghosh2
Active Contributor
0 Kudos

Hi,

Have you inspected what special characters are causing exception to occur? Now in XML, parser will throw exceptions for the following characters ampersand,less than,gtreater than, single quote and back slash. Exceptions will also be raised if chinese special characaters are received through communication channels. In those cases you need to change the communication channel encoding scheme. Now I assume your present problem is with ampersand,less than,gtreater than, single quote and back slash this can be removed with UDF only in message mapping. Just try this UDF given below



public String removeSpecialCharacters(String s,Container container){ 

s=s.replaceAll("&","&amp;");
s=s.replaceAll("<" , "& lt;");
s=s.replaceAll(">", "& gt;");
s=s.replaceAll("'", "&apos;");
s=s.replaceAll("\"", "& quot;");
//if you want to remove ampersand instead of replacement
s=s.replaceAll("&","");
//to remove # 
s=s.replaceAll("#","");
return s;
}

The special characters are not getting printed properly in the code here, except the first(with ampersand) and the fourth lines(with single quote) , so sometimes I included spaces, again comma is also not being printed in second line(less than). For proper replacement in characters you refer to this link http://support.microsoft.com/kb/316063. Use this UDF for each field in mapping where you are getting special characters.

Please follow the same way as wrote the first line of replacing the ampersand. Check if this works.

In place of UDF you can use text function "replaceString" to replace specific characters.

regards

Anupam

Edited by: anupamsap on Sep 30, 2011 10:00 AM

Edited by: anupamsap on Sep 30, 2011 11:12 AM

Edited by: anupamsap on Oct 1, 2011 10:37 PM

stefan_grube
Active Contributor
0 Kudos

> Now I assume your present problem is with ampersand,less than,gtreater than, single quote and back slash this can be removed with UDF only in message mapping. Just try this UDF given below

I am sorry to have to say that this answer is absolutely wrong.

1. The characters that you mention are valid characters of XML

2. You cannot use an UDF to replace those characters

3. When you have a parser error, the mapping will not be executed, so this code is useless anyway.

4. You can see in the error message, that a character hex 00 causes the trouble

anupam_ghosh2
Active Contributor
0 Kudos

Hi Stefan,

I encountered a problem once when an xml had ampersand in it. When I was trying to view output of a java mapping in IE, it was throwing an error. I consulted various website which said that due to presence of certain characters the XML is not well formed. Then I removed them within java mapping code and I was able to view the XML structure. Now this was a special case since I was directly reading the file contents into java mapping code and there was no FCC being used. Thus in source side did not have any XML structure, so the replacement worked, and I concluded that if these characters are present in source XML, parser cannot parse the document at all.

you pointed out that "1. The characters that you mention are valid characters of XML"

When I check these links

http://www.w3schools.com/xml/xml_syntax.asp and http://www.w3.org/TR/REC-xml/#syntax

I find "less than" symbol and "ampersand" are strictly illegal in XML in their literal form except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section.

Here in present case I did not read the error message carefully, hence missed the HEX code "0x0" completely. Here is one of the pages, I consulted http://xml.silmaril.ie/specials.html. The problem here as you pointed out the Source XML itself will not be formed, so UDF will not work at all. Here the solution is as you pointed out, that special characters like 0x0 is removed by sender.

Finally if sender is unable to remove characters in his end , then alternative is not to use FCC, but read the file content directly into a java mapping code. This java mapping will remove the special characters and form the target XML. The UDF might be used within the java mapping code to remove the characters and this article may be followed , here the author explains the method to read CSV file and convert it into target XML without FCC.

Thank you for pointing out the mistake in my earlier post.

regards

Anupam

Edited by: anupamsap on Oct 4, 2011 2:28 AM

Former Member
0 Kudos

Hi All,

thanks for your reply

sorry for the late response,

i solved my problem with out using the message mapping.Becuase my Source & Target structures are same so i used the single structure for both source & Target.

now all special charecters are processed.

Thanks for your response

Answers (1)

Answers (1)

stefan_grube
Active Contributor
0 Kudos

First of all: I do not know where you found this Java program, but this is rubbish. This will destroy your payload.

You cannot just use any kind of Java program which you find anywhere. If you do not understand Java, understand the way how Java mapping works, how to compile and deploy a Java program on PI, you will never be able to solve your problem by a Java program.

Your error message states, there is a non-valid character char #0x0 in your XML file.

That means, that the sender provides a non-valid file.

So ask the guy who is responsible for this to fix it. Do not even try to fix wrong data inside PI.

PI can only transport valid XML. There are rules for XML. There must not be any #0x0 inside.