Skip to end of metadata
Go to start of metadata

Description:

There are some blogs and wikis over java mapping and parameterized java mapping with help of new Java mapping API (PI7.1), simple message structures can be handled without DOM or SAX.If we  have source and target messaages with different and complex structures then we may need xml parsers like DOM or SAX.This blog describes java mapping with new API with help of DOM parser, and SAX parsers.  Some queries in forums on SAX parser with New API.Hope it can be useful and addressed them.

Note: You can still use the previous Java mapping API

1. Previous API: We must implement the interface “StreamTransformation” and must implement two methods, “execute” and “setParameter”

2. Whereas in new API we must extend the abstract class “AbstractTransformation” and must implement method called transform (TransformationInput in, TransformationOutput out)


 

Source Message Structure.


 

 Target Message

1. Java mapping with DOM Parser and with New API:

It describes DOM parser with new mapping API.

 /*                                                New API,JAVA Mapping DOM Parser
 * Created on Jan 20, 2011
 */
package prasad.NewAPI;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
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;

/*
 * @author Prasad Nemalikanti
 */
public class NewAPIJavaMapping extends AbstractTransformation {
   String result="";
    
  /* The transform method which must be implemented */
  public void transform(TransformationInput in,TransformationOutput out) throws StreamTransformationException
  {
    InputStream instream = in.getInputPayload().getInputStream();
   
  // String for constructing target message structure 
    String fresult="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    fresult = fresult.concat("<ns0:MT_Customer xmlns:ns0=\"urn:newapi-javamapping\">");
    try{
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
       
        Document doc = builder.parse(instream);
        traversingXML(doc);
       }
     catch(Exception e){}
    
 fresult = fresult.concat(result);
 fresult = fresult.concat("</ns0:MT_Customer>");
 
 try{
  out.getOutputPayload().getOutputStream().write(fresult.getBytes());
  /* assigning the created target message to "TransformationOutput"*/
    }
   catch(IOException e1){}
  }
 /*DOM Parser */
 public void traversingXML(Node node)
  {
   NodeList children = node.getChildNodes();
   for(int i=0;i<children.getLength();i++)
   {
     Node child = children.item(i);
     short childType = child.getNodeType();
     if(childType==Node.ELEMENT_NODE)
     {
              String nodeName=child.getNodeName();
              String targetNodeName=null;
        
              if(nodeName.equals("Users"))
               targetNodeName="Customers";
              else if(nodeName.equals("ID"))
               targetNodeName="CustomerID";
              else if(nodeName.equals("UserName"))
               targetNodeName="Name";
              else if(nodeName.equals("City"))
                targetNodeName="City";
              else if(nodeName.equals("State"))
                targetNodeName="State";
              else if(nodeName.equals("Country"))
                targetNodeName="Country";
            
             if(targetNodeName!=null)
              result=result.concat("<"+targetNodeName+">");
             
   traversingXML(child); 
   
   if(targetNodeName!=null)
   result=result.concat("</"+targetNodeName+">");
       }
     else if(childType==Node.TEXT_NODE) 
      {
       String nodeValue = child.getNodeValue();
       result = result.concat(nodeValue);
      }
   }
  }
 }

 

 

Interface Mapping(DOM)

2.Java mapping with SAX parser(new API):

 Description:

When we use SAX parser "DefaultHandler" should be extended , apart from this we need to extend "AbstractTransformation" class.It is not allowed to extend more than one class.

So here I have taken one more class to extend "DefaultHandler" ,which was taken intom main class to get xml parser.

 /*                                                  New API,JAVA Mapping SAX Parser
 * Created on Jan 21, 2011
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package prasad.NewAPI.SAX;
/*
 * @author Prasad Babu Nemalikanti
 */
 import java.io.InputStream;
 import java.io.IOException;
 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;
 import javax.xml.parsers.SAXParserFactory;
 import javax.xml.parsers.SAXParser;
 import org.xml.sax.helpers.DefaultHandler;
 import org.xml.sax.SAXException;
 import org.xml.sax.Attributes;
public class NewAPISAX extends AbstractTransformation {
 
 public void transform(TransformationInput in,TransformationOutput out) throws StreamTransformationException{
  InputStream instream = in.getInputPayload().getInputStream();
  Handling handler= new Handling();
  SAXParserFactory factory = SAXParserFactory.newInstance();
  String result=null;
   try{
      SAXParser saxpr = factory.newSAXParser(); 
      saxpr.parse(instream,handler);
      }
     catch(Exception e){e.printStackTrace();}
    
     result=handler.getResult();
     try{
     out.getOutputPayload().getOutputStream().write(result.getBytes());
      }
  catch(IOException e){}
   }
 }
 
/* Second class Which extends DefaultHandler*/

class Handling extends DefaultHandler
{
 public String fresult = null;
 public void startDocument() throws SAXException
 {
       fresult="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    fresult = fresult.concat("<ns0:MT_Customer xmlns:ns0=\"urn:newapi-javamapping\">");
 }
 public void endDocument() throws SAXException
 {
   fresult=fresult.concat("</ns0:MT_Customer>");
 }
 public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
 {
   String lName = localName;
   if(lName.equals(""))
    lName=qName;
   if(lName.equals("Users"))
     fresult=fresult.concat("<Customers>");
    
   if(lName.equals("ID"))
   fresult=fresult.concat("<CustomerID>");
  
   if(lName.equals("UserName"))
   fresult=fresult.concat("<Name>");
  
   if(lName.equals("City"))
   fresult=fresult.concat("<City>");
  
   if(lName.equals("State"))
   fresult=fresult.concat("<State>");
  
   if(lName.equals("Country"))
   fresult=fresult.concat("<Country>");     
 }
  public void endElement(String namespaceURI, String localName,String qName) throws SAXException
 {
  String lName = localName;
  if(lname.equals(""))
         lName=qName;
  if(lName.equals("Users"))
  fresult=fresult.concat("</Customers>");
  
  if(lName.equals("ID"))
  fresult=fresult.concat("</CustomerID>");
  
  if(lName.equals("UserName"))
  fresult=fresult.concat("</Name>");
  
  if(lName.equals("City"))
  fresult=fresult.concat("</City>");
  
  if(lName.equals("State"))
  fresult=fresult.concat("</State>");
  
  if(lName.equals("Country"))
  fresult=fresult.concat("</Country>");
   }
  public void characters(char[] ch, int start, int length) throws SAXException
  {
      String val= new String(ch,start, length);
   fresult=fresult.concat(val);
  }
  public String getResult()
  {
    return fresult;
  }
}


 
Interface Mapping(SAX parser)