1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.aludratest.content.edifact.edifatto;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
21 import java.util.Map;
22
23 import javax.xml.namespace.QName;
24 import javax.xml.xpath.XPathExpressionException;
25
26 import org.aludratest.content.edifact.EdifactContent;
27 import org.aludratest.exception.TechnicalException;
28 import org.databene.commons.SystemInfo;
29 import org.databene.edifatto.ComparisonSettings;
30 import org.databene.edifatto.EdiChecker;
31 import org.databene.edifatto.EdiFormatSymbols;
32 import org.databene.edifatto.EdiGenerator;
33 import org.databene.edifatto.EdiWriter;
34 import org.databene.edifatto.Edifatto;
35 import org.databene.edifatto.compare.AggregateDiff;
36 import org.databene.edifatto.compare.ComparisonModel;
37 import org.databene.edifatto.model.Interchange;
38 import org.databene.edifatto.xml.NameBasedEdiToXMLConverter;
39 import org.w3c.dom.Element;
40
41
42
43
44
45 public class EdifattoContent implements EdifactContent {
46
47
48 public EdifattoContent() {
49 }
50
51
52 public Interchange readInterchange(InputStream in) {
53 try {
54 return Edifatto.parseEdiFile(in);
55 } catch (IOException e) {
56 throw new TechnicalException("Error parsing EDI document", e);
57 }
58 }
59
60
61 public void writeInterchange(Interchange interchange, OutputStream out, boolean useLinefeed) {
62 try {
63 String lineFeed = (useLinefeed ? SystemInfo.getLineSeparator() : null);
64 new EdiWriter(lineFeed).writeInterchange(interchange, out);
65 } catch (IOException e) {
66 throw new TechnicalException("Error writing EDI document", e);
67 }
68 }
69
70 @Override
71 public Interchange createInterchange(String templateUri, EdiFormatSymbols symbols, Map<String, Object> variables) {
72 try {
73 return EdiGenerator.createInterchange(templateUri, symbols, variables);
74 } catch (IOException e) {
75 throw new TechnicalException("Error creating EDI document", e);
76 }
77 }
78
79
80
81
82
83
84
85
86
87
88
89 public Object queryXML(Interchange interchange, String expression, QName returnType) {
90 try {
91 return Edifatto.queryXML(interchange, expression, returnType);
92 } catch (XPathExpressionException e) {
93 throw new TechnicalException("Error in XPath query '" + expression + "'", e);
94 }
95 }
96
97
98
99
100
101
102 public AggregateDiff diff(Interchange expected, Interchange actual,
103 ComparisonSettings settings, ComparisonModel<Element> model) {
104 try {
105 NameBasedEdiToXMLConverter converter = new NameBasedEdiToXMLConverter();
106 EdiChecker checker = new EdiChecker(settings, model, converter);
107 return checker.diff(expected, actual);
108 } catch (Exception e) {
109 throw new TechnicalException("Error comparing Edifact interchanges", e);
110 }
111 }
112
113
114
115
116 @Override
117 public String formatRecursively(Interchange interchange) {
118 return Edifatto.formatRecursively(interchange);
119 }
120
121 }