Groovy returns error when trying to split on newline

Mark Veenstra picture Mark Veenstra · Sep 19, 2013 · Viewed 18.2k times · Source

Am I trying to split a message on a newline character and use the following script:

    def mesType = "";
def lines = message.getPayload().split("\n");

if ( lines[0][0..6] == '123456' ||  lines[1][0..6] == '123456') {
    mesType = "MES1";
}

if ( lines[0][0..7] == '654321' ||  lines[1][0..7] == '654321') {
    mesType = "MES2";
}

if ( lines[0][0..7] == '234561' ||  lines[1][0..7] == '234561') {
    mesType = "MES3";
}

message.setProperty('mesType', mesType);

return message.getPayload();

But when I use this I got the following error in my log file:

groovy.lang.MissingMethodException: No signature of method: [B.split() is applicable for argument types: (java.lang.String) values: {"\n"} (javax.script.ScriptException)

When I changes the split line to the following:

def lines = message.getPayload().toString().split("\n");

I get an error that the array is OutOfBound, so it looks like it is still not doing anything on the newline character.

The message that comes in (message.getPayload) is a message from the file system, and does contain newline characters. It looks like this:

ABCDFERGDSFF
123456SDFDSFDSFDSF
JGHJGHFHFH

What am I doing wrong? Message is collected using Mule 2.X

Answer

tim_yates picture tim_yates · Sep 19, 2013

looks like message.payload returns a byte[] which you need to get back into a String:

def lines = new String( message.payload, 'UTF-8' ).split( '\n' )

Should get it :-)

Also, I tend to prefer writing things like this as:

def mesType = new String( message.payload, 'US-ASCII' ).split( '\n' ).take( 2 ).with { lines ->
    switch( lines ) {
        case { it.any { line -> line.startsWith( '123456' ) } } : 'MES1' ; break
        case { it.any { line -> line.startsWith( '654321' ) } } : 'MES2' ; break
        case { it.any { line -> line.startsWith( '234561' ) } } : 'MES3' ; break
        default :
          ''
    }
}

Rather than lots of if...else blocks with ranged string access (ie: yours will fail if you get a line with only 3 chars, or only 1 line in the payload)

With Groovy 1.5.6, you're stuck with:

def mesType = new String( message.payload, 'US-ASCII' ).split( '\n' )[ 0..1 ].with { lines ->

And keep your fingers crossed it has at least 2 lines in the payload

Or you're going to need to introduce a method to take up to 2 elements from an array

Can you try:

It might be the with that's breaking in 1.5.6 (not sure)... Try unrolling it back to what you had originally:

def lines = new String( message.payload, 'US-ASCII' ).split( '\n' )[ 0..1 ]
def mesType = 'empty'
if(      lines.any { line -> line.startsWith( '123456' ) } ) mesType = 'MES1'
else if( lines.any { line -> line.startsWith( '654321' ) } ) mesType = 'MES2'
else if( lines.any { line -> line.startsWith( '234561' ) } ) mesType = 'MES3'