Skip to content

Smooks cartridge leveraging Apache Daffodil to parse files and unparse XML

License

Notifications You must be signed in to change notification settings

smooks/smooks-dfdl-cartridge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smooks DFDL Cartridge

Maven Central Sonatype Nexus (Snapshots) Build Status

The DFDL cartridge opens up Smooks to an incredible number of data formats (e.g., SWIFT, ISO8583, HL7). In fact, this cartridge forms the foundations of the EDI and EDIFACT cartridges. The DFDL cartridge deserializes (i.e., parses) non-XML data and serializes (i.e., unparses) XML according to the structure described in a DFDL schema. Take the subsequent DFDL schema as an example:

csv.dfdl.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" xmlns:ex="http://example.com"
  targetNamespace="http://example.com" elementFormDefault="unqualified">

  <xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />

  <xs:annotation>
    <xs:appinfo source="http://www.ogf.org/dfdl/">
      <dfdl:format ref="ex:GeneralFormat" separator="" initiator=""
        terminator="" textTrimKind="none" initiatedContent="no" ignoreCase="no"
        separatorPosition="infix" occursCountKind="implicit"
        emptyValueDelimiterPolicy="both" representation="text" textNumberRep="standard"
        lengthKind="delimited" encoding="ASCII" encodingErrorPolicy="error" />
    </xs:appinfo>
  </xs:annotation>

  <xs:element name="file">
    <xs:complexType>
      <xs:sequence dfdl:separator="%NL;" dfdl:separatorPosition="postfix">
        <xs:element name="header" minOccurs="0" maxOccurs="1"
          dfdl:occursCountKind="implicit">
          <xs:complexType>
            <xs:sequence dfdl:separator=",">
              <xs:element name="title" type="xs:string" maxOccurs="unbounded" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="record" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence dfdl:separator=",">
              <xs:element name="item" type="xs:string" maxOccurs="unbounded"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This schema describes the structure of CSV data like the one below:

input.csv
last,first,middle,DOB
smith,robert,brandon,1988-03-24
johnson,john,henry,1986-01-23
jones,arya,cat,1986-02-19

A Smooks config parsing the above CSV using the DFDL cartridge would be written as:

smooks-config.xml
<?xml version="1.0"?>
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:dfdl="https://www.smooks.org/xsd/smooks/dfdl-1.0.xsd">

    <dfdl:parser schemaUri="/csv.dfdl.xsd"/>

    ...

</smooks-resource-list>

dfdl:parser is a reader and its schemaUri attribute references the DFDL schema driving the parsing behaviour. Assuming input.csv is the source, dfdl:parser will generate the event stream:

<ex:file xmlns:ex="http://example.com">
    <header>
        <title>last</title>
        <title>first</title>
        <title>middle</title>
        <title>DOB</title>
    </header>
    <record>
        <item>smith</item>
        <item>robert</item>
        <item>brandon</item>
        <item>1988-03-24</item>
    </record>
    <record>
        <item>johnson</item>
        <item>john</item>
        <item>henry</item>
        <item>1986-01-23</item>
    </record>
    <record>
        <item>jones</item>
        <item>arya</item>
        <item>cat</item>
        <item>1986-02-19</item>
    </record>
</ex:file>

Shown in the next snippet is a pipeline enclosing the dfdl:unparser visitor:

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

    ...

    <core:smooks filterSourceOn="#document">
        <core:action>
            <core:inline>
                <core:replace/>
            </core:inline>
        </core:action>
        <core:config>
            <smooks-resource-list>
                <dfdl:unparser schemaUri="/csv.dfdl.xsd" unparseOnNode="*"/>
            </smooks-resource-list>
        </core:config>
    </core:smooks>

</smooks-resource-list>

In contrast to the dfdl:parser schemaUri attribute , the schemaUri schema in dfdl:unparser drives the unparsing behaviour. dfdl:unparser replaces each node in the event stream with its serialized CSV counterpart, essentially implementing a pass-through application.

The DFDL cartridge supports variables, on disk caching, and trace debugging. Consult the XSD documentation for further information. It is strongly advised to learn DFDL before authoring a DFDL schema. Resources to get started with DFDL include:

Parser reader options

Indent

Indent the generated event stream to make it easier to read. Useful for troubleshooting. The default value is false. Example:

smooks-config.xml
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:dfdl="https://www.smooks.org/xsd/smooks/dfdl-1.0.xsd">

    <dfdl:parser schemaUri="/csv.dfdl.xsd" indent="true"/>

</smooks-resource-list>

Cache on disk

Persist DFDL schema on disk to reduce compilation time in subsequent runs. The default value is false. Example:

smooks-config.xml
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:dfdl="https://www.smooks.org/xsd/smooks/dfdl-1.0.xsd">

    <dfdl:parser schemaUri="/csv.dfdl.xsd" cacheOnDisk="true"/>

</smooks-resource-list>

Validation mode

Validation modes for validating the resulting infoset against the DFDL schema. The following values are supported:

Value Description

Off

Turn off all validation against the DFDL schema.

Limited

Perform XSD validation of facets, minLength, maxLength, enumeration, minInclusive, minExclusive, maxInclusive, maxExclusive, and maxOccurs constraints. Validation failures will be printed in the log but will not interrupt parsing or unparsing.

Full

Perform full schema validation using Xerces. A validation failure will abort parsing and throw a org.smooks.api.SmooksException.

The default value is Off. Example:

smooks-config.xml
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:dfdl="https://www.smooks.org/xsd/smooks/dfdl-1.0.xsd">

    <dfdl:parser schemaUri="/csv.dfdl.xsd" validationMode="Limited"/>

</smooks-resource-list>

Validation failures can be retrieved from the Smooks execution context as shown below:

...

org.smooks.Smooks smooks = new org.smooks.Smooks();
org.smooks.api.ExecutionContext executionContext = smooks.createExecutionContext();
smooks.filterSource(executionContext, source, result);

List<org.apache.daffodil.japi.Diagnostic> diagnostics = executionContext.get(org.smooks.cartridges.dfdl.parser.DfdlParser.DIAGNOSTICS_TYPED_KEY);
...

Debugging

Enable/disable trace debugging. The default value is false. Example:

smooks-config.xml
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:dfdl="https://www.smooks.org/xsd/smooks/dfdl-1.0.xsd">

    <dfdl:parser schemaUri="/csv.dfdl.xsd" debugging="true"/>

</smooks-resource-list>

Schematron validation

Apply standalone or embedded Schematron rules within the DFDL schema. Note that Schematron validation leads to the input stream being loaded into memory therefore such validation is not recommended for large streams.

Standalone Schematron rules are applied like this:

smooks-config.xml
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:dfdl="https://www.smooks.org/xsd/smooks/dfdl-1.0.xsd">

    <dfdl:parser schemaUri="/csv.dfdl.xsd">
        <dfdl:schematron url="rules.sch"/>
    </dfdl:parser>

</smooks-resource-list>

Embedded rules are applied as follows:

smooks-config.xml
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:dfdl="https://www.smooks.org/xsd/smooks/dfdl-1.0.xsd">

    <dfdl:parser schemaUri="/csv.dfdl.xsd">
        <dfdl:schematron/>
    </dfdl:parser>

</smooks-resource-list>

Unparser visitor options

Cache on disk

Persist DFDL schema on disk to reduce compilation time in subsequent runs. The default value is false. Example:

smooks-config.xml
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:dfdl="https://www.smooks.org/xsd/smooks/dfdl-1.0.xsd">

    <dfdl:unparser schemaUri="/csv.dfdl.xsd" unparseOnNode="*" cacheOnDisk="true"/>

</smooks-resource-list>

Validation mode

Validation modes for validating the input infoset against the DFDL schema. The following values are supported:

Value Description

Off

Turn off all validation against the DFDL schema.

Limited

Perform XSD validation of facets, minLength, maxLength, enumeration, minInclusive, minExclusive, maxInclusive, maxExclusive, and maxOccurs constraints. Validation failures will be printed in the log but will not interrupt parsing or unparsing.

Full

Perform full schema validation using Xerces. A validation failure will abort unparsing and throw a org.smooks.api.SmooksException.

The default value is Off. Example:

smooks-config.xml
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:dfdl="https://www.smooks.org/xsd/smooks/dfdl-1.0.xsd">

    <dfdl:unparser schemaUri="/csv.dfdl.xsd" unparseOnNode="*" validationMode="Limited"/>

</smooks-resource-list>

Debugging

Enable/disable trace debugging. The default value is false. Example:

smooks-config.xml
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:dfdl="https://www.smooks.org/xsd/smooks/dfdl-1.0.xsd">

    <dfdl:unparser schemaUri="/csv.dfdl.xsd" unparseOnNode="*" debugging="true"/>

</smooks-resource-list>

Maven Coordinates

pom.xml
<dependency>
    <groupId>org.smooks.cartridges</groupId>
    <artifactId>smooks-dfdl-cartridge</artifactId>
    <version>1.0.0-RC4</version>
</dependency>

XML Namespace

xmlns:dfdl="https://www.smooks.org/xsd/smooks/dfdl-1.0.xsd"

License

Smooks DFDL Cartridge is open source and licensed under the terms of the Apache License Version 2.0, or the GNU Lesser General Public License version 3.0 or later. You may use Smooks DFDL Cartridge according to either of these licenses as is most appropriate for your project.

SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-or-later