All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.milyn.edi.unedifact.d13a.D13AInterchangeFactory Maven / Gradle / Ivy

The newest version!
/**
 * Generated by Smooks EJC (Edifact Java Compiler).
 */
package org.milyn.edi.unedifact.d13a;

import org.milyn.Smooks;
import org.milyn.payload.JavaResult;
import org.milyn.edisax.model.internal.Delimiters;
import org.milyn.edisax.EDIConfigurationException;
import org.xml.sax.SAXException;
import org.milyn.smooks.edi.unedifact.UNEdifactReaderConfigurator;        
import org.milyn.smooks.edi.unedifact.model.UNEdifactInterchange;
import org.milyn.smooks.edi.unedifact.model.UNEdifactInterchangeFactory;        
import org.milyn.container.ExecutionContext;
import org.milyn.event.report.HtmlReportGenerator;

import org.xml.sax.InputSource;
import javax.xml.transform.stream.StreamSource;
import java.io.Reader;
import java.io.Writer;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.IOException;
import javax.xml.transform.Result;

public class D13AInterchangeFactory implements UNEdifactInterchangeFactory {

    public static final String MAPPING_MODEL = "urn:org.milyn.edi.unedifact:d13a-mapping:1.7.1.0";
    public static final String INTERCHANGE_BINDING_CONFIG = "/org/milyn/edi/unedifact/d13a/interchange-bindingconfig.xml";
        
    private Smooks smooks;
    private String reportPath;

    public static D13AInterchangeFactory getInstance() throws IOException, SAXException {
        return new D13AInterchangeFactory();
    }

    public void addConfigurations(InputStream resourceConfigStream) throws SAXException, IOException {
        smooks.addConfigurations(resourceConfigStream);
    }

    public UNEdifactInterchange fromUNEdifact(InputStream ediStream, Result... additionalResults) throws IOException {
        return fromUNEdifact(new InputSource(ediStream), additionalResults);
    }

    public UNEdifactInterchange fromUNEdifact(InputSource ediSource, Result... additionalResults) throws IOException {
        StreamSource streamSource = new StreamSource();

        streamSource.setInputStream(ediSource.getByteStream());
        streamSource.setReader(ediSource.getCharacterStream());

        JavaResult javaResult = new JavaResult();
        int numAdditionalRes = (additionalResults != null? additionalResults.length : 0);
        Result[] results = new Result[numAdditionalRes + 1];

        results[0] = javaResult;
        if(additionalResults != null) {
            System.arraycopy(additionalResults, 0, results, 1, numAdditionalRes);
        }

        ExecutionContext executionContext = smooks.createExecutionContext();

        if(reportPath != null) {
            executionContext.setEventListener(new HtmlReportGenerator(reportPath));
        }

        smooks.filterSource(executionContext, streamSource, results);
        return javaResult.getBean(UNEdifactInterchange.class);
    }

    public void toUNEdifact(UNEdifactInterchange interchange, Writer writer) throws IOException {
        interchange.write(writer);
    }

    public void setReportPath(String reportPath) {
        this.reportPath = reportPath;
    }

    private D13AInterchangeFactory() throws IOException, SAXException {
        smooks = new Smooks();

        try {
            smooks.setReaderConfig(new UNEdifactReaderConfigurator(MAPPING_MODEL));        
        } catch(EDIConfigurationException e) {
            IOException ioException = new IOException("Exception reading UN/EDIFACT Mapping model.");
            ioException.initCause(e);
            throw ioException;
        }

        smooks.addConfigurations(INTERCHANGE_BINDING_CONFIG);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy