fb
Uncategorized 2 minutes

WSO-2-EASY: Working with Smooks and WSO2

Thijs Volders
Thijs Volders
Strategic Technology Officer
16 jul no 1
Scroll

16_jul_no_1.jpg

WSO-2-EASY: Working with Smooks and WSO2

The name Smooks sounds like something very exotic but it is far more mundane than it sounds: Smooks is an extensible framework for building applications for processing XML and non XML data (CSV, EDI, Java etc) using Java.What Smooks help you do is to take a data Source of some kind (e.g. XML) and from it generate an Event Stream, to which you apply Visitor logic to produce a Result of some other kind (e.g. EDI, CSV, or another structured form). A Visitor is a simple piece of Java logic, that follows the visitor-pattern, that can perform a specific task on the message fragment at which it is targeted.

Think of binding fragment data in a Java object, validate a message fragment or apply an XSLT.

If you want to use Smooks in your WSO2 ESB it is good to know that you can use the latest 1.4 notation (at least for the most recent version of the ESB v4.8.1). The benefit is a highly simplified usage compared to earlier versions.

Take for example this piece of XML that reads Comma Separated Values (CSV) file and extracts some fields from each line.

In the earlier version Smooks the configuration style was somewhat more verbose;

Old-style:

1
2
3
4
5
6
7
8
<!--?xml version="1.0" encoding="UTF-8"?-->
    
           org.milyn.csv.CSVReader
            <paramname="fields">$ignore$,doc_code,$ignore$,
lsh_docnum,
$ignore$,$ignore$,sid_itm_desc,$ignore$,sid_unit_value,
$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,
$ignore$,$ignore$,sid_elm1,sid_elm2,sid_elm3,sid_elm4,
$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,
$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,
$ignore$,$ignore$,$ignore$,sid_ref1,$ignore$,$ignore$,
$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,
$ignore$,$ignore$
           vorderingen
           ;
           vordering
        

In the new style it has been compressed to use convention over configuration, just like Maven’s approach. We leave out some details that are not of interest for the developer. 

To new-style (= smooks 1.4):

1
2
3
<!--?xml version="1.0" encoding="UTF-8"?-->
 
   <csv:readerfields="$ignore$,doc_code,$ignore$,lsh_docnum,
$ignore$,$ignore$,sid_itm_desc,$ignore$,sid_unit_value,
$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,
$ignore$,$ignore$,sid_elm1,sid_elm2,sid_elm3,sid_elm4,
$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,
$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,
$ignore$,$ignore$,$ignore$,sid_ref1,$ignore$,$ignore$,
$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,$ignore$,
$ignore$,$ignore$"   ="" separator=";" quote="'" skiplines="1" rootelementname="vorderingen" recordelementname="vordering">

The benefit is that new style Smooks is much more concise and has been removed from unnecessary technical details when you don’t need to influence it.

Especially the skipLines is a very useful addition which didn’t work in the old-style notation!

Working daily with WSO2 products we sometimes encounter things that even we did not know yet, but in fact are so handy we want to share it with the whole world. We have created a new group of articles called WSO-2-EASY that will show some of best tips and tricks that will help you to either save time or create tidier code  

 WSO2Easy.png