// Java Mapping Code package test.jaxbpimapping;import generated.Book; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import org.xml.sax.SAXException; 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 XSDValidationUsingJAXB extends AbstractTransformation { private Boolean success = true; public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException { getTrace().addInfo("JAVA Mapping XSDValidationUsingJAXB is Initiated"); JAXBContext jaxbContext = null; Unmarshaller unMarshaller = null; Schema schema = null; String outputPayload = null; InputStream in = arg0.getInputPayload().getInputStream(); try { jaxbContext = JAXBContext.newInstance(Book.class); unMarshaller = jaxbContext.createUnmarshaller(); SchemaFactory schemaFactory = SchemaFactory .newInstance("http://www.w3.org/2001/XMLSchema"); schema = schemaFactory.newSchema(new File( "/MyFolder/BookXSDXNode.xsd")); //This can be any folder on the PI file server which <SID>adm has access to unMarshaller.setSchema(schema); Book b = (Book) unMarshaller.unmarshal(in); int i = b.getDetails().size(); getTrace().addInfo("Number of occurrence of Details node is :" + i); try { outputPayload = "<?xml version=\"1.0\"?><validation><Status>success</Status><Description></Description></validation>"; arg1.getOutputPayload().getOutputStream().write( outputPayload.getBytes("UTF-8")); } catch (IOException e) { getTrace().addWarning("IOException: " + e.toString()); } } catch (JAXBException e) { success = false; getTrace().addInfo("Success (JAXBException) " + success.toString()); getTrace().addWarning("JAXBException: " + e.toString()); try { outputPayload = "<?xml version=\"1.0\"?><validation><Status>failed</Status><Description>" + e.toString() + "</Description></validation>"; arg1.getOutputPayload().getOutputStream().write( outputPayload.getBytes("UTF-8")); } catch (IOException IOe) { getTrace().addWarning("IOException: " + IOe.toString()); } } catch (SAXException e) { success = false; getTrace().addInfo("Success (SAXException) " + success.toString()); getTrace().addWarning("SAXException: " + e.toString()); try { outputPayload = "<?xml version=\"1.0\"?><validation><Status>failed</Status><Description>" + e.toString() + "</Description></validation>"; arg1.getOutputPayload().getOutputStream().write( outputPayload.getBytes("UTF-8")); } catch (IOException IOe) { getTrace().addWarning("IOException: " + IOe.toString()); } } }
Below is the XSD:
<?xml version="1.0" encoding="UTF-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="AdditionalInfo"> <xs:complexType> <xs:sequence> <xs:element ref="journalTitle" /> <xs:element ref="publisher" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="author"> <xs:complexType mixed="true" /> </xs:element> <xs:element name="Book"> <xs:complexType> <xs:sequence> <xs:element ref="Details" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Details"> <xs:complexType> <xs:sequence> <xs:element ref="title" /> <xs:element ref="author" /> <xs:element ref="edition" /> <xs:element ref="AdditionalInfo" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="edition" type="xs:integer"> </xs:element> <xs:element name="journalTitle"> <xs:complexType mixed="true" /> </xs:element> <xs:element name="publisher"> <xs:complexType mixed="true" /> </xs:element> <xs:element name="title"> <xs:complexType mixed="true" /> </xs:element> </xs:schema>