View Javadoc
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   * Created by IntelliJ IDEA.
18   * User: pvsubrah
19   * Date: 2/29/12
20   * Time: 10:06 PM
21   * To change this template use File | Settings | File Templates.
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          Locale defaultLocale = Locale.getDefault();
45          Locale.setDefault(new Locale("en", "IE"));
46          try {
47              StringResult javaResult = new StringResult();
48              smooks.filterSource(executionContext, new StreamSource(new ByteArrayInputStream(messageIn)), javaResult);
49              return "<orders>" + javaResult.getResult() + "</orders>";
50          } finally {
51              Locale.setDefault(defaultLocale);
52              smooks.close();
53          }
54      }
55  
56      private String getConfigurationFileName() {
57          return "edi-config.xml";
58      }
59  }