Guest User

JavaToEdifactWithForLoop

a guest
Mar 13th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. /*-
  2. * ========================LICENSE_START=================================
  3. * Java-to-EDIFACT
  4. * %%
  5. * Copyright (C) 2020 Smooks
  6. * %%
  7. * Licensed under the terms of the Apache License Version 2.0, or
  8. * the GNU Lesser General Public License version 3.0 or later.
  9. *
  10. * SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-or-later
  11. *
  12. * ======================================================================
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. * ======================================================================
  27. *
  28. * This program is free software; you can redistribute it and/or
  29. * modify it under the terms of the GNU Lesser General Public
  30. * License as published by the Free Software Foundation; either
  31. * version 3 of the License, or (at your option) any later version.
  32. *
  33. * This program is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  36. * Lesser General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU Lesser General Public License
  39. * along with this program; if not, write to the Free Software Foundation,
  40. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  41. * =========================LICENSE_END==================================
  42. */
  43. package org.smooks.examples.java2edifact;
  44.  
  45. import jakarta.xml.bind.JAXBContext;
  46. import jakarta.xml.bind.JAXBElement;
  47. import jakarta.xml.bind.JAXBException;
  48. import org.smooks.Smooks;
  49. import org.smooks.edifact.binding.d03b.BGMBeginningOfMessage;
  50. import org.smooks.edifact.binding.d03b.C002DocumentMessageName;
  51. import org.smooks.edifact.binding.d03b.C106DocumentMessageIdentification;
  52. import org.smooks.edifact.binding.d03b.C507DateTimePeriod;
  53. import org.smooks.edifact.binding.d03b.C516MonetaryAmount;
  54. import org.smooks.edifact.binding.d03b.DTMDateTimePeriod;
  55. import org.smooks.edifact.binding.d03b.INVOIC;
  56. import org.smooks.edifact.binding.d03b.Interchange;
  57. import org.smooks.edifact.binding.d03b.MOAMonetaryAmount;
  58. import org.smooks.edifact.binding.d03b.Message;
  59. import org.smooks.edifact.binding.service.E0001SyntaxIdentifier;
  60. import org.smooks.edifact.binding.service.E0051ControllingAgencyCoded;
  61. import org.smooks.edifact.binding.service.E0065MessageType;
  62. import org.smooks.edifact.binding.service.E0081SectionIdentification;
  63. import org.smooks.edifact.binding.service.S001SyntaxIdentifier;
  64. import org.smooks.edifact.binding.service.S002InterchangeSender;
  65. import org.smooks.edifact.binding.service.S003InterchangeRecipient;
  66. import org.smooks.edifact.binding.service.S004DateAndTimeOfPreparation;
  67. import org.smooks.edifact.binding.service.S005RecipientReferencePasswordDetails;
  68. import org.smooks.edifact.binding.service.S009MessageIdentifier;
  69. import org.smooks.edifact.binding.service.UNA;
  70. import org.smooks.edifact.binding.service.UNBInterchangeHeader;
  71. import org.smooks.edifact.binding.service.UNHMessageHeader;
  72. import org.smooks.edifact.binding.service.UNSSectionControl;
  73. import org.smooks.edifact.binding.service.UNTMessageTrailer;
  74. import org.smooks.edifact.binding.service.UNZInterchangeTrailer;
  75. import org.smooks.engine.DefaultApplicationContextBuilder;
  76. import org.smooks.io.sink.StringSink;
  77. import org.smooks.io.source.ByteSource;
  78. import org.xml.sax.SAXException;
  79.  
  80. import javax.xml.namespace.QName;
  81. import java.io.ByteArrayOutputStream;
  82. import java.io.IOException;
  83. import java.math.BigDecimal;
  84.  
  85. public class Main {
  86.  
  87. public static void main(String[] args) throws IOException, SAXException, JAXBException {
  88. // for(int i =1; i < 10; i++) {
  89. // run();
  90. // }
  91. System.out.println("\n" + run());
  92. }
  93.  
  94. protected static String run() throws JAXBException, IOException, SAXException {
  95. // Build Java model
  96. Interchange interchange = new Interchange().
  97. withUNA(new UNA().
  98. withCompositeSeparator(":").
  99. withFieldSeparator("+").
  100. withDecimalSeparator(".").
  101. withEscapeCharacter("?").
  102. withRepeatSeparator("*").
  103. withSegmentTerminator("'")).
  104. withUNB(new UNBInterchangeHeader().
  105. withS001(new S001SyntaxIdentifier().
  106. withE0001(E0001SyntaxIdentifier.UNOC).withE0002("4")).
  107. withS002(new S002InterchangeSender().
  108. withE0004("5790000274017").
  109. withE0007("14")).
  110. withS003(new S003InterchangeRecipient().
  111. withE0010("5708601000836").
  112. withE0007("14")).
  113. withS004(new S004DateAndTimeOfPreparation().
  114. withE0017(new BigDecimal(990420)).
  115. withE0019(new BigDecimal(1137))).
  116. withE0020("17").
  117. withS005(new S005RecipientReferencePasswordDetails().withE0022("")).
  118. withE0026("INVOIC").
  119. withE0035(new BigDecimal(1))).
  120. withMessage(new Message().
  121. withContent(new JAXBElement<>(new QName("UNH"), UNHMessageHeader.class, new UNHMessageHeader().
  122. withE0062("30").
  123. withS009(new S009MessageIdentifier().
  124. withE0065(E0065MessageType.INVOIC).
  125. withE0052("D").
  126. withE0054("03B").
  127. withE0051(E0051ControllingAgencyCoded.UN)))).
  128. withContent(new JAXBElement<>(new QName("http://www.ibm.com/dfdl/edi/un/edifact/D03B", "INVOIC", "D03B"), INVOIC.class, new INVOIC().
  129. withBGM(new BGMBeginningOfMessage().
  130. withC002(new C002DocumentMessageName().withE1001("380")).
  131. withC106(new C106DocumentMessageIdentification().withE1004("539602"))).
  132. withDTM(new DTMDateTimePeriod().
  133. withC507(new C507DateTimePeriod().
  134. withE2005("137").
  135. withE2380("19990420").
  136. withE2379("102"))).
  137. withUNS(new UNSSectionControl().
  138. withE0081(E0081SectionIdentification.S)).
  139. withSegGrp50(new INVOIC.SegGrp50().
  140. withMOA(new MOAMonetaryAmount().
  141. withC516(new C516MonetaryAmount().
  142. withE5025("64").
  143. withE5004(new BigDecimal("100.95")).
  144. withE6345("GBP")))))).
  145. withContent(new JAXBElement<>(new QName("UNT"), UNTMessageTrailer.class, new UNTMessageTrailer().
  146. withE0074(new BigDecimal(36)).
  147. withE0062("30")))).
  148. withUNZ(new UNZInterchangeTrailer().
  149. withE0036(new BigDecimal(1)).
  150. withE0020("17"));
  151.  
  152. // Turn Java model into XML
  153. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  154. JAXBContext jaxbContext = JAXBContext.newInstance(Interchange.class, org.smooks.edifact.binding.service.ObjectFactory.class, org.smooks.edifact.binding.d03b.ObjectFactory.class);
  155. jaxbContext.createMarshaller().marshal(interchange, byteArrayOutputStream);
  156.  
  157. // Turn XML into EDIFACT
  158. final Smooks smooks = new Smooks(new DefaultApplicationContextBuilder().withClassLoader(Main.class.getClassLoader()).build());
  159. smooks.addResourceConfigs("java-to-edifact/smooks-config.xml");
  160. StringSink stringResult = new StringSink();
  161. for(int i =1; i < 10; i++){
  162. // StringSink stringResult = new StringSink();
  163. stringResult = new StringSink();
  164. long startTime = System.currentTimeMillis();
  165. smooks.filterSource(new ByteSource(byteArrayOutputStream.toByteArray()), stringResult);
  166. ;
  167. long elapsedTime = System.currentTimeMillis() - startTime;
  168. System.out.println("Time taken to finish filter source jsonToEdi with java : " + elapsedTime);
  169. }
  170.  
  171.  
  172. return stringResult.getResult();
  173. }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment