1 package org.kuali.ole.converter;
2
3 import org.milyn.Smooks;
4 import org.milyn.SmooksException;
5 import org.milyn.container.ExecutionContext;
6 import org.milyn.io.StreamUtils;
7 import org.milyn.payload.StringResult;
8 import org.xml.sax.SAXException;
9
10 import javax.xml.transform.stream.StreamSource;
11 import java.io.*;
12 import java.net.URISyntaxException;
13 import java.net.URL;
14 import java.util.Locale;
15
16
17
18
19
20
21
22
23 public class OLEEDIConverter {
24 private Smooks smooks;
25
26 public OLEEDIConverter() {
27 try {
28 String configurationFileName = getConfigurationFileName();
29 InputStream inputStream = getClass().getResourceAsStream(configurationFileName);
30 smooks = new Smooks(inputStream);
31 } catch (FileNotFoundException e) {
32 e.printStackTrace();
33 } catch (SAXException e) {
34 e.printStackTrace();
35 } catch (IOException e) {
36 e.printStackTrace();
37 }
38 }
39
40
41 public String convertToXML(String ediFile) throws IOException, SAXException, SmooksException {
42 byte[] messageIn = ediFile.getBytes();
43 ExecutionContext executionContext = smooks.createExecutionContext();
44 try {
45 Locale defaultLocale = Locale.getDefault();
46 Locale.setDefault(new Locale("en", "IE"));
47 StringResult javaResult = new StringResult();
48 smooks.filterSource(executionContext, new StreamSource(new ByteArrayInputStream(messageIn)), javaResult);
49 Locale.setDefault(defaultLocale);
50 return "<orders>"+javaResult.getResult()+"</orders>";
51 } finally {
52 smooks.close();
53 }
54 }
55
56 private String getConfigurationFileName() {
57 return "edi-config.xml";
58 }
59 }