cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in Java Mapping

Former Member
0 Kudos

Hi,

I was trying out a simple Java mapping example.

Example of source structure is -

<?xml version="1.0"?>

<ns0:MT_SRC xmlns:ns0="http://www.sap-press.com/xi/training/00">

<organization>

<employee>

<firstname>Jack</firstname>

<lastname>Rose</lastname>

</employee>

<employee>

<firstname>Paul</firstname>

<lastname>McNealy</lastname>

</employee>

<employee>

<firstname>Vaibhav</firstname>

<lastname>Pandey</lastname>

</employee>

</organization>

</ns0:MT_SRC>

Example of target structure is -

<?xml version="1.0" encoding="UTF-8"?>

<ns0:MT_TRGT xmlns:ns0="http://www.sap-press.com/xi/training/00"><Organization><employee><name>JackRose</name></employee><employee><name>PaulMcNealy</name></employee><employee><name>VaibhavPandey</name></employee></Organization></ns0:MT_TRGT>

and here's the code I'm using

package com.mapping;

import java.io.*;

import java.util.Map;

import java.util.HashMap;

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.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

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

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

public class JavaMap implements StreamTransformation{

private Map param = null;

private MappingTrace trace = null;

public void setParameter (Map param) {

this.param = param;

if (param == null) {

this.param = new HashMap();

}

}

public void execute(InputStream in, OutputStream out) {

OutputStreamWriter buf_writer = new OutputStreamWriter(out);

InputStreamReader buf_reader = new InputStreamReader(in);

String mat = new String();

String extmat = new String();

try {

buf_writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>" ,0,56);

buf_writer.write("<ns0:MT_TRGT xmlns:ns0=\"http://www.sap-press.com/xi/training/00\">");

buf_writer.write("<Organization>",0,20);

buf_writer.flush();

} catch(IOException e){};

try {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setValidating(false);

DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder.parse(in);

String fname = null;

String lname = null;

doc.getDocumentElement().normalize();

//System.out.println("Root element " + doc.getDocumentElement().getNodeName());

NodeList nodeLst = doc.getElementsByTagName("employee");

//System.out.println("Information of all employees");

for (int s = 0; s < nodeLst.getLength(); s++) {

Node fstNode = nodeLst.item(s);

if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

buf_writer.write("<employee>");

Element fstElmnt = (Element) fstNode;

NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");

Element fstNmElmnt = (Element) fstNmElmntLst.item(0);

NodeList fstNm = fstNmElmnt.getChildNodes();

//System.out.println("First Name : " + ((Node) fstNm.item(0)).getNodeValue());

fname = fstNm.item(0).getNodeValue();

NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");

Element lstNmElmnt = (Element) lstNmElmntLst.item(0);

NodeList lstNm = lstNmElmnt.getChildNodes();

//System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());

lname = lstNm.item(0).getNodeValue();

buf_writer.write("<name>"fname" "lname"</name>");

buf_writer.write("</employee>");

}

}

buf_writer.write("</Organization>");

buf_writer.write("</ns0:MT_TRGT>");

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

transformer.setOutputProperty("indent", "yes");

DOMSource source = new DOMSource(doc);

StreamResult result = new StreamResult(out);

transformer.transform(source, result);

} catch (Exception e) {

e.printStackTrace();

}

}

}

However, when I'm testing my Interface mapping, I'm getting the following error -

Call method execute of the application Java mapping com.mapping.JavaMap

Error during appliction Java mapping com/mapping/JavaMap

java.lang.StringIndexOutOfBoundsException at java.lang.String.getChars(String.java:672)

Could anyone please help me out with this?

Thanks and Regards,

Shiladitya

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Swarup/Farooq,

If you check my code u'll find that I'm not using the String.getChars() method anywhere in the class.

This is what seems suprising to me. I have tried by giving input values to all fields, but am still getting the same error.

Regards,

Shiladitya

Former Member
0 Kudos

Hi Shiladitya,

String extmat = new String();

try {

buf_writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>" ,0,56);

buf_writer.write("<ns0:MT_TRGT xmlns:ns0=\"http://www.sap-press.com/xi/training/00\">");

buf_writer.write("<Organization>",0,20); //Why length is 20? it shuould be buf_writer.write("<Organization>");

Thanks,

Farooq

Former Member
0 Kudos

java.lang.StringIndexOutOfBoundsException at java.lang.String.getChars(String.java:672)

Meaning there could be problem with the variable that is of type String but it is totally empty.

for testing purpose try to give data in all the fields....

Edited by: Farooq Farooqui on Feb 25, 2008 5:26 PM

Former Member
0 Kudos

HI,

It looks to be that your JavaMap class have one field whihc will be not getting the proper value as input thus its creating the array Index out of bound exception.

Please can you verify if you have assigned the proper String size for all the fields.

Or probably you are getting the blank value which have created the execption.

Also can you narrow down and let me know about the line String.getChars(String.java:672)

Here you have used the try and catch option but may be the exact line no. can give better idea about the problem.

Thanks

swarup