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

org.milyn.edi.unedifact.d05a.D05AInterchangeFactory Maven / Gradle / Ivy

There is a newer version: 1.7.1.0
Show newest version
/**
 * Generated by Smooks EJC (Edifact Java Compiler).
 */
package org.milyn.edi.unedifact.d05a;

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.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 D05AInterchangeFactory implements UNEdifactInterchangeFactory {

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

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

    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);
        }

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

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

    private D05AInterchangeFactory() 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