package com.edi;
import java.io.*;
import java.util.List;
import java.util.Locale;
import javax.xml.transform.stream.StreamSource;
import org.milyn.ReaderConfigurator;
import org.milyn.Smooks;
import org.milyn.SmooksException;
import org.milyn.cdr.SmooksResourceConfiguration;
import org.milyn.event.report.HtmlReportGenerator;
import org.milyn.container.ExecutionContext;
import org.milyn.io.StreamUtils;
import org.milyn.payload.StringResult;
import org.milyn.smooks.edi.EDIReaderConfigurator;
import org.xml.sax.SAXException;
public class Main {
// private byte[] messageIn = readInputMessage();
protected String runSmooksTransform() throws IOException, SAXException, SmooksException {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(new Locale("en", "IE"));
// Instantiate Smooks with the config...
// Smooks smooks = new Smooks("smooks-config.xml");
Smooks smooks = new Smooks();
smooks.setReaderConfig(new EDIReaderConfigurator("edi-to-xml-order-mapping.xml"));
// Smooks smooks = new Smooks();
// smooks.addConfigurations(new FileInputStream("D:\\lesson\\camelDemo\\target\\classes\\smooks-config.xml"));
try {
// Create an exec context - no profiles....
ExecutionContext executionContext = smooks.createExecutionContext();
StringResult result = new StringResult();
// Configure the execution context to generate a report...
// executionContext.setEventListener(new HtmlReportGenerator("target/report/report.html"));
// Filter the input message to the outputWriter, using the execution context...
// smooks.filterSource(executionContext, new StreamSource(new ByteArrayInputStream(readInputMessage())), result);
smooks.filterSource(new StreamSource(new ByteArrayInputStream(readInputMessage())), result);
Locale.setDefault(defaultLocale);
return result.getResult();
} finally {
smooks.close();
}
}
public static void main(String[] args) throws IOException, SAXException, SmooksException {
Main main = new Main();
byte[] messageIn = main.readInputMessage();
System.out.println("\n\n==============Message In==============");
System.out.println(new String(messageIn));
System.out.println("======================================\n");
// main.pause("The EDI input stream can be seen above. Press 'enter' to see this stream transformed into XML...");
String messageOut = main.runSmooksTransform();
System.out.println("==============Message Out=============");
System.out.println(messageOut.replaceAll("\r|\n|\t", ""));
System.out.println("======================================\n\n");
// main.pause("And that's it! Press 'enter' to finish...");
}
private byte[] readInputMessage() {
try {
return StreamUtils.readStream(new FileInputStream((Main.class.getClassLoader().getResource("").getFile()+"\\input-message.edi")));
} catch (IOException e) {
e.printStackTrace();
return "<no-message/>".getBytes();
}
}
private void pause(String message) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("> " + message);
in.readLine();
} catch (IOException e) {
}
System.out.println("\n");
}
}
使用smooks框架EDI转XML
最新推荐文章于 2024-03-29 21:19:26 发布