org.milyn.edi.unedifact.d09b.D09BInterchangeFactory Maven / Gradle / Ivy
/**
* Generated by Smooks EJC (Edifact Java Compiler).
*/
package org.milyn.edi.unedifact.d09b;
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 D09BInterchangeFactory implements UNEdifactInterchangeFactory {
public static final String MAPPING_MODEL = "urn:org.milyn.edi.unedifact:d09b-mapping:1.5.1";
public static final String INTERCHANGE_BINDING_CONFIG = "/org/milyn/edi/unedifact/d09b/interchange-bindingconfig.xml";
private Smooks smooks;
private String reportPath;
public static D09BInterchangeFactory getInstance() throws IOException, SAXException {
return new D09BInterchangeFactory();
}
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 D09BInterchangeFactory() 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);
}
}