package com.sapss.testme;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
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 i24989 (Anoop Rai)
* @since 19-11-2017
* @version 1.0
*/
public class TestJavaMapping extends AbstractTransformation {
TransformationInput transInput = null;
TransformationOutput transOutput = null;
public static void main(String[] args) throws StreamTransformationException, FileNotFoundException,
TransformerConfigurationException, TransformerFactoryConfigurationError {
TestJavaMapping pm = new TestJavaMapping();
FileInputStream fin = new FileInputStream("C:\\Users\\154151.XML");
FileOutputStream fout = new FileOutputStream("C:\\Users\\OFFLINEOUT.xml");
pm.execute(fin, fout);
}
public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {
this.execute(in.getInputPayload().getInputStream(), out.getOutputPayload().getOutputStream());
}
public void execute(InputStream in, OutputStream out) throws StreamTransformationException {
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.newDocument();
Element outRoot = doc.createElement("SAP_TestPayload");
outRoot.setAttribute("xmlns", "http://testme.org/");
Element piXMLInput = doc.createElement("PIXMLInput");
BufferedReader bfr = new BufferedReader(new InputStreamReader(in, "utf-8"));
String line;
String contentLine = "";
while ((line = bfr.readLine()) != null) {
contentLine = contentLine + line.trim();
}
piXMLInput.setTextContent(contentLine);
outRoot.appendChild(piXMLInput);
doc.appendChild(outRoot);
Transformer trns = TransformerFactory.newInstance().newTransformer();
DOMSource targetMsg = new DOMSource(doc);
StreamResult stRslt = new StreamResult(out);
trns.transform(targetMsg, stRslt);
} catch (ParserConfigurationException e) {
throw new StreamTransformationException("Unable to Parse Input XML message: " + e.getMessage());
} catch (UnsupportedEncodingException e) {
throw new StreamTransformationException(
"Unsupportable input (XML UTF-8 Only) or blank : " + e.getMessage());
} catch (IOException e) {
throw new StreamTransformationException("Unable to read xml lines or it may be blank: " + e.getMessage());
} catch (TransformerConfigurationException e) {
throw new StreamTransformationException("Unable to build output XML message: " + e.getMessage());
} catch (TransformerFactoryConfigurationError e) {
throw new StreamTransformationException("Unable to build output XML message: " + e.getMessage());
} catch (TransformerException e) {
throw new StreamTransformationException("Unable to build output XML message: " + e.getMessage());
}
try {
out.flush();
} catch (IOException e) {
throw new StreamTransformationException("Unable to flush output XML message: " + e.getMessage());
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
8 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |