Guest User

SmooksFile

a guest
Mar 12th, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. package org.smooks.examples.xml2edi;
  2.  
  3. import org.smooks.Smooks;
  4. import org.smooks.api.ExecutionContext;
  5. import org.smooks.api.SmooksException;
  6. import org.smooks.api.resource.config.ResourceConfig;
  7. import org.smooks.engine.DefaultApplicationContextBuilder;
  8. import org.smooks.engine.resource.config.DefaultResourceConfig;
  9. import org.smooks.io.payload.ByteSource;
  10. import org.smooks.io.payload.StringResult;
  11. import org.smooks.support.StreamUtils;
  12. import org.xml.sax.SAXException;
  13.  
  14. import javax.xml.transform.stream.StreamSource;
  15. import java.io.*;
  16. import java.util.*;
  17.  
  18. public class Main {
  19.  
  20. private static byte[] messageIn = readInputMessage();
  21.  
  22. private static String interchange = """
  23. <D99B:Interchange
  24. xmlns:D99B="http://www.ibm.com/dfdl/edi/un/edifact/D99B"
  25. xmlns:srv="http://www.ibm.com/dfdl/edi/un/service/4.1"
  26. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  27. <UNB>
  28. <S001><E0001>UNOC</E0001><E0002>3</E0002></S001>
  29. <S002><E0004>AAR679E</E0004><E0007>AAR679E</E0007></S002>
  30. <S003><E0010>AAA336C</E0010></S003>
  31. <S004><E0017>040127</E0017><E0019>0730</E0019></S004>
  32. <E0020>241030000110</E0020>
  33. <E0026></E0026>
  34. <E0031>1</E0031>
  35. <E0035>1</E0035>
  36. </UNB>
  37. <D99B:Message>
  38. <UNH>
  39. <E0062>1</E0062>
  40. <S009>
  41. <E0065>CUSCAR</E0065>
  42. <E0052>D</E0052>
  43. <E0054>99B</E0054>
  44. <E0051>UN</E0051>
  45. </S009>
  46. </UNH>
  47. <D99B:CUSCAR>
  48. <BGM>
  49. <C002>
  50. <E1001>933</E1001>
  51. <E1000>SEAINT</E1000>
  52. </C002>
  53. <C106>
  54. <E1004>202410290064</E1004>
  55. <E1056>53</E1056>
  56. </C106>
  57. <E1225>9</E1225>
  58. </BGM>
  59. <SegGrp-2>
  60. <NAD>
  61. <E3035>CN</E3035>
  62. <C058>
  63. <E3124>JAMESTRONG PACKAGING AUSTRALIA PTY</E3124>
  64. <E3124>2 HALLSTROM AVENUE TAREE NSW 2430</E3124>
  65. </C058>
  66. </NAD>
  67. </SegGrp-2>
  68. <SegGrp-4>
  69. <TDT>
  70. <E8051>20</E8051>
  71. <E8028>186S</E8028>
  72. <C228><E8179>11</E8179></C228>
  73. <C222>
  74. <E8213>9329538</E8213>
  75. <E3055>11</E3055>
  76. </C222>
  77. </TDT>
  78. </SegGrp-4>
  79. </D99B:CUSCAR>
  80. <UNT>
  81. <E0074>44</E0074>
  82. <E0062>1</E0062>
  83. </UNT>
  84. </D99B:Message>
  85. <UNZ>
  86. <E0036>1</E0036>
  87. <E0020>241030000110</E0020>
  88. </UNZ>
  89. </D99B:Interchange>
  90. """;
  91.  
  92. protected static String convertInterchangeToEdifact() {
  93. ResourceConfig edifactUnparser = new DefaultResourceConfig("*", new Properties());
  94. edifactUnparser.setResource("");
  95. edifactUnparser.setParameter("schemaUri", "/d99b/EDIFACT-Messages.dfdl.xsd");
  96. edifactUnparser.setResourceType("dfdl");
  97. edifactUnparser.setParameter("messageType", "CUSCAR");
  98. edifactUnparser.setParameter("dataProcessorFactory", "org.smooks.cartridges.edifact.EdifactDataProcessorFactory");
  99.  
  100. ResourceConfig pipeline = new DefaultResourceConfig("/Interchange", new Properties());
  101. pipeline.setResource("org.smooks.engine.resource.visitor.smooks.NestedSmooksVisitor");
  102. pipeline.setParameter("action", "REPLACE");
  103. pipeline.setParameter("smooksResourceList", "<smooks-resource-list xmlns=\"https://www.smooks.org/xsd/smooks-2.0.xsd\">" + edifactUnparser.toXml() + "</smooks-resource-list>");
  104.  
  105. String encoding = "UTF-8";
  106. Runtime runtime = Runtime.getRuntime();
  107. System.gc();
  108. try { Thread.sleep(100); } catch (InterruptedException e) { }
  109.  
  110. HashMap<String, Smooks> map = new HashMap<>();
  111. Smooks smooks = new Smooks(new DefaultApplicationContextBuilder().setClassLoader(Main.class.getClassLoader()).build());
  112. StringResult stringResult = new StringResult();
  113. map.put("smooksObject", smooks);
  114.  
  115. try {
  116. smooks.addConfiguration(pipeline);
  117. ExecutionContext executionContext = smooks.createExecutionContext();
  118. executionContext.setContentEncoding(encoding);
  119.  
  120. for (int i = 1; i < 10; i++) {
  121. long startTime = System.currentTimeMillis();
  122. smooks.filterSource(executionContext, new ByteSource(interchange.getBytes(encoding)), stringResult);
  123. long elapsedTime = System.currentTimeMillis() - startTime;
  124. System.out.println("Time taken to finish filter source jsonToEdi : " + elapsedTime);
  125. }
  126. } catch (Exception e) {
  127. System.out.println("Error processing Smooks transformation");
  128. e.printStackTrace();
  129. }
  130.  
  131. return stringResult.getResult();
  132. }
  133.  
  134. public static void main(String[] args) throws IOException, SAXException, SmooksException {
  135. System.out.println("\n\n==============Message In==============");
  136. System.out.println(new String(messageIn));
  137. System.out.println("======================================\n");
  138.  
  139. String messageOut = Main.convertInterchangeToEdifact();
  140.  
  141. System.out.println("==============Message Out=============");
  142. System.out.println(messageOut);
  143. System.out.println("======================================\n\n");
  144. }
  145.  
  146. private static byte[] readInputMessage() {
  147. try {
  148. return StreamUtils.readStream(new FileInputStream("input-message.xml"));
  149. } catch (IOException e) {
  150. e.printStackTrace();
  151. return "<no-message/>".getBytes();
  152. }
  153. }
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment