尝试拆分换行符时,Groovy返回错误

时间:2013-09-19 14:56:56

标签: groovy mule

我是否尝试在换行符上拆分消息并使用以下脚本:

    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();

但是当我使用它时,我的日志文件中出现以下错误:

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

当我将分割线更改为以下内容时:

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

我得到一个错误,该数组是OutOfBound,所以看起来它仍然没有对换行符进行任何操作。

来自(message.getPayload)的消息是来自文件系统的消息,并且包含换行符。它看起来像这样:

ABCDFERGDSFF
123456SDFDSFDSFDSF
JGHJGHFHFH

我做错了什么?使用Mule 2.X

收集消息

2 个答案:

答案 0 :(得分:6)

看起来message.payload会返回byte[],您需要返回一个字符串:

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

应该得到它: - )

另外,我倾向于写这样的东西:

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 :
          ''
    }
}

而不是许多具有远程字符串访问权限的if...else块(例如:如果你的行只有3个字符,或者有效负载中只有1行,则会失败)

使用Groovy 1.5.6,你会遇到:

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

并保持手指交叉,有效载荷中至少有2行

或者您需要引入一种方法来从数组中获取最多2个元素

你可以尝试:

可能是with在1.5.6中突破(不确定)...尝试将其展开回原来的版本:

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'

答案 1 :(得分:0)

const archiver = require('archiver')
const zip = archiver('zip')
const path = require('path')
const fs = require('fs')
const appDir = path.dirname(require.main.filename)

exports.FileArchiver = function (feedArray, res) {
    // const app = this.app;
    const uploadsDir = path.join(appDir, '/uploads/');
    const templatesDir = path.join(appDir, '/templates/');
    const extensions = [".jpg", ".png", ".svg"];
    let imageArray = [];

    const feedArrayObject = JSON.parse(feedArrayString);

    feedArrayObject.forEach(function(x){iterate(x)}); // grab image names from object
    imageArray = uniq_fast(imageArray); // remove duplicates

    // zip images
    for (let i = 0; i < imageArray.length; i++) {
      console.log(imageArray[i])
      const filePath = path.join(uploadsDir, imageArray[i]);
      zip.append(fs.createReadStream(filePath), { name: 'images/'+imageArray[i] });
    }

    res.attachment('download.zip');
    zip.pipe(res);
    // zip template directory
    console.log(templatesDir)
    zip.directory(templatesDir, false);
    zip.on('error', (err) => { throw err; });

    zip.finalize();

    return this;
  }

这种方法也应该有效