Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Shabarish_Nair
Active Contributor
package test.jaxbpimapping;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import sourceData.Book;
import sourceData.Details;
import targetData.AdditionalInfo;
import targetData.AuthoredBy;
import targetData.BookData;
import targetData.BookTitle;
import targetData.CurrentEdition;
import targetData.DetailInfo;
import targetData.JournalInfo;
import targetData.PublishedBy;
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;
public class PI_JAXB_Source_To_Target extends AbstractTransformation {
          /*
           * Each JAVA Mapping using the 7.1 API will implement the method
           * transform(TransformationInput arg0, TransformationOutput arg1)
           * as opposed to execute(inputStream in, outputStream out) Method
           * in earlier version.
           */
          InputStream is = null;
 
          @SuppressWarnings({ "unchecked", "unchecked" })
          public void transform(TransformationInput arg0, TransformationOutput arg1)
                              throws StreamTransformationException {
                    /*
                     * An info message is added to trace. An instance of trace of object is
                     * obtained by calling the getTrace method of class AbstractTransformation
                     */
                    getTrace().addInfo("JAVA Mapping PI_JAXB_Source_To_Target is Initiated");
                    try
                    {
                    is = arg0.getInputPayload().getInputStream();
 
                    JAXBContext jaxbContext = JAXBContext.newInstance(Book.class);
                    Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
 
                    /* Uncomment the below line of code in case you want to force a schema level validation of the incoming XML*/
 
                    /*SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
                    Schema schema = schemaFactory.newSchema(new File("BookXSD.xsd"));
                    unMarshaller.setSchema(schema);*/
 
                    Book book_source = (Book) unMarshaller.unmarshal(is);
                    getTrace().addInfo("Unmarshall Successful");
 
                    /*------------- Marshall the Target----------------*/
                    targetData.ObjectFactory factoryTarget = new targetData.ObjectFactory();
 
                    BookData bookdata = factoryTarget.createBookData();
                    DetailInfo detailinfo = factoryTarget.createDetailInfo();
                    AuthoredBy authoredby = factoryTarget.createAuthoredBy();
                    BookTitle booktitle = factoryTarget.createBookTitle();
                    CurrentEdition currentedition = factoryTarget.createCurrentEdition();
                    AdditionalInfo addInfo = factoryTarget.createAdditionalInfo();
                    PublishedBy publishedby = factoryTarget.createPublishedBy();
                    JournalInfo journalinfo = factoryTarget.createJournalInfo();
 
 
                    List<Details> detailsS_List = book_source.getDetails();
                    getTrace().addInfo("Source Book has (" + detailsS_List.size() + ") Details Node");
 
                    for (int i = 0; i < detailsS_List.size(); i++)
                    {
                              Details detailsS = (Details) detailsS_List.get(i);
 
                              detailinfo = factoryTarget.createDetailInfo();
                              addInfo = factoryTarget.createAdditionalInfo();
 
                              authoredby = factoryTarget.createAuthoredBy();
                              authoredby.setContent(detailsS.getAuthor().getContent());
                              detailinfo.setAuthoredBy(authoredby);
 
                              booktitle = factoryTarget.createBookTitle();
                              booktitle.setContent(detailsS.getTitle().getContent());
                              detailinfo.setBookTitle(booktitle);
 
                              currentedition = factoryTarget.createCurrentEdition();
                              currentedition.setContent(detailsS.getEdition().getContent());
                              detailinfo.setCurrentEdition(currentedition);
 
 
 
                              publishedby = factoryTarget.createPublishedBy();
                              publishedby.setContent(detailsS.getPublisher().getContent());
                              addInfo.setPublishedBy(publishedby);
 
                              journalinfo = factoryTarget.createJournalInfo();
                              journalinfo.setContent(detailsS.getJournalTitle().getContent());
                              addInfo.setJournalInfo(journalinfo);
 
                              detailinfo.setAdditionalInfo(addInfo);
                              bookdata.getDetailInfo().add(detailinfo);
 
                    }
 
 
                    JAXBContext contextTarget = JAXBContext.newInstance(BookData.class);
                    Marshaller marshallerBookdata = contextTarget.createMarshaller();
                    marshallerBookdata.marshal(bookdata, arg1.getOutputPayload().getOutputStream());
 
 
 
                    }
          catch(Exception e)
                    {
                              getTrace().addWarning(e.toString());
 
                    }
 
                  }
          public String convertInputStreamToString(InputStream in) {
                    StringBuffer sb = new StringBuffer();
                    try {
                              InputStreamReader isr = new InputStreamReader(in);
                              Reader reader = new BufferedReader(isr);
                              int ch;
                              while ((ch = in.read()) > -1) {
                                        sb.append((char) ch);
                              }
                              reader.close();
                    } catch (Exception exception) {
                    }
                    return sb.toString();
          }

You would notice that JAXB really does make things easy for one to program. There are methods for nearly everything, making it easy for us to traverse and build XML structures. It also makes the program much more readable. Hope this blog helps you understand another dimension to XML parsing and thus adding another alternative to your traditional Java mapping scenarios.

Note:

1. If not using JDK 1.6 and above, you can download the JAR files for JAXB from here

2. We are yet to identify how JAXB based Java mapping performs in comparison to SAX and DOM parser based mapping. This has to be tested out to understand how the mapping will perform under load.

6 Comments
Labels in this area