on 03-28-2014 7:35 AM
Hi Experts,
I am practicing the java mapping in PI, My requirement is simple I just want to concatenate the two input strings. And my code is like this
I have few doubts :-
1) execute method is mandatory
CODE :-
import java.io.*;
import java.util.*;
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;
import com.sap.aii.mapping.api.AbstractTrace;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class Java_Test extends AbstractTransformation {
public String var1;
public String var2;
public void transform(TransformationInput input, TransformationOutput output)throws StreamTransformationException
{
InputStream ins=input.getInputPayload().getInputStream();
OutputStream outs=output.getOutputPayload().getOutputStream();
int n,m;
String result=null;
try{
DocumentBuilderFactory dfactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dbuilder=dfactory.newDocumentBuilder();
Document doc=dbuilder.parse(ins);
NodeList nl=doc.getElementsByTagName("PMI_SOURCE_PROD");
NodeList nl1=doc.getElementsByTagName("LOGISTA_CODE");
String PMI_SOURCE_PROD=nl.item(0).getTextContent();
String LOGISTA_CODE=nl1.item(0).getTextContent();
n=nl.getLength();
m=nl.getLength();
if(n==m)
{
for(int i=0;i<=n;i++)
{
result=PMI_SOURCE_PROD+LOGISTA_CODE;
}
outs.getOutputPayload().getOutputStream().Writes(result.getBytes()); // I am getting the error in this line Kindly check and help me in this.
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Thanks ,
Vijay Kumar K.V.N
Hello Vijay
It would be useful to see the Java stack trace, but the error might be because 'result' is null. It will only be populated if the number of nodes in PMI_SOURCE_PROD is the same as the number of nodes in LOGISTA_CODE. You could check if result is null before writing to the OutputStream.
The loop for(int i=0;i<=n;i++) will fail because the index starts at 0 and goes up to n, so you will be reading values for n+1 elements in the array where the number of elements is n. It should probably be i<n.
Also, you appear to be setting result several times but with the same value every time, although this wouldn't cause an error.
regards
Steve
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Vijay.
This line looks fine.
OutputStream outs=output.getOutputPayload().getOutputStream();
Then you can write to the output stream
outs.write(result.getBytes());
I think you are using
outs.getOutputPayload().getOutputStream()
Which is trying to get an OutputPayload object from the OutputStream which will fail.
regards
Steve
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi vijay,
Try with:
outs.getOutputPayload().getOutputStream().write(result.getBytes(
"UTF-8"
));
Check also that the result variable has any value, make a trace before the instruction that throws the exception.
On the other way, i think this is only for testing the java mapping development isn´t it?, because the concatenate function could be done with the standard PI functions.
Regards.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
92 | |
11 | |
10 | |
9 | |
9 | |
7 | |
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.