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