Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy returns error when trying to split on newline

Tags:

groovy

mule

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

like image 398
Mark Veenstra Avatar asked Sep 19 '13 14:09

Mark Veenstra


People also ask

How do I split a string into a newline?

Split String at NewlineCreate a string in which two lines of text are separated by \n . You can use + to concatenate text onto the end of a string. Convert \n into an actual newline character. Although str displays on two lines, str is a 1-by-1 string containing both lines of text.

What does split do in groovy?

split() is a function in groovy that splits the String around matches of the given regular expression.

How do you split a string with a new line character in VB net?

One can only split on a char but in most cases NewLine is two characters, Carriage Return (0x0D AKA Char 13) and Line Feed (0x0A AKA Char 10). But in other systems it's just a LF. So I simply remove all instances of the CR and split on the LF. Save this answer.


2 Answers

def lines = "${message.getPayload()}".split('\n');

This approach should also work

like image 175
Artūrs Gloveckis Avatar answered Nov 01 '22 02:11

Artūrs Gloveckis


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'
like image 42
tim_yates Avatar answered Nov 01 '22 03:11

tim_yates