h3. Introduction
package za.co.company.xi.storeconnectivity;
/**
This mapping program will convert GMTLog record types (XML) from the Extended Amount record types
to simple record types.
*
*/
import java.io.*;
import java.util.Map;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import com.sap.aii.mapping.api.MappingTrace;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.value.api.XIVMService;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
public class GMTLogExtendedFields2GMTLog extends DefaultHandler implements StreamTransformation {
private Writer writer;
private MappingTrace trace;
private boolean atTransactionType = false;
public void setParameter(Map map) {
trace = (MappingTrace) map.get(StreamTransformationConstants.MAPPING_TRACE);
}
public Writer getWriter() {
return writer;
}
public void setWriter(Writer out) {
this.writer = out;
}
public void startDocument() throws SAXException {
emit("<?xml version='1.0' encoding='UTF-8'?>");
}
public void endDocument() throws SAXException {
try {
writer.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
public void startElement(String namespaceURI,
String lName, // local name
String qName, // qualified name
Attributes attrs) throws SAXException {
// Perform transaction type translation.
if (lName.startsWith("GMTLogTransaction")) {
String[] typeSource = lName.split("GMTLogTransaction");
String typeTarget;
if (typeSource.length > 1) {
// Lookup record type conversion
typeTarget = XIVMService.executeMapping("GMTLogExtendedField", "GMTLogExtendedFieldSchema", typeSource[1], "GMTLogExtendedFieldValue", "GMTLogExtendedFieldValueSchema");
if (typeTarget==null) {
logInfo("No mapping configured for type: " + typeSource[1]);
typeTarget = typeSource[1];
}
logInfo("Using type: "+ typeTarget+ " for record type: "+typeSource[1]);
} else {
typeTarget = "";
}
// Construct new element name
String eName;
eName = "GMTLogTransaction" + typeTarget; // element name
emit("<" + eName + ">");
// Catch TransactionType element
} else if (lName.equals("TransactionType")) {
atTransactionType = true;
String eName = lName; // element name
emit("<" + eName + ">");
} else {
String eName = lName; // element name
emit("<" + eName + ">");
}
}
public void endElement(String namespaceURI,
String lName, // simple name
String qName // qualified name
) throws SAXException {
if (lName.startsWith("GMTLogTransaction")) {
// Perform transaction type translation.
String[] typeSource = lName.split("GMTLogTransaction");
String typeTarget = new String();
if (typeSource.length > 1) {
// Lookup record type conversion
typeTarget = XIVMService.executeMapping("GMTLogExtendedField", "GMTLogExtendedFieldSchema", typeSource[1], "GMTLogExtendedFieldValue", "GMTLogExtendedFieldValueSchema");
if (typeTarget==null) {
typeTarget = typeSource[1];
}
} else {
typeTarget = "";
}
String eName;
eName = "GMTLogTransaction" + typeTarget; // element name
emit("</" + eName + ">");
} else if (lName.equals("TransactionType")) {
// Release TransactionType tag
atTransactionType = false;
emit("</" + lName + ">");
} else {
emit("</" + lName + ">");
}
}
public void characters(char buf[], int offset, int len) throws SAXException {
if (atTransactionType) {
String s = new String(buf, offset, len);
String t = s;
// Lookup record type conversion
t = XIVMService.executeMapping("GMTLogExtendedField", "GMTLogExtendedFieldSchema", s, "GMTLogExtendedFieldValue", "GMTLogExtendedFieldValueSchema");
if (t==null) {
t = s;
}
// encode XML!
emit(encodeHTML(t));
} else {
String s = new String(buf, offset, len);
// encode XML!
emit(encodeHTML(s));
}
}
// Utility Methods ...
private String encodeHTML(String s) {
StringBuffer out = new StringBuffer();
for(int i=0; i<s.length(); i++) {
char c = s.charAt(i);
if(c > 127 || c=='&' || c=='"' || c=='<' || c=='>') {
out.append("&#"(int)c";");
} else {
out.append(c);
}
}
return out.toString();
}
private void logInfo(String message) {
if (trace != null)
trace.addInfo(message);
else
System.out.println(message);
}
// Wrap I/O exceptions in SAX exceptions, to
// suit handler signature requirements
private void emit(String s) throws SAXException {
try {
writer.write(s);
writer.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
public void execute(InputStream in, OutputStream out)
throws StreamTransformationException {
long start = System.currentTimeMillis();
logInfo("GMTLogExtendedFields2GMTLog mapping starting...");
OutputStreamWriter writer = new OutputStreamWriter(out);
this.setWriter(writer);
// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(in, this);
} catch (SAXException se) {
throw new StreamTransformationException(se.getMessage());
} catch (ParserConfigurationException pce) {
throw new StreamTransformationException(pce.getMessage());
} catch (IOException ioe) {
throw new StreamTransformationException(ioe.getMessage());
} finally {
long end = System.currentTimeMillis();
logInfo("GMTLogExtendedFields2GMTLog mapping completed in " + (end - start) + " milliseconds.");
}
}
}
All that is left to do in the Repository is to upload the new Java mapping as an Imported Archive and adding it to the Interface Mapping as the first step as shown in the picture below.
!https://weblogs.sdn.sap.com/weblogs/images/251795501/100305-MapExtFields-REP-IFD-modified.png|height... Interface Mapping in the Repository|width=696|src=https://weblogs.sdn.sap.com/weblogs/images/251795501/100305-MapExtFields-REP-IFD-modified.png|border...!</body>