Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.이 콘텐츠는 선택한 언어로 제공되지 않습니다.
12.18. Implementing a Source Reader for use with Smooks
- You should first implement a basic reader class as shown below:
public class MyCSVReader implements SmooksXMLReader { // Implement all of the XMLReader methods... }
public class MyCSVReader implements SmooksXMLReader { // Implement all of the XMLReader methods... }
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Two methods from theorg.xml.sax.XMLReader
interface are of particular interest:setContentHandler(ContentHandler)
is called by Smooks Core. It sets theorg.xml.sax.ContentHandler
instance for the reader. Theorg.xml.sax.ContentHandler
instance methods are called from inside theparse(InputSource)
method.parse(InputSource)
: This is the method that receives the Source data input stream, parses it (i.e. in the case of this example, the CSV stream) and generates the SAX event stream through calls to theorg.xml.sax.ContentHandler
instance supplied in thesetContentHandler(ContentHandler)
method.
Refer to http://download.oracle.com/javase/6/docs/api/org/xml/sax/ContentHandler.html for more details. - Configure your CSV reader with the names of the fields associated with the CSV records. Configuring a custom reader implementation is the same for any Smooks component. See the example below:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Now that you have the basic Reader implementation stub, you can start writing unit tests to test the new reader implementation. To do this you will need something with CSV input. Observe the example below featuring a simple list of names in a file with the name
names.csv
:Tom,Jones Mike,Jones Mark,Jones
Tom,Jones Mike,Jones Mark,Jones
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Use a test Smooks configuration to configure Smooks with your MyCSVReader. As stated before, everything in Smooks is a resource and can be configured with the basic
<resource-config>
configuration. While this works fine, it's a little noisy, so Smooks provides a basic<reader>
configuration element specifically for the purpose of configuring a reader. The configuration for the test looks like the following, in themycsvread-config.xml
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Implement the JUnit test class:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Implement the
parse
method:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Run the unit test class to see the following output on the console (formatted):
Copy to Clipboard Copied! Toggle word wrap Toggle overflow After this, it is a case of expanding the tests, hardening the reader implementation code, and so on. Then you can use your reader to perform all sorts of operations supported by Smooks.