使用smooks框架EDI转XML

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值