cancel
Showing results for 
Search instead for 
Did you mean: 

PI 7.1 - Java Mapping Compilation Error

Former Member
0 Kudos

Hi Experts,

Need your help in Java Mapping code compliation in PI 7.1.

Requirement is to PARSE input xml (attached file - input_file and desried as output xml - output_file.

We have written java code based on SAX Parser [Attached file - Java_code].

Intersting is :-

Now, when we are compling this java code in Eclipse , everything is working fine and it is giving proper output.

Coming to  PI, when we are following the same code and steps as per PI . It is not parsing the file and creating only blank file.

What could be the problem ?

Please comment !

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Anshul,

                The code you have uploaded is not indented. I tried to go through each line of code but soon lost my patience to check the error. Indented code helps in better maintainance of the project. you can see more from this link http://en.wikipedia.org/wiki/Indent_style .  Now what I can see is you are trying to code for PI 7.1 where you should be using

public class Demo extends  AbstractTransformation

{

   ----------

}

and not

public class Demo extends DefaultHandler implements StreamTransformation{

}

which is not supported by PI 7.1 .

In java you cannot inherit multiple classes. As you are alo trying to extend the class  DefaultHandler this is not possible under the class Demo. Thus you should have Second class Which extends DefaultHandler within your code. When you import the code into PI server both the class files would get imported although from operation mapping you are going to call Demo.class.

The structure of the second class should be something like this

class Handling extends DefaultHandler

{

      public void startDocument() throws SAXException

      {

               --------

       }

      public void endDocument() throws SAXException

      {

            ----

      }

      public void startElement()     {

           ____________________

      }

            //other methods etc.

}

you can call methods of class Handling by instanciating an object of class Handling within   execute method of class Demo. Moreover execute method cannot be called in PI 7.1 directly thus you need to add the method

public void transform(TransformationInput arg0, TransformationOutput arg1)

  throws StreamTransformationException {

  this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());

}

inside the class Demo.  Lot of changes are required in the code to make the code work in PI 7.1 .

As you have used @SuppressWarnings in your code compiler is not showing you any warnings .

When I tried to make it work for PI 7.1 it is producing blank XML file. I could have tried to re write the code  but if a code is not well indented my thinking stops there. Please go thorugh each line of this link which clearly shows how to use SAX parser in Pi 7.1. Still if you have difficulty , you can come back to the forum. In eclipse the code works fine but once you try to deploy this in PI 7.1 it will produce blank XML. Finally one last point, the target XML you have uploaded does not have any namespace information or the target message root node information. Say for example I was expecting some output like this

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

<ns0:MT_Customer xmlns:ns0="urn:newapi-javamapping">

-      <ORDER>
           <current_date>2012-08-29 15:50:23</current_date>
           <action>issue</action>
           <order_number>P0101158</order_number>
           <order_type>PURCHASE</order_type>
           <location_code>ADD/FAK/ET-ANL/DOCK</location_code>
           <terms_conditions>A10</terms_conditions>
-      <ITEMS>
           <line_number>1</line_number>
           <line_description>Boeing 787</line_description>
           <part_number>787</part_number>
           <order_quantity>1 EA</order_quantity>
           <unit_standard_price>10000</unit_standard_price>
           <promised_by_dt>2012-09-18 15:50:21</promised_by_dt>
           <type>FIXASSET</type>
           <t_code>CMP1-1110003</t_code>

    </ITEMS>

</ORDER>

</ns0:MT_Customer>

But your code is producing

<ORDER> 

           <current_date>2012-08-29 15:50:23</current_date>

           <action>issue</action>

           <order_number>P0101158</order_number>

           <order_type>PURCHASE</order_type>

           <location_code>ADD/FAK/ET-ANL/DOCK</location_code>

           <terms_conditions>A10</terms_conditions>

-      <ITEMS>

           <line_number>1</line_number>

           <line_description>Boeing 787</line_description>

           <part_number>787</part_number>

           <order_quantity>1 EA</order_quantity>

           <unit_standard_price>10000</unit_standard_price>

           <promised_by_dt>2012-09-18 15:50:21</promised_by_dt>

           <type>FIXASSET</type>

           <t_code>CMP1-1110003</t_code>

    </ITEMS>

</ORDER>

which is another reason where you can expect a blank XML in PI server.

Regards

Anupam  

Former Member
0 Kudos

Hi Anupam,

First of all really Thanks for your valuable time & efforts.

We have followed your inputs. Worked on your suggested logic.

Attached are updated 2 java files {Demo.java  &  Handling.java }.

Again during exceution in Eclipse, it is working fine and giving poper output.

Where during in PI it is giivng below outpu each time -

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

<ns0:MT_Recv xmlns:ns0='http://java.com'>

<ORDER>

</ORDER>

</ns0:MT_Recv>

____________________________________________________________________________________

Demo.java

import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.IOException;
import java.io.InputStream;

/*IMPORT statement imports the specified classes and its methods into the program */
/*Every Java mapping program must implement the interface StreamTransformation and its methods execute() and setParameter() and extend the class DefaultHandler.*/

public class Demo extends AbstractTransformation  {
public Map map = null;
    public AbstractTrace trace = null;

String test;
String number1 , number2 , number3, number4, number5, number6, number8, number9, number10, number11, number12, number13, number14, number15;
String lineEnd = System.getProperty("line.separator");

public void setParameter (Map in)
{
map = in;
if (map == null)
{
  this.map = new HashMap();
}
}

 


public void execute (InputStream in, OutputStream out)
throws com.sap.aii.mapping.api.StreamTransformationException
{
Handling handler = new Handling();
SAXParserFactory factory = SAXParserFactory.newInstance();
try
{
  SAXParser saxParser = factory.newSAXParser();
 
  saxParser.parse(in, handler);
  test = handler.getResult();
  out.write(test.getBytes());
}
catch (Throwable t)
{
  t.printStackTrace();
}

}
 

public void transform(TransformationInput arg0, TransformationOutput arg1)  throws StreamTransformationException
   


   this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream()); 
   
}


//public static void main(String[] args) throws Exception
//{
//   // TODO Auto-generated method stub
//  FileInputStream in=new FileInputStream("C:\\Users\\Ashish\\Desktop\\java.xml");  
//   FileOutputStream out=new FileOutputStream("C:\\Users\\Ashish\\Desktop\\Output.xml");
//   Demo genFormat=new Demo();
//   genFormat.execute(in,out);  
// }

}

________________________________________________________________________________________

Hanling.java

import java.io.IOException;
import java.io.OutputStream;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.IOException;
import java.io.InputStream;

public class Handling extends DefaultHandler
{
public boolean input1 = false;
public boolean input2 = false;
public boolean input3 = false;
public boolean input4 = false;
public boolean input5 = false;
public boolean input6 = false;
public boolean input8 = false;
public boolean input9 = false;
public boolean input10 = false;
public boolean input11 = false;
public boolean input12 = false;
public boolean input13 = false;
public boolean input14 = false;
public boolean input15 = false;
String number1 , number2 , number3, number4, number5, number6, number8, number9, number10, number11, number12, number13, number14, number15;
public String fresult = null;
public boolean input = false;



  public void startDocument() throws SAXException
  {
   fresult=("<?xml version='1.0' encoding='UTF-8'?>");
   fresult = fresult.concat("<ns0:MT_Recv xmlns:ns0='http://java.com'>");
   fresult = fresult.concat("<ORDER>");
  }
  public void endDocument() throws SAXException
  {
   fresult = fresult.concat("</ORDER>");
   fresult = fresult.concat("</ns0:MT_Recv>");
  }
  public void startElement (String namespaceURI, String sName,
  String qName, Attributes attrs) throws SAXException
  {
   String eName = sName;
   if ("".equals(eName))
    eName = qName;
   if(eName.equals("ns2:current_date"))
   {
    input1 = true;
   }
   if(eName.equals("ns2:action"))
   {
    input2 = true;
   }
   if(eName.equals("ns2:order_number"))
   {
    input3 = true;
   }
   if(eName.equals("ns2:order_type"))
   {
    input4 = true;
   }
   if(eName.equals("ns2:location_code"))
   {
    input5 = true;
   }
   if(eName.equals("ns2:terms_conditions"))
   {
    input6 = true;
   }  
   if(eName.equals("ns2:line_number"))
   {
    input8 = true;
   }
   if(eName.equals("ns2:line_description"))
   {
    input9 = true;
   }
   if(eName.equals("ns2:part_number"))
   {
    input10 = true;
   }
   if(eName.equals("ns2:order_quantity"))
   {
    input11 = true;
   }
   if(eName.equals("ns2:unit_standard_price"))
   {
    input12 = true;
   }
   if(eName.equals("ns2:promised_by_dt"))
   {
    input13 = true;
   }
   if(eName.equals("ns:type"))
   {
    input14 = true;
   }
   if(eName.equals("ns:t_code"))
   {
    input15 = true;
   }
  }
 
  public void endElement (String namespaceURI, String sName, String qName) throws SAXException
  {
   String eName = sName;
   if ("".equals(eName))
    eName = qName;
   if(eName.equals("ns2:current_date"))
   {
    input1 = false;
   }
   if(eName.equals("ns2:action"))
   {
    input2 = false;
   }
   if(eName.equals("ns2:order_number"))
   {
    input3 = false;
   }
   if(eName.equals("ns2:order_type"))
   {
    input4 = false;
   }
   if(eName.equals("ns2:location_code"))
   {
    input5 = false;
   }
   if(eName.equals("ns2:terms_conditions"))
   {
    input6 = false;
   }
  
   if(eName.equals("ns2:line_number"))
   {
    input8 = false;
   }
   if(eName.equals("ns2:line_description"))
   {
    input9 = false;
   }
   if(eName.equals("ns2:part_number"))
   {
    input10 = false;
   }
   if(eName.equals("ns2:order_quantity"))
   {
    input11 = false;
   }
   if(eName.equals("ns2:unit_standard_price"))
   {
    input12 = false;
   }
   if(eName.equals("ns2:promised_by_dt"))
   {
    input13 = false;
   }
   if(eName.equals("ns:type"))
   {
    input14 = false;
   }
   if(eName.equals("ns:t_code"))
   {
    input15 = false;
   }

  }
   
 
  public void characters(char ch[] , int start, int length) throws SAXException
  {   
   String dataString = new String(ch, start, length).trim();
   
     if (input1)
      {
       number1 = dataString;
     }
      if (input2)
      {
       number2 = dataString;
      }

      if (input3)
      {  
       number3 = dataString;
      }

      if (input4)
      {
       number4 = dataString;
      }

      if (input5)
      {
       number5 = dataString;
      }

      if (input6)
      {
        number6 = dataString;
      }

      if (input8)
      {
        number8 = dataString;
      }


      if (input9)
      {
       number9 = "";
       number9 = dataString;
      }


      if (input10)
      {
       number10="";
       number10 = dataString;
      }


      if (input11)
      {
       number11 = "";
       number11 = dataString;
      }

      if (input12)
      {
       number12 = "";
       number12 = dataString;
      }

      if (input13)
      {
       number13 = "";
       number13 = dataString;
      }

      if (input14)
      {
       number14 = "";
       number14 = dataString;
      }

      if (input15)
      {
       number15 = "";
       number15 = dataString;
      }

      if (input1 == true)
      {
       fresult = fresult.concat("<current_date>");
       fresult = fresult.concat(number1);
       fresult = fresult.concat("</current_date>");
      }

      if (input2 == true)
      {
       fresult = fresult.concat("<action>" +number2+"</action>");
      }

      if (input3 == true)
      {
       fresult = fresult.concat("<order_number>" +number3+"</order_number>");
      }

      if (input4 == true)
      {
       fresult = fresult.concat("<order_type>" +number4+"</order_type>");
      }

      if (input5 == true)
      {
       fresult = fresult.concat("<location_code>"+number5+"</location_code>");
      }

      if (input6 == true)
      {
       fresult = fresult.concat("<terms_conditions>"+number6+"</terms_conditions>");
      }

      if (input8 == true)
      {
       fresult = fresult.concat("<ITEMS>");
       fresult = fresult.concat("<line_number>"+number8+"</line_number>");
      }

      if (input9 == true)
      {
       fresult = fresult.concat("<line_description>"+number9+"</line_description>");
      }

      if (input10 == true)
      {
       fresult = fresult.concat("<part_number>" +number10+"</part_number>");
      }

      if (input11 == true)
      {
       fresult = fresult.concat("<order_quantity>"+number11+"</order_quantity>");
      }
      if (input12 == true)
      {
       fresult = fresult.concat("<unit_standard_price>"+number12+"</unit_standard_price>");
      }
      if (input13 == true)
      {
       fresult = fresult.concat("<promised_by_dt>"+number13+"</promised_by_dt>");
      }
      if (input14 == true)
      {
       fresult = fresult.concat("<type>"+number14 +"</type>");
      }
      if (input15 == true)
      {
       fresult = fresult.concat("<t_code>"+number15+"</t_code>");
       fresult = fresult.concat("</ITEMS>");
      }
    }
   
 

  public String getResult()
  {
   return fresult;
  }
}


anupam_ghosh2
Active Contributor
0 Kudos

Hi Anshul,

                Thank you for including indentation in your code. Instead of shifting the the lines of code towards right using space bar , try using the TAB. This will make your code more readable. You have not followed exact methodology explained by the author in the link I mentioned earlier. your code will erase any empty tag coming in source XML to appear in target XML but the code mentioned in the link is free from that side effect. Too much use of global variable sometimes make the errors getting detected very difficult. Your code has too many boolean variables being used globally. I would suggest minimize the use of global variables and exactly follow the method used in the link.

I usually prefer the DOM over SAX parser. Please check number of occurrences of each target field in target XSD. The Sax code should satisfy the occurrence values as well. When I test in eclipse your code with the same input xml, its producing correct output but this does not guarantee its correct working in PI server.

Regards

Anupam

Former Member
0 Kudos

Hi Anupam,

Thanks for valuable inputs. Dont know why it is not exceuting in PI where as it is successful in Eclipse.

One main reason is client is giving me unstructured xml file which I am reading the whole xml file as a "string" data type. like below :-

Input Strucrure :-

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

- <xsd:schema targetNamespace="http://java.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://java.com">

  <xsd:element name="MT_Send" type="DT_Sender" />

- <xsd:complexType name="DT_Sender">

- <xsd:sequence>

  <xsd:element name="name" type="xsd:string" />

  </xsd:sequence>

  </xsd:complexType>

  </xsd:schema>

Output Structure:-

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

- <xsd:schema targetNamespace="http://java.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://java.com">

  <xsd:element name="MT_Recv" type="ORDER" />

- <xsd:complexType name="ORDER">

- <xsd:sequence>

  <xsd:element name="current_date" type="xsd:string" />

  <xsd:element name="action" type="xsd:string" />

  <xsd:element name="order_number" type="xsd:string" />

  <xsd:element name="order_type" type="xsd:string" />

  <xsd:element name="location_code" type="xsd:string" />

  <xsd:element name="terms_conditions" type="xsd:string" />

- <xsd:element name="ITEMS" minOccurs="0" maxOccurs="unbounded">

- <xsd:complexType>

- <xsd:sequence>

  <xsd:element name="line_number" type="xsd:string" />

  <xsd:element name="line_description" type="xsd:string" />

  <xsd:element name="part_number" type="xsd:string" />

  <xsd:element name="order_quantity" type="xsd:string" />

  <xsd:element name="unit_standard_price" type="xsd:string" />

  <xsd:element name="promised_by_dt" type="xsd:string" />

  <xsd:element name="type" type="xsd:string" />

  <xsd:element name="t_code" type="xsd:string" />

  </xsd:sequence>

  </xsd:complexType>

  </xsd:element>

  </xsd:sequence>

  </xsd:complexType>

  </xsd:schema>

Last attempt !

anupam_ghosh2
Active Contributor
0 Kudos

Hi Anshul,

                Could you upload the entire XML you are getting in PI as inbound payload? When entire XML message comes as a string you need different methodology to handle it. You need to convert the string into XML and apply SAX parser again. The present input you are giving to the java mapping is in form of XML thus its producing proper output in eclipse. In PI server this XML is comming as a field value to the field <sequence>. SAX is never reading  the contents of the field in <sequence> in your code thus its producing no tags in output XML. This is a sample code to convert string to XML , this you need to apply in your code http://www.coderanch.com/t/512978/java/java/convert-string-xml-file-java . In these cases DOM parser proves to be more helpful than SAX as you can extract value of  a specific field within source XML without parsing the entire XML sequencially as SAX does it. Anyways this is also possible to achieve results with SAX.

Regards

Anupam  

Former Member
0 Kudos

Hi Anupam,

Thanks for your response .Below is the entire XML. I have extracted from SXMB_MONI.

Sceanrio is - SOAP to FILE

"response "is the Inbound Data Type

___________________________________________________________________________________________________

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

- <!--  Receiver Grouping

  -->

- <MXI_PO_MT xmlns="http://mxi.com" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <response xmlns=""><?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:event id="309005" xmlns="http://xml.mxi.com/xsd/integration/event/1.1" xmlns:ns1="http://xml.mxi.com/xsd/integration/event/1.1"><resp:response msg_id="309005" msg_status="COMPLETE" x...http://xml.mxi.com/xsd/integration/response/1.1"><ns2:send_order_information xmlns="http://xml.mxi.com/xsd/core/finance/send_order_information/1.1" xmlns:ns="http://xml.mxi.com/xsd/core/mxdomaintypes/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://xml.mxi.com/xsd/core/finance/send_order_information/1.1"> <ns2:current_date>2012-09-04 14:00:32</ns2:current_date> <ns2:action>create</ns2:action> <ns2:order> <ns2:order_number>P0103840</ns2:order_number> <ns2:order_type>REPAIR</ns2:order_type> <ns2:cancelled>false</ns2:cancelled> <ns2:issued>false</ns2:issued> <ns2:authorized>false</ns2:authorized> <ns2:contact> <ns:username xmlns:ns="18835http://xml.mxi.com/xsd/core/mxdomaintypes/1.1">18835</ns:username> <ns:hr_code xmlns:ns="18835http://xml.mxi.com/xsd/core/mxdomaintypes/1.1">18835</ns:hr_code> <ns:name xmlns:ns="Yohannis" _mce_href="http://xml.mxi.com/xsd/core/mxdomaintypes/1.1">Yohannis">http://xml.mxi.com/xsd..., Addisu</ns:name> </ns2:contact> <ns2:vendor> <ns:code xmlns:ns="81205http://xml.mxi.com/xsd/core/mxdomaintypes/1.1">81205</ns:code> <ns:name xmlns:ns="BOEING" _mce_href="http://xml.mxi.com/xsd/core/mxdomaintypes/1.1">BOEING">http://xml.mxi.com/xsd/cor... COMMERCIAL AIRPLANES CO.</ns:name> </ns2:vendor> <ns2:priority>NORMAL</ns2:priority> <ns2:bill_to xsi:nil="true"/> <ns2:consign_to xsi:nil="true"/> <ns2:ship_to_location> <ns2:location_code>ADD/DOCK</ns2:location_code> <ns2:address1>Ethiopian Airlines</ns2:address1> <ns2:address2>Bole International Airport, P.O.Box 1755</ns2:address2> <ns2:city>Addis Ababa, Ethiopia</ns2:city> <ns2:state xsi:nil="true"/> <ns2:country>ETH</ns2:country> <ns2:zip_code xsi:nil="true"/> <ns2:phone xsi:nil="true"/> <ns2:fax xsi:nil="true"/> <ns2:email xsi:nil="true"/> </ns2:ship_to_location> <ns2:transportation_type xsi:nil="true"/> <ns2:currency>USD</ns2:currency> <ns2:exchange_rate>1.25</ns2:exchange_rate> <ns2:terms_conditions xsi:nil="true"/> <ns2:fob xsi:nil="true"/> <ns2:order_external_reference xsi:nil="true"/> <ns2:customer_code xsi:nil="true"/> <ns2:ship_to_code xsi:nil="true"/> <ns2:note_to_vendor xsi:nil="true"/> <ns2:borrow_rate xsi:nil="true"/> <ns2:order_line> <ns2:line_number>1</ns2:line_number> <ns2:line_description>Repair TABLE ASSY (PN: 854711-439A, SN: SN001234)</ns2:line_description> <ns2:part_number>854711-439A</ns2:part_number> <ns2:order_quantity>1 EA</ns2:order_quantity> <ns2:order_standard_quantity>1 EA</ns2:order_standard_quantity> <ns2:received_quantity>0 EA</ns2:received_quantity> <ns2:unit_standard_price>465</ns2:unit_standard_price> <ns2:unit_price>465</ns2:unit_price> <ns2:promised_by_dt>2012-09-05 13:00:00</ns2:promised_by_dt> <ns2:promised_by_dt_confirmed>false</ns2:promised_by_dt_confirmed> <ns2:account> <ns:code xmlns:ns="WIDE-BODY-5000801http://xml.mxi.com/xsd/core/mxdomaintypes/1.1">WIDE-BODY-5000801</ns:code> <ns:type xmlns:ns="EXPENSEhttp://xml.mxi.com/xsd/core/mxdomaintypes/1.1">EXPENSE</ns:type> <ns:external_reference xmlns:ns="462http://xml.mxi.com/xsd/core/mxdomaintypes/1.1">462</ns:external_reference> <ns:t_code xmlns:ns="5000801http://xml.mxi.com/xsd/core/mxdomaintypes/1.1">5000801</ns:t_code> </ns2:account> <ns2:line_external_reference xsi:nil="true"/> <ns2:vendor_note xsi:nil="true"/> <ns2:owner>ETH</ns2:owner> <ns2:price_type xsi:nil="true"/> <ns2:po_line_type>REPAIR</ns2:po_line_type> <ns2:receiver_note xsi:nil="true"/> </ns2:order_line> </ns2:order> </ns2:send_order_information></resp:response></ns1:event></soapenv:Body></soapenv:Envelope></response>

  </MXI_PO_MT>

anupam_ghosh2
Active Contributor
0 Kudos

Hi Anshul,

                   With this payload the mapping should throw exception. Are you getting any exception during runtime?

Is this payload directly flowing in PI server or you are getting this as response to some query? The payload contains this statement

"<?xml version="1.0" encoding="UTF-8" ?>" twice, thus making the XML invalid one.Input Xml to the code is the xml paylosd you  have posted in your last response to this thread.I have modified your code a little , so in case at run time you were not getting any exception due to invalid XML then this code should produce required output

import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.sap.aii.mapping.api.AbstractTrace;
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 java.io.*;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.InputStream;

/*IMPORT statement imports the specified classes and its methods into the program */
/*Every Java mapping program must implement the interface StreamTransformation and its methods execute() and setParameter() and extend the class DefaultHandler.*/

public class Demo extends AbstractTransformation  {
public Map map = null;
    public AbstractTrace trace = null;

String test;
String number1 , number2 , number3, number4, number5, number6, number8, number9, number10, number11, number12, number13, number14, number15;
String lineEnd = System.getProperty("line.separator");

public void setParameter (Map in)
{
map = in;
if (map == null)
{
  this.map = new HashMap();
}
}

public String extractXMLPayload(InputStream in)
{
  String s="";
  try
  {
   BufferedReader br= new BufferedReader(new InputStreamReader(in));
   int startIndex=0,endIndex=0;
   String line="",str="";
   while((line=br.readLine())!=null)
   {
    str+=line;
   }
  
   startIndex=str.indexOf("<soapenv:Envelope");
   endIndex=str.indexOf("soapenv:Envelope>");
  
   if(endIndex>startIndex)
   {
     s="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";  
     s=s+str.substring(startIndex,endIndex+ ("soapenv:Envelope>".length()));
   } 
     
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  return s;
}


public void execute (InputStream in, OutputStream out)
throws com.sap.aii.mapping.api.StreamTransformationException
{

Handling handler = new Handling();
SAXParserFactory factory = SAXParserFactory.newInstance();
String s=extractXMLPayload(in);

try
{
  InputStream is=null;
  is= new ByteArrayInputStream(s.getBytes("UTF-8"));
  if(is!=null)
  {
   in=is;
  }
}
catch(Exception e)
{
  e.printStackTrace();
}

try
{
   SAXParser saxParser = factory.newSAXParser();
   saxParser.parse(in, handler);
   test = handler.getResult();
   out.write(test.getBytes());
}
catch (Throwable t)
{
  t.printStackTrace();
}

}
 

public void transform(TransformationInput arg0, TransformationOutput arg1)  throws StreamTransformationException
   

   this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream()); 
   
}


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

  FileInputStream in=new FileInputStream("D:\\scn\\input.xml");  
  FileOutputStream out=new FileOutputStream("D:\\scn\\Output_file3.xml");
  Demo genFormat=new Demo();
  genFormat.execute(in,out);  
}

}


class Handling extends DefaultHandler
{
public boolean input1 = false;
public boolean input2 = false;
public boolean input3 = false;
public boolean input4 = false;
public boolean input5 = false;
public boolean input6 = false;
public boolean input8 = false;
public boolean input9 = false;
public boolean input10 = false;
public boolean input11 = false;
public boolean input12 = false;
public boolean input13 = false;
public boolean input14 = false;
public boolean input15 = false;
String number1 , number2 , number3, number4, number5, number6, number8, number9, number10, number11, number12, number13, number14, number15;
public String fresult = null;
public boolean input = false;

  public void startDocument() throws SAXException
  {
   fresult=("<?xml version='1.0' encoding='UTF-8'?>");
   fresult = fresult.concat("<ns0:MT_Recv xmlns:ns0='http://java.com'>");
   fresult = fresult.concat("<ORDER>");
  }
  public void endDocument() throws SAXException
  {
   fresult = fresult.concat("</ORDER>");
   fresult = fresult.concat("</ns0:MT_Recv>");
  }
  public void startElement (String namespaceURI, String sName,
  String qName, Attributes attrs) throws SAXException
  {
  
 
   String eName = sName;
   if ("".equals(eName))
   {
    eName = qName;
   }
   if(eName.equals("ns2:current_date"))
   {
    input1 = true;
   }
   if(eName.equals("ns2:action"))
   {
    input2 = true;
   }
   if(eName.equals("ns2:order_number"))
   {
    input3 = true;
   }
   if(eName.equals("ns2:order_type"))
   {
    input4 = true;
   }
   if(eName.equals("ns2:location_code"))
   {
    input5 = true;
   }
   if(eName.equals("ns2:terms_conditions"))
   {
    input6 = true;
   }  
   if(eName.equals("ns2:line_number"))
   {
    input8 = true;
   }
   if(eName.equals("ns2:line_description"))
   {
    input9 = true;
   }
   if(eName.equals("ns2:part_number"))
   {
    input10 = true;
   }
   if(eName.equals("ns2:order_quantity"))
   {
    input11 = true;
   }
   if(eName.equals("ns2:unit_standard_price"))
   {
    input12 = true;
   }
   if(eName.equals("ns2:promised_by_dt"))
   {
    input13 = true;
   }
   if(eName.equals("ns:type"))
   {
    input14 = true;
   }
   if(eName.equals("ns:t_code"))
   {
    input15 = true;
   }
  }
 
  public void endElement (String namespaceURI, String sName, String qName) throws SAXException
  {
   String eName = sName;
   if ("".equals(eName))
   {
    eName = qName;
   }   
   if(eName.equals("ns2:current_date"))
   {
    input1 = false;
   }
   if(eName.equals("ns2:action"))
   {
    input2 = false;
   }
   if(eName.equals("ns2:order_number"))
   {
    input3 = false;
   }
   if(eName.equals("ns2:order_type"))
   {
    input4 = false;
   }
   if(eName.equals("ns2:location_code"))
   {
    input5 = false;
   }
   if(eName.equals("ns2:terms_conditions"))
   {
    input6 = false;
   }
  
   if(eName.equals("ns2:line_number"))
   {
    input8 = false;
   }
   if(eName.equals("ns2:line_description"))
   {
    input9 = false;
   }
   if(eName.equals("ns2:part_number"))
   {
    input10 = false;
   }
   if(eName.equals("ns2:order_quantity"))
   {
    input11 = false;
   }
   if(eName.equals("ns2:unit_standard_price"))
   {
    input12 = false;
   }
   if(eName.equals("ns2:promised_by_dt"))
   {
    input13 = false;
   }
   if(eName.equals("ns:type"))
   {
    input14 = false;
   }
   if(eName.equals("ns:t_code"))
   {
    input15 = false;
   }

  }
   
 
  public void characters(char ch[] , int start, int length) throws SAXException
  {   
   String dataString = new String(ch, start, length).trim();
   
     if (input1)
      {
      number1 = dataString;
     
     }
      if (input2)
      {
       number2 = dataString;
      }

      if (input3)
      {  
       number3 = dataString;
      }

      if (input4)
      {
       number4 = dataString;
      }

      if (input5)
      {
       number5 = dataString;
      }

      if (input6)
      {
       number6 = dataString;
      }

      if (input8)
      {
       number8 = dataString;
      }


      if (input9)
      {
       number9 = "";
       number9 = dataString;
      }


      if (input10)
      {
       number10="";
       number10 = dataString;
      }


      if (input11)
      {
       number11 = "";
       number11 = dataString;
      }

      if (input12)
      {
       number12 = "";
       number12 = dataString;
      }

      if (input13)
      {
       number13 = "";
       number13 = dataString;
      }

      if (input14)
      {
       number14 = "";
       number14 = dataString;
      }

      if (input15)
      {
       number15 = "";
       number15 = dataString;
      }

      if (input1 == true)
      {
       fresult = fresult.concat("<current_date>");
       fresult = fresult.concat(number1);
       fresult = fresult.concat("</current_date>");
      }

      if (input2 == true)
      {
       fresult = fresult.concat("<action>" +number2+"</action>");
      }

      if (input3 == true)
      {
       fresult = fresult.concat("<order_number>" +number3+"</order_number>");
      }

      if (input4 == true)
      {
       fresult = fresult.concat("<order_type>" +number4+"</order_type>");
      }

      if (input5 == true)
      {
       fresult = fresult.concat("<location_code>"+number5+"</location_code>");
      }

      if (input6 == true)
      {
       fresult = fresult.concat("<terms_conditions>"+number6+"</terms_conditions>");
      }

      if (input8 == true)
      {
       fresult = fresult.concat("<ITEMS>");
       fresult = fresult.concat("<line_number>"+number8+"</line_number>");
      }

      if (input9 == true)
      {
       fresult = fresult.concat("<line_description>"+number9+"</line_description>");
      }

      if (input10 == true)
      {
       fresult = fresult.concat("<part_number>" +number10+"</part_number>");
      }

      if (input11 == true)
      {
       fresult = fresult.concat("<order_quantity>"+number11+"</order_quantity>");
      }
      if (input12 == true)
      {
       fresult = fresult.concat("<unit_standard_price>"+number12+"</unit_standard_price>");
      }
      if (input13 == true)
      {
       fresult = fresult.concat("<promised_by_dt>"+number13+"</promised_by_dt>");
      }
      if (input14 == true)
      {
       fresult = fresult.concat("<type>"+number14 +"</type>");
      }
      if (input15 == true)
      {
       fresult = fresult.concat("<t_code>"+number15+"</t_code>");
       fresult = fresult.concat("</ITEMS>");
      }
    }
   
 

  public String getResult()
  {
   //System.out.println(fresult);
   return fresult;
  }
}

the input xml (invalid) one is what you posted in your last response to this thread.

The output is shown below

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

- <ns0:MT_Recv xmlns:ns0="http://java.com">

- <ORDER>

<current_date>2012-09-04 14:00:32</current_date>

<action>create</action>

<order_number>P0103840</order_number>

<order_type>REPAIR</order_type>

<location_code>ADD/DOCK</location_code>

- <ITEMS>

<line_number>1</line_number>

<line_description>Repair TABLE ASSY (PN: 854711-439A, SN: SN001234)</line_description>

<part_number>854711-439A</part_number>

<order_quantity>1 EA</order_quantity>

<unit_standard_price>465</unit_standard_price>

<promised_by_dt>2012-09-05 13:00:00</promised_by_dt>

<type>EXPENSE</type>

<t_code>5000801</t_code>

</ITEMS>

</ORDER>

</ns0:MT_Recv>

Try this code. Hope this resolves the problem.

Regards

Anupam

Former Member
0 Kudos

Hi Anupam,

Hats off .  . Now it is generating a file.

finally solution implemented .Thanks a ton

Answers (2)

Answers (2)

former_member181985
Active Contributor
0 Kudos

Hi,

I checked your code and it works in PI as well without any modification.

Your thread subject line indicates compilation error, but I don't see any error description in your question. You only mentioned blank file which not an error.

Former Member
0 Kudos

Hi Praveen,

Thanks for your time.

While execution the code in PI [as per java code] we are expecting output file but it is generating only blank xml file.

While the same code is working fine in Eclipse.

What could be the problem ?

former_member181985
Active Contributor
0 Kudos

The only problem I see is that,

  1. you have hardcoded ns2 and ns prefix  in your java code which can be different at runtime (from input XML content) in PI.
  2. Try to test from interface mapping directly (as I mentioned in my blogs) and change your java code accordingly

Check this: http://stackoverflow.com/questions/3484046/how-can-i-parse-a-namespace-using-the-sax-parser

- Praveen

former_member181985
Active Contributor
0 Kudos