从 java 对象准备 edi 数据

From java Object prepare the edi data

我是 edi 数据的新手。我正在使用 smooks api 读取 edi 数据并能够将其解析为 java 对象。我想将 java 对象转换为 edi 数据,因为我没有得到太多信息。这是我尝试从 edi 文件读取并创建 java 对象 -

的示例
customOrder.edi - COR*130*PINGPONG02*You got it to work*1230
---------------
POJO -
------
    public class CustomOrder implements Serializable{
    private int number;
    private String sender;
    private String message;
    private int price;
    // setter and getter
    }
custom-order-mapping.xml -
-------------------------
    <?xml version="1.0" encoding="UTF-8"?><medi:edimap xmlns:medi="http://www.milyn.org/schema/edi-message-mapping-1.3.xsd">
    <medi:description name="DVD Order" version="1.0" />

    <medi:delimiters segment="&#10;" field="*" component="^" sub-component="~" />

    <medi:segments xmltag="CustomOrder">
        <medi:segment segcode="COR" xmltag="co">
            <medi:field xmltag="number" />
            <medi:field xmltag="sender" />
            <medi:field xmltag="message" />
            <medi:field xmltag="price" />
        </medi:segment>
    </medi:segments>
</medi:edimap>

smooks-config.xml -
------------------
<?xml version="1.0"?>
<smooks-resource-list
    xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
    xmlns:edi="http://www.milyn.org/xsd/smooks/edi-1.1.xsd"
    xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd"
    xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd">

    <edi:reader mappingModel="/example/custom-order-mapping.xml" />


    <jb:bean beanId="customer" class="example.model.CustomOrder" createOnElement="co">
        <!-- Customer bindings... -->
        <jb:value property="number" data="#/number" decoder="Integer"/>
        <jb:value property="sender" data="#/sender" decoder="String"/>
        <jb:value property="message" data="#/message" decoder="String"/>
        <jb:value property="price" data="#/price" decoder="Integer"/>
    </jb:bean>
</smooks-resource-list>

 Main method -
--------------
     Main smooksMain = new Main();
        ExecutionContext executionContext = smooksMain.smooks.createExecutionContext();
        org.milyn.payload.JavaResult result = smooksMain.runSmooksTransform(executionContext);
        CustomOrder custOrder = (CustomOrder) result.getBean("customer");
        // Need to get to edi data from java object custOrder
       // Please help me - this part of code

我想从 java 对象准备 edi 数据。如果除了 Smooks 之外还有任何其他 api/framework 会做同样的事情,me.please 就可以告诉我,谢谢。

我搜索了一下,从 smooks 论坛了解到,要从 java 对象准备 edi 数据,我们必须使用 Edifact Java 编译器(EJC)。 上面的例子是从 edi 数据准备 java 对象。 Pojo class 必须实现 EDIWritable 并覆盖 write method.Here 是更改后的 Pojo class -

public class CustomOrder implements Serializable, EDIWritable{
private int number;
private IntegerDecoder numberDecoder;
private String sender;
private String message;
private int price;
private IntegerDecoder priceDecoder;
public CustomOrder() {
    numberDecoder = new IntegerDecoder();
    priceDecoder = new IntegerDecoder();
}
public int getNumber() {
    return number;
}
public void setNumber(int number) {
    this.number = number;
}
public void setSender(String sender) {
    this.sender = sender;
}
public String getSender() {
    return sender;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}
public int getPrice() {
    return price;
}
public void setPrice(int price) {
    this.price = price;
}
public  void write(Writer writer, Delimiters delimiters) throws IOException {
    // TODO Auto-generated method stub
     Writer nodeWriter = writer;

        if(number != 0) {
            nodeWriter.write(delimiters.escape(numberDecoder.encode(number)));
        }
        nodeWriter.write(delimiters.getField());

        if(sender != null) {
            nodeWriter.write(delimiters.escape(sender.toString()));
        }
        nodeWriter.write(delimiters.getField());

        if(message != null) {
            nodeWriter.write(delimiters.escape(message.toString()));
        }
        nodeWriter.write(delimiters.getField());
        if(price != 0) {
            nodeWriter.write(delimiters.escape(priceDecoder.encode(price)));
        }
        //nodeWriter.write(delimiters.getField());

        writer.write(delimiters.getSegmentDelimiter());
        writer.flush();

}
}

接下来,我们要准备pojo的工厂class -

自定义订单工厂

public class CustomOrderFactory {
 private Smooks smooks;
 private Delimiters delimiters;
 public static CustomOrderFactory getInstance() throws IOException, SAXException {
        return new CustomOrderFactory();
    }
    public void addConfigurations(InputStream resourceConfigStream) throws SAXException, IOException {
        smooks.addConfigurations(resourceConfigStream);
    }
    public void toEDI(CustomOrder instance, Writer writer) throws IOException {
        instance.write(writer, delimiters);
    }

    private CustomOrderFactory() throws IOException, SAXException {
        smooks = new Smooks(CustomOrderFactory.class.getResourceAsStream("smooks-config.xml"));
        System.out.println("smooks is prepared");
        try {   
            Edimap edimap = EDIConfigDigester.digestConfig(CustomOrderFactory.class.getResourceAsStream("custom-order-mapping.xml"));
            System.out.println("ediMap is prepared");
            delimiters = edimap.getDelimiters();
            System.out.println("delimeter is prepared");
        } catch(EDIConfigurationException e) {
            IOException ioException = new IOException("Exception reading EDI Mapping model.");
            ioException.initCause(e);
            throw ioException;
        }
    }
}

一旦 CustomOrder 对象准备就绪,如上面 Main class 中所示。我们必须使用这个对象来转换为 edi 数据格式。这是完整的 Main class -

Main class
----------
Main smooksMain = new Main();
ExecutionContext executionContext = smooksMain.smooks.createExecutionContext();
org.milyn.payload.JavaResult result = smooksMain.runSmooksTransform(executionContext);
CustomOrder custOrder = (CustomOrder) result.getBean("customer");
// Prepare edi data from java object custOrder
CustomOrderFactory customOrderFactory = CustomOrderFactory.getInstance();
OutputStream os = new FileOutputStream("createdEDIFile.edi");
customOrderFactory.toEDI(custOrder, new OutputStreamWriter(os));    
System.out.println("Edi file is created from java object");

就是这样。希望它会有所帮助。谢谢。