[DISCUSS] JAXB Integration with Smooks

8 views
Skip to first unread message

Claude Mamo

unread,
Mar 5, 2025, 12:06:09 PMMar 5
to smook...@googlegroups.com
In one of our online check-ins, Mantra raised the need to be able to perform JAXB marhsalling and unmarshalling from within the Smooks config. Currently, we need to switch between Java and Smooks XML API to accomplish this as demonstrated in the java-to-edifact and edifact-to-java examples.

I propose we implement reader and visitor resources in the JavaBean cartridge for the corresponding marshall and unmarshall JAXB operations. Let's consider how much marshalling could look like in the Smooks config:

<?xml version="1.0"?>
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:edifact="https://www.smooks.org/xsd/smooks/edifact-2.0.xsd"
                      xmlns:core="https://www.smooks.org/xsd/smooks/smooks-core-1.6.xsd"
                      xmlns:jb="https://www.smooks.org/xsd/smooks/javabean-1.6.xsd">

    <jb:jaxb-marshaller class="org.smooks.edifact.binding.d03b.Interchange">
        <jb:objectFactories>
            <jb:objectFactory>org.smooks.edifact.binding.service.ObjectFactory</jb:objectFactory>
            <jb:objectFactory>org.smooks.edifact.binding.d03b.ObjectFactory</jb:objectFactory>
        </jb:objectFactories>
    </jb:jaxb-marshaller>

    ...
       ...

</smooks-resource-list>

The client would leverage the existing JavaSource class to filter as follows:

 Interchange interchange = new Interchange().
                withUNA(new UNA().
                        withCompositeSeparator(":").
                        withFieldSeparator("+").
                        withDecimalSeparator(".").
                        withEscapeCharacter("?").
                        withRepeatSeparator("*").
                        withSegmentTerminator("'")).
                        ...;

final Smooks smooks = new Smooks("smooks-config.xml");
StringSink stringResult = new StringSink();
smooks.filterSource(new JavaSource(interchange), stringResult);

Now consider how unmarshalling could look like from the Smooks config:

<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:edifact="https://www.smooks.org/xsd/smooks/edifact-2.0.xsd"
                      xmlns:jb="https://www.smooks.org/xsd/smooks/javabean-1.6.xsd">
   
    ...
    ...

    <jb:jaxb-unmarshaller beanId="interchange" class="org.smooks.edifact.binding.d03b.Interchange" createOnElement="/Interchange">
        <jb:objectFactories>
            <jb:objectFactory>org.smooks.edifact.binding.service.ObjectFactory</jb:objectFactory>
            <jb:objectFactory>org.smooks.edifact.binding.d03b.ObjectFactory</jb:objectFactory>
        </jb:objectFactories>
    </jb:jaxb-unmarshaller>

</smooks-resource-list>

jaxb-unmarshaller would behave very similarly to the jb:bean visitor. The biggest differences are that (1) the binding would be automatic (i.e, no need to define value, wiring, and expression visitors) AND (2) all the child events are kept in-memory before binding the targeted event and its child events to the bean. This would not scale well if jaxb-unmarshaller targeted large chunks of events so we should document the performance limitation compared to jb:bean.

Claude

Claude Mamo

unread,
Mar 10, 2025, 5:16:40 AMMar 10
to smook...@googlegroups.com
I've merged a no. of PRs following this proposal. A side effect of adding these two new resources to the JavaBean cartridge is that Java 11 is now the minimum supported version for this cartridge (required by the Jakarta API). This is a breaking change so we'll have to bump the cartridge's major version. I've revised the edifact-to-java and java-to-edifact examples to incorporate these features but it's still not too late to get feedback.

Claude
Reply all
Reply to author
Forward
0 new messages