forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create class responsible for converting Orders
- Loading branch information
1 parent
086a01f
commit 2335c79
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
libraries/src/main/java/com/baeldung/smooks/converter/OrderConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.baeldung.smooks.converter; | ||
|
||
import com.baeldung.smooks.model.Order; | ||
import org.milyn.Smooks; | ||
import org.milyn.payload.JavaResult; | ||
import org.milyn.payload.StringResult; | ||
import org.xml.sax.SAXException; | ||
|
||
import javax.xml.transform.stream.StreamSource; | ||
import java.io.IOException; | ||
|
||
public class OrderConverter { | ||
|
||
public Order convertOrderXMLToOrderObject(String path) throws IOException, SAXException { | ||
Smooks smooks = new Smooks(OrderConverter.class.getResourceAsStream("/smooks/smooks-mapping.xml")); | ||
try { | ||
JavaResult javaResult = new JavaResult(); | ||
smooks.filterSource(new StreamSource(OrderConverter.class.getResourceAsStream(path)), javaResult); | ||
return (Order) javaResult.getBean("order"); | ||
} finally { | ||
smooks.close(); | ||
} | ||
} | ||
|
||
public Order convertOrderJSONToOrderObject(String path) throws IOException, SAXException { | ||
Smooks smooks = new Smooks(OrderConverter.class.getResourceAsStream("/smooks/smooks-mapping-json.xml")); | ||
try { | ||
JavaResult javaResult = new JavaResult(); | ||
smooks.filterSource(new StreamSource(OrderConverter.class.getResourceAsStream(path)), javaResult); | ||
return (Order) javaResult.getBean("order"); | ||
} finally { | ||
smooks.close(); | ||
} | ||
} | ||
|
||
|
||
public String convertOrderXMLtoEDIFACT(String path) throws IOException, SAXException { | ||
return convertDocumentWithTempalte(path, "/smooks/smooks-transform-edi.xml"); | ||
} | ||
|
||
public String convertOrderXMLtoEmailMessage(String path) throws IOException, SAXException { | ||
return convertDocumentWithTempalte(path, "/smooks/smooks-transform-email.xml"); | ||
} | ||
|
||
private String convertDocumentWithTempalte(String path, String config) throws IOException, SAXException { | ||
Smooks smooks = new Smooks(config); | ||
|
||
try { | ||
StringResult stringResult = new StringResult(); | ||
smooks.filterSource(new StreamSource(OrderConverter.class.getResourceAsStream(path)), stringResult); | ||
return stringResult.toString(); | ||
} finally { | ||
smooks.close(); | ||
} | ||
} | ||
} |