View Javadoc
1   /*
2    * Copyright (C) 2010-2014 Hamburg Sud and the contributors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Parses and saves EDIFACT and X12 documents from and to streams.
43   * @author Volker Bergmann
44   */
45  public class EdifattoContent implements EdifactContent {
46  
47      /** Constructor. */
48      public EdifattoContent() {
49      }
50  
51      /** Parses an EDIFACT or X12 interchange available in an {@link InputStream}. */
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      /** Writes an EDIFACT or X12 interchange to an {@link OutputStream}. */
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       * Creates an XML representation of the interchange and performs an XPath query on it.
81       * @param interchange the interchange to query
82       * @param expression the XPath query to perform
83       * @param returnType determines the type of the returned object: 
84       *   {@link javax.xml.xpath.XPathConstants#STRING} for a single {@link java.lang.String},
85       *   {@link javax.xml.xpath.XPathConstants#NODE} for a single {@link org.w3c.dom.Element},
86       *   {@link javax.xml.xpath.XPathConstants#NODESET} for a {@link org.w3c.dom.NodeList}
87       * @return the found nodes of the interchange in the form of XML elements
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      /** Finds out the differences between two EDIFACT or X12 interchanges, 
98       *  ignoring elements that are tolerated by the {@link ComparisonSettings}.
99       *  @param expected 
100      *  @param actual 
101      *  @return an {@link AggregateDiff} between the documents */
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     /** Formats a full interchange structure recursively as String.
114      *  @param interchange the Edifact interchange to format
115      *  @return a string representation of the interchange */
116     @Override
117     public String formatRecursively(Interchange interchange) {
118         return Edifatto.formatRecursively(interchange);
119     }
120 
121 }