2

LineFeed各行の後にフォーマットされたコンテンツを含む文字列があります。その変数の内容をフォーマットして、各行が80文字以下になるように制限したいと思います。

Groovyで誰かがこれを手伝ってくれる?

テスト目的で、コンテンツをファイルにコピーしました

String fileContents = new File('E://Projects//temp//license').text
println fileContents

fileContentsコンテンツまたはコンソール出力

List of connectivities are:
    Valid [Metadata Exchange for Microsoft Visio]
   Valid [Metadata Exchange for Microstrategy]
   Valid [Metadata Exchange for Microsoft SQL Server Reporting Services and Analysis Services]
   Valid [Metadata Exchange for Netezza]
   Valid [Metadata Exchange for Oracle]
   Valid [Metadata Exchange for Oracle BI Enterprise Edition]
   Valid [Metadata Exchange for Oracle Designer]

Command ran successfully

アップデート

これは、tim_yatesの回答後に使用しているものです

def es=lic.entrySet()
xml.licInfo() {
    int i=0
    es.each{
        if(!it.key.contains("failed with error"))
        {
            String val=new String(it.value)
            license(name:it.key,value:trimOutput(val),assignedTo:resultRows[i++])

        }
    }       
}

def trimOutput(text)
{

    text=text.tokenize( '\n' )*.toList()*.collate(90)*.collect { it.join() }.flatten().join( '\n' )
    text
}

しかし、それは私に次の例外を与えます

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.collate() is applicable for argument types: (java.lang.Integer) values: [90]
Possible solutions: clone(), collect(groovy.lang.Closure), collect(groovy.lang.Closure), clear(), clear(), clear()

その他の更新(println esのコンソール出力)

[license_all =Edition:          BAAC Standard
Software Version:  6.5
Distributed by:    ABC
Issued on:         2012-Feb-06
Validity period:   Non-Expiry
Serial number:     210502
Deployment level:  Production

List of supported platforms are:
   [All operating systems] is authorized for [100] logical CPUs
Number of authorized repository instances: 100
Number of authorized CAL usage count: 100

List of connectivities are:

   Valid [Metadata Exchange for Microsoft SQL Server Reporting Services and Analysis Services]
   Valid [Metadata Exchange for Netezza]
   Valid [Metadata Exchange for Oracle]
   Valid [Metadata Exchange for Oracle BI Enterprise Edition]
   Valid [Metadata Exchange for Oracle Designer]
   Valid [Metadata Exchange for Oracle Warehouse Builder]
   Valid [Metadata Exchange for Popkin System Architect]
   Valid [Metadata Exchange for SAP R/3]
   Valid [Metadata Exchange for Select SE]
   Valid [Metadata Exchange for Silverrun - RDM]
   Valid [Metadata Exchange for SQL Server]
   Valid [Metadata Exchange for Sybase ASE]
   Valid [Metadata Exchange for Sybase PowerDesigner]
   Valid [Metadata Exchange for Teradata]

Command ran successfully.
]
4

2 に答える 2

2

長さが80文字を超える行で何をしたいかに応じて、2つの異なる方法があります。

def text = '''List of connectivities are:
             |    Valid [Metadata Exchange for Microsoft Visio]
             |   Valid [Metadata Exchange for Microstrategy]
             |   Valid [Metadata Exchange for Microsoft SQL Server Reporting Services and Analysis Services]
             |   Valid [Metadata Exchange for Netezza]
             |   Valid [Metadata Exchange for Oracle]
             |   Valid [Metadata Exchange for Oracle BI Enterprise Edition]
             |   Valid [Metadata Exchange for Oracle Designer]
             |
             |Command ran successfully'''.stripMargin()

// Strip everything after 80 chars
println text.tokenize( '\n' )*.  // Split the lines based on newline character
             take( 80 ).         // Only take upto the first 80 chars of each String
             join( '\n' )        // And join them back together with '\n' between them

// Add newline if line is over 80 chars
println text.tokenize( '\n' )*.      // Split the lines based on newline character
             toList()*.              // Convert each String to a list of chars
             collate(80)*.           // Split these into multiple lists, 80 chars long
             collect { it.join() }.  // Join all of the chars back into strings
             flatten().              // Flatten the multiple lists of Strings into one
             join( '\n' )            // And join these strings back together with '\n' between them

編集

編集後、この作業を行います:

String trimOutput( String input, int width=90 ) {
  input.tokenize( '\n' )*.
        toList()*.
        collate( width )*.
        collect { it.join() }.
        flatten().
        join( '\n' )
}

xml.licInfo {
  lic.eachWithIndex { key, value, idx ->
    // Shouldn't this be 'value', not 'key'?
    if( !key.contains( 'failed with error' ) ) {
      license( name: key, assignedTo: idx, trimOutput( value ) )
    }
  }
}

現在のキーではなく、ライセンスマップの値で「エラーで失敗」をチェックするように変更する必要があると思いますが、確信が持てません)


Edit2

groovy 1.8.1で立ち往生している場合は、collate方法がないため、自分でロールする必要があります。

List.metaClass.collate = { size ->
  def rslt = delegate.inject( [ [] ] ) { ret, elem ->
    ( ret.last() << elem ).size() >= size ? ret << [] : ret
  }
  !rslt.last() ? rslt[ 0..-2 ] : rslt
}
于 2012-04-25T08:16:12.960 に答える
1

インジェクトを使用した別のソリューション:

String trimOutput( String input, int width=90 ) {
    input.tokenize('\n')*.inject('') { output, ch ->
        output.size() % (width + 1) ?
            (output + ch) :
            (output + '\n' + ch)
    }.join('\n')
}

または、注入と照合の組み合わせ:

String trimOutput( String input, int width=90 ) {
    input.tokenize('\n').inject([]) { lines, line ->
        lines + line.toList().collate(width)*.join()
    }.join('\n')
}
于 2012-04-25T16:06:14.860 に答える