on 10-31-2011 12:43 PM
Hello,
I've got a problem with character encoding in JAVA mappings.
I set a simple JAVA mapping, than only reads the input stream and passes it to the output stream.
Next the java code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Map;
import com.sap.aii.mapping.api.*;
import com.sap.aii.mapping.api.StreamTransformationException;
public class GenerateSOAPHeader extends AbstractTransformation {
public void transform(TransformationInput in, TransformationOutput out)
throws StreamTransformationException {
String outputPayload = new String();
String inputPayload = new String();
try {
// read input payload from stream
inputPayload = convertInputStreamToString(in.getInputPayload()
.getInputStream());
} catch (Exception e) {
throw new StreamTransformationException(e.getMessage());
}
outputPayload = inputPayload;
try {
// write output payload to stream
out.getOutputPayload().getOutputStream().write(outputPayload.getBytes());
} catch (IOException e) {
throw new StreamTransformationException(e.getMessage());
}
}
public void setParameter(Map arg0) {
// TODO Auto-generated method stub
}
public String convertInputStreamToString(InputStream in) {
StringBuffer sb = new StringBuffer();
String Newpayload = "";
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) {
}
/*
* The following is to remove the <?xml version="1.0"
* encoding="UTF-8"?> from the input message payload
*/
Newpayload = sb.toString();
int len = Newpayload.indexOf(">");
Newpayload = Newpayload.substring(len + 1);
return Newpayload;
}
}
If I use command
out.getOutputPayload().getOutputStream().write(outputPayload.getBytes());
to write the outputstream all character, like ä ö ü (german umlaute), are converted to ?, e.g.
<Tag>äöü</Tag> mapped to <Tag>??????</Tag>
If I set the output encoding to UTF-8
out.getOutputPayload().getOutputStream().write(outputPayload.getBytes("UTF-8"));
I dont get ? but following:
<Tag>äöü</Tag> mapped to <Tag>äöü</Tag>
Please advice.
I think the issue comes up as a add byte to a string buffer. So when you have a 2-byte character, then it is appended twice to the string buffer.
I had a similar task and I used following code:
// convert InputStream to String
byte[] bbuf = new byte[in.available()];
int bblen = in.read(bbuf);
String inputConverted = new String(bbuf,"UTF-8");
inputConverted = inputConverted.replaceAll("<\\?xml version=\"1\\.0\" encoding=\"UTF-8\"\\?>","");
So I create a byte array first and transform it to a string at once.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Stefan,
Would you be please able to help me with your code, I am having similar problem. I have written java mapping to add header and trailer record to the content of the file. I am now having issues handling special character - ’ (This is actually not the apostrophe) and it gets converted into some unknown value - ’sÂ
Here is my code, Could you please tell me where should I plugin your above said code -
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class renameAD_APD_File extends AbstractTransformation {
private static final DynamicConfigurationKey KEY_FILENAME_AS2 =
DynamicConfigurationKey.create("http://seeburger.com/xi/common/dtAS2FileName", "DYNFILENAME");
private static final DynamicConfigurationKey KEY_FILENAME_FILE =
DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
String newFileName = null;
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date date = new Date();
String output = null;
String strFileContent = null;
DynamicConfiguration conf = arg0.getDynamicConfiguration();
String oldValue = conf.get(KEY_FILENAME_FILE);
oldValue = oldValue.replaceAll(".dat","");
String newValue = "Outbound_" + dateFormat.format(date) + ".dat";
String inData = convertStreamToString(arg0.getInputPayload().getInputStream());
String header = "000000000000" + dateFormat.format(date) + "V3.0ITEM"; // the header string
String trailer = "999999999999"; // the trailer string
String outData = header + "\n"+ inData + trailer + "\n" ; // final payload to be written back
try
{
//The JAVA mapping output payload is returned using the TransformationOutput class
arg1.getOutputPayload().getOutputStream().write(outData.getBytes("UTF-8"));
conf.put(KEY_FILENAME_AS2,newValue);
}
catch(Exception exception1) { }
}
public String convertStreamToString(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();
}
}
Hi,
Please kindly try this code.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.api.StreamTransformationException;
public class GenerateSOAPHeader extends AbstractTransformation {
public void encoding(InputStream in,OutputStream out) throws StreamTransformationException
{
try
{
String outputPayload = new String();
String inputPayload = new String();
String newEncoding="ISO-8859-1";
inputPayload = convertInputStreamToString(in);
outputPayload = new String(inputPayload.getBytes(newEncoding),newEncoding);
out.write(outputPayload.getBytes(newEncoding));
}
catch(Exception e)
{
throw new StreamTransformationException(e.getMessage());
}
}
public void transform(TransformationInput in, TransformationOutput out)
throws StreamTransformationException {
this.encoding(in.getInputPayload().getInputStream(), out.getOutputPayload().getOutputStream()) ;
}
public String convertInputStreamToString(InputStream in) throws IOException{
String sb="";
String Newpayload = "";
String newEncoding="ISO-8859-1";
try {
int c;
while(2>1)
{
c=in.read();
if(c<0)
{
break;
}
sb+=(char)c;
}
}
catch (Exception e) {
throw new IOException(e.getMessage());
}
/*
* The following is to remove the <?xml version="1.0"
* encoding="UTF-8"?> from the input message payload
* and add new encoding scheme.
*/
Newpayload = sb.toString();
int len = Newpayload.indexOf(">");
Newpayload = Newpayload.substring(len + 1);
Newpayload="<?xml version="1.0" encoding=""+newEncoding+""?>" +Newpayload;
return Newpayload;
}
public static void main(String args[])
{
try
{
FileInputStream in=new FileInputStream("C:\Apps\test\myjava.xml");
FileOutputStream out=new FileOutputStream("C:\Apps\test\myjava11.xml");
GenerateSOAPHeader g=new GenerateSOAPHeader();
g.encoding(in, out);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
you may delete the main() method and use the code.
Sample input xml myjava.xml
<?xml version="1.0" encoding="UTF-8"?>
<ns0:mt_emp xmlns:ns0="urn:javamappingf2f">
<employee>
<name>
<firstname>äöü</firstname>
<lastname>hi</lastname>
<country>cou</country>
</name>
</employee>
</ns0:mt_emp>
output xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<ns0:mt_emp xmlns:ns0="urn:javamappingf2f">
<employee>
<name>
<firstname>äöü</firstname>
<lastname>hi</lastname>
<country>cou</country>
</name>
</employee>
</ns0:mt_emp>
Hope this solves your problem. I have done few changes in your code to interpret special characters and changed the encoding scheme of the xml.
regards
Anupam
NB:- just did little changes to the code.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
85 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.