|
| 1 | +package com.baeldung.smooks.converter; |
| 2 | + |
| 3 | +import com.baeldung.smooks.model.Order; |
| 4 | +import org.milyn.Smooks; |
| 5 | +import org.milyn.payload.JavaResult; |
| 6 | +import org.milyn.payload.StringResult; |
| 7 | +import org.xml.sax.SAXException; |
| 8 | + |
| 9 | +import javax.xml.transform.stream.StreamSource; |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +public class OrderConverter { |
| 13 | + |
| 14 | + public Order convertOrderXMLToOrderObject(String path) throws IOException, SAXException { |
| 15 | + Smooks smooks = new Smooks(OrderConverter.class.getResourceAsStream("/smooks/smooks-mapping.xml")); |
| 16 | + try { |
| 17 | + JavaResult javaResult = new JavaResult(); |
| 18 | + smooks.filterSource(new StreamSource(OrderConverter.class.getResourceAsStream(path)), javaResult); |
| 19 | + return (Order) javaResult.getBean("order"); |
| 20 | + } finally { |
| 21 | + smooks.close(); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + public String convertOrderXMLtoEDIFACT(String path) throws IOException, SAXException { |
| 27 | + return convertDocumentWithTempalte(path, "/smooks/smooks-transform-edi.xml"); |
| 28 | + } |
| 29 | + |
| 30 | + public String convertOrderXMLtoEmailMessage(String path) throws IOException, SAXException { |
| 31 | + return convertDocumentWithTempalte(path, "/smooks/smooks-transform-email.xml"); |
| 32 | + } |
| 33 | + |
| 34 | + private String convertDocumentWithTempalte(String path, String config) throws IOException, SAXException { |
| 35 | + Smooks smooks = new Smooks(config); |
| 36 | + |
| 37 | + try { |
| 38 | + StringResult stringResult = new StringResult(); |
| 39 | + smooks.filterSource(new StreamSource(OrderConverter.class.getResourceAsStream(path)), stringResult); |
| 40 | + return stringResult.toString(); |
| 41 | + } finally { |
| 42 | + smooks.close(); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments