How to use milyn-smooks-flatfile-1.5.2

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to use milyn-smooks-flatfile-1.5.2

Smooks User
Hi,

I have a requirement to read the input flat file line by line (\n delimiter). I have seen milyn-smooks-flatfile-1.5.2.jar has some classes like RegexParser and RegexParserFactory using which we can achieve this. When I try to execute the following code, I am getting a null pointer exception because factory is coming as null. Can anyone suggest how to configure the factory and parser in the smooks-config.xml
file and how to make this code work??

Thanks
Srikanth

My Sample Code:
package org.test;

import org.milyn.Smooks;
import org.milyn.SmooksException;
import org.milyn.flatfile.RecordParser;
import org.milyn.flatfile.regex.RegexParserFactory ;
import org.milyn.io.StreamUtils;
import org.milyn.container.ExecutionContext;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.*;

public class Main {

    private static byte[] messageIn = readInputMessage();

    protected static String runSmooksTransform() throws IOException, SAXException, SmooksException {

        Smooks smooks = new Smooks("smooks-config.xml");

        try {
            ExecutionContext executionContext = smooks.createExecutionContext();
            StringWriter writer = new StringWriter();
           
            RegexParserFactory parserFactory = new RegexParserFactory();
            RecordParser parser = parserFactory.newRecordParser();
            System.out.println(parserFactory.getRegexPattern().pattern());
            parser.setRecordParserFactory(parserFactory);
            parser.setDataSource(new InputSource(new InputStreamReader(new ByteArrayInputStream(messageIn), "UTF-8")));
            System.out.println(parser.nextRecord().toString());
            return parser.nextRecord().toString();
           
         
        } catch (Exception e) {
        e.printStackTrace();
        }
        finally {
            smooks.close();
        }
        return "passed";
    }

    public static void main(String[] args) throws IOException, SAXException, SmooksException {
        String messageOut = Main.runSmooksTransform();
        System.out.println(messageOut);
    }

    private static byte[] readInputMessage() {
        try {
            return StreamUtils.readStream(new FileInputStream("input.log"));
        } catch (IOException e) {
            e.printStackTrace();
            return "<no-message/>".getBytes();
        }
    }
}


My sample config:

<?xml version="1.0"?>

<smooks-resource-list
        xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
        xmlns:regex="http://www.milyn.org/xsd/smooks/regex-1.5.xsd">

     <regex:reader
                  recordDelimiter="\n"
                  regexPattern="\n"
     />

</smooks-resource-list>