cancel
Showing results for 
Search instead for 
Did you mean: 

Java Map--Please help me

Former Member
0 Kudos

Hello Experts,

I have code similar to the code give below:

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.util.Map;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Element;

import org.w3c.dom.Document;

import org.w3c.dom.Text;

import com.sap.aii.mapping.api.StreamTransformation;

/**

  • @author santhosh.vellingiri

*/

public class xmlpayload implements StreamTransformation {

public static void main(String args[]) throws Exception {

FileInputStream inFile =

new FileInputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlin.XML");

FileOutputStream outFile =

new FileOutputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlout.XML");

xmlpayload xml = new xmlpayload();

xml.execute(inFile, outFile);

System.out.println("Success");

}

public void setParameter(Map param) {

Map map = param;

}

public void execute(InputStream in, OutputStream out)

throws com.sap.aii.mapping.api.StreamTransformationException {

try {

//***********************Code To Generate The XML Parsing Objects****************************//

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

TransformerFactory tf = TransformerFactory.newInstance();

Transformer transform = tf.newTransformer();

//Document doc = db.parse(in);

Document docout = db.newDocument();

Element root = docout.createElement("MT_Trg");

root.setAttribute("xmlns:ns","urn:Test_File_to_File");

docout.appendChild(root);

Element stringinp = docout.createElement("stringinp");

root.appendChild(stringinp);

BufferedReader inpxml = new BufferedReader(new InputStreamReader(in));

StringBuffer buffer = new StringBuffer();

String line="";

while ((line = inpxml.readLine()) != null)

{

buffer.append(line);

}

String inptxml=buffer.toString();

Text srcxml = docout.createTextNode(inptxml);

stringinp.appendChild(srcxml);

DOMSource domS = new DOMSource(docout);

transform.transform((domS), new StreamResult(out));

} catch (Exception e) {

System.out.print("Problem parsing the file: " + e.getMessage());

e.printStackTrace();

}

}

}

Now in this code there is main function:

public static void main(String args[]) throws Exception {

FileInputStream inFile =

new FileInputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlin.XML");

FileOutputStream outFile =

new FileOutputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlout.XML");

xmlpayload xml = new xmlpayload();

xml.execute(inFile, outFile);

System.out.println("Success");

}

I want that file will come through File adapter no like the code that is picking from C:/ drive.

and output will go through fule adapter not on C:/ drive.

Please help Experts.

Hemant Gupta

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Closed

Former Member
0 Kudos

you can follow this blog to know how to use java mapping in sap XI

[https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/8138] [original link is broken] [original link is broken] [original link is broken];

just to test the mapping you have written code in the main method but only exccute method will be called and its code will be excuted during runtime.the file that is picked up by the adapter will be sent to the Integration engine to excute the mapping(java )by the adapter framework.It does not matter even if the main method is there in the code but if you want you can remove it.

Former Member
0 Kudos

Hello,

So you are saying it will not call main method when i load it in PI.

public static void main(String args[]) throws Exception {

FileInputStream inDataFile1 =

new FileInputStream("C:
Documents and Settings
kochhaas
Desktop
hemant
input_ESA.txt");

//byte[] startCharacters = null;

XMLHeaderGenerator xml = new XMLHeaderGenerator();

//inDataFile1.read(startCharacters, 0, 2);

BufferedReader inpxml = new BufferedReader(new InputStreamReader(inDataFile1));

String line = inpxml.readLine();

xml.fileContent = line;

xml.tokenOne = line.substring(2, 5);

xml.tokenTwo = line.substring(17, 23);

line = line.substring(0, 2);

if(line.equals(xml.noProgressToken)){

String nextLine;

while((nextLine = inpxml.readLine()) != null ){

xml.fileContent = xml.fileContent + "\n" + nextLine;

}

FileOutputStream outFile =

new FileOutputStream("C:
Documents and Settings
kochhaas
Desktop
hemant
output
hemant.xml");

//xml.setParameter(map);

xml.execute(inDataFile1, outFile);

System.out.println("Success");

}

else{

System.out.println("Failure");

}

My main code is this......so u r saying it will not call this method

santhosh_kumarv
Active Contributor
0 Kudos

>>So you are saying it will not call main method when i load it in PI.

Yes. Did you check the help.sap link for Java mapping that i referred earlier?

Only execute() and setParameter() methods are required for java mapping. Any other methods in the Java class should be called explictly from the execute() method. So main() method will not get executed unless u make an explict call.

If u have any mapping logic in main() move it to execute(). To test the execute() method in an IDE we for the mapping login we need to invoke it. Hence we create a main() method to pick a input file and process it in by calling execute() and write the output file. This is done only to check the correctness of the execute() method.

~Sanv...

Former Member
0 Kudos

Thanks Santosh,

Also one more thing i am calling one proprty file in execute mathod. I donot have access to File syatem of PI. Can i inport it in Imported archives. If yes what path i hvae to give in Execute().

Former Member
0 Kudos

You need to create a adapter module .and specify it in the module tab of comm. channel then module read teh file picked up by the adapter and execute the code you specify in the module

follow this thread to develop adapter module [;

Edited by: Kubra fatima on Aug 21, 2009 7:40 AM

Former Member
0 Kudos

Hello Experts,

I have code similar to the code give below:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx

  • @author santhosh.vellingiri

*/

public class xmlpayload implements StreamTransformation {

public static void main(String args[]) throws Exception {

FileInputStream inFile =

new FileInputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlin.XML");

FileOutputStream outFile =

new FileOutputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlout.XML");

xmlpayload xml = new xmlpayload();

xml.execute(inFile, outFile);

System.out.println("Success");

} public void setParameter(Map param) {

Map map = param;

}

public void execute(InputStream in, OutputStream out)

throws com.sap.aii.mapping.api.StreamTransformationException {

try {

//***********************Code To Generate The XML Parsing Objects****************************//

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

TransformerFactory tf = TransformerFactory.newInstance();

Transformer transform = tf.newTransformer();

//Document doc = db.parse(in);

Document docout = db.newDocument();

Element root = docout.createElement("MT_Trg");

root.setAttribute("xmlns:ns","urn:Test_File_to_File");

docout.appendChild(root);

Element stringinp = docout.createElement("stringinp");

root.appendChild(stringinp);

BufferedReader inpxml = new BufferedReader(new InputStreamReader(in));

StringBuffer buffer = new StringBuffer();

String line="";

while ((line = inpxml.readLine()) != null)

{

buffer.append(line);

}

String inptxml=buffer.toString();

Text srcxml = docout.createTextNode(inptxml);

stringinp.appendChild(srcxml);

DOMSource domS = new DOMSource(docout);

transform.transform((domS), new StreamResult(out));

} catch (Exception e) {

System.out.print("Problem parsing the file: " + e.getMessage());

e.printStackTrace();

}

}

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Now in this code there is main function:public static void main(String args[]) throws Exception {

FileInputStream inFile =

new FileInputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlin.XML");

FileOutputStream outFile =

new FileOutputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlout.XML");

xmlpayload xml = new xmlpayload();

xml.execute(inFile, outFile);

System.out.println("Success");

}

I want that file will come through File adapter no like the code that is picking from C:/ drive.

and output will go through fule adapter not on C:/ drive.Please help Experts.

Hemant Gupta

Former Member
0 Kudos

Hello All,

The code i want to chane is

public static void main(String args[]) throws Exception {

FileInputStream inFile =

new FileInputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlin.XML");

FileOutputStream outFile =

new FileOutputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlout.XML");

xmlpayload xml = new xmlpayload();

xml.execute(inFile, outFile);

System.out.println("Success");

}

THis code is picking the file from C:/ drive but i want that it should come through File adapter same in case to output.

Help

santhosh_kumarv
Active Contributor
0 Kudos

Hi,

Just for testing the code in NWDS i used the code to pick the file from desktop and process it.

In java mapping the input stream will be always be passed to the execute method.

public void execute(java.io.InputStream in, java.io.OutputStream out)

From Help.sap

At runtime, the Integration Engine calls this method to execute a mapping. This method contains an input stream for the source document and an output stream for the target document as parameters. These streams are usually XML documents. You can import the substructures to be converted from the input stream and output the converted target document in the output stream.

Mapping logic is written in the execute function. So the same code will work in XI i.e. the content from the file will be passed as an input stream to execute function.

Check this for better understanding http://help.sap.com/saphelp_nw04/Helpdata/EN/e2/e13fcd80fe47768df001a558ed10b6/content.htm.

Just for testing I pick the file from C drive and pass it to execute method.

~SaNv...

jyothi_anagani
Active Contributor
0 Kudos

Hi hemanth,

Refer this blogs....

/people/stefan.grube/blog/2006/10/23/testing-and-debugging-java-mapping-in-developer-studio

/people/sankararao.bhatta/blog/2008/07/27/java-mapping-secrets-revealed

/people/madanmohan.agrawal/blog/2009/07/02/enhanced-receiver-determination-using-java-mapping-dom-parser

If you want to write a java mapping no need of FileInputStream or FileOutputStream......

Your method has to implement the StreamTransformation interface and you have to write your total code in Execute Metod...

Check the above blogs and write the programme...

Thanks.

Former Member
0 Kudos

Thanks santosh,

I have written some code in Netbean and i also did the same thing for testing but i donot know how to remove the path from the code so that it can pick the file through input structure.

santhosh_kumarv
Active Contributor
0 Kudos

>>I have written some code in Netbean and i also did the same thing for testing but i donot know how to remove the path from the code so that it can pick the file through input structure.

If you testing is fine and the code is perfect, then import the same in XI and use. The path does not have any impact over the java mapping. In XI runtime Main() method is not called and by default only execute() and setparamaeter() are executed.

If u doubt still remove/comment the entire main method-> compile-> and export. Java may throw an waring the no main method exist. Jst ignore that.

~Sanv...

Former Member
0 Kudos

Ok Santhosh,

Thanks Buddy.

So i need to do some changes i the code as i am storing some value in the variable from the input file.

Thanks.