Groovy script to split a file line at ',' and create a new XML file - groovy

I am very new to Groovy and trying to figure my way out.
I am trying to write a groovy to split the lines of a file on encountering ',' and then write a if condition based on the first two characters of the line. After that I wanted to create a XML file using the different data in the file. This is how far I have reached.
def Message processData(Message message) {
//Body
def body = message.getBody(java.lang.String)as String;
def varStringWriter = new StringWriter();
def varXMLBuilder = new MarkupBuilder(varStringWriter);
String newItem ;
body.eachLine{
line -> newItem = line ;
String newItem1 = newItem.substring(0,2).trim();
String newItem2 = newItem.substring(3,11).trim();
varXMLBuilder.RECORD{
node1(newItem1);
node2(newItem2);
}
}
def xml = varStringWriter.toString();
xml="<RECORDS>"+xml+"</RECORDS>" ;
message.setBody(xml);
return message;
}
In the above code I tried to use offset but, since each of my file lines are of different length it wont work.
Please help me handle this issue.
Regards,
Nisha

Splitting on a character can be done like this:
data = 'axaratgxrgc,rxregxsergcs'
def lines = data.split(/,/)
assert lines[0] == 'axaratgxrgc'
assert lines[1] == 'rxregxsergcs'

Welcome, first of all, to groovy and Stack Overflow :)
You can use tokenize() to split a string, as shown bellow.
And yeah, don't worry about ; in groovy ;)
def Message processData(Message message) {
//Body
def body = message.getBody(java.lang.String) as String;
def varStringWriter = new StringWriter()
def varXMLBuilder = new MarkupBuilder(varStringWriter)
body.eachLine { line ->
def newItems = line.tokenize(',') // input is a list of chars that will split your string, usually better than .split()
String newItem1 = newItems.first() // looks like you want just two items
String newItem2 = newItems.last() // but you can use as an array as well newItems[0] and newItems[1]
varXMLBuilder.RECORD {
node1(newItem1)
node2(newItem2)
}
}
def xml = varStringWriter.toString()
xml="<RECORDS>${xml}</RECORDS>" // you can use ${} to add a variable inside a string
message.setBody(xml)
return message
}

Related

use jsonSlurper.parseText for text that has dash

How can I use jsonSlurper.parseText to parse "807-000" that has dash in it with groovy ?
You are generating the below string for parsing:
[807-000]
What I think you wanted is an json array containing a string:
["807-000"]
You could generate that json yourself:
def arr2 = "[" + arr.collect({ '"' + it + '"' }).join(",") + "]"
But why reinvent the wheel, when you can do it like this:
def arr2 = groovy.json.JsonOutput.toJson(arr)
It's not entirely clear what exactly do you want to do. parseText() is waiting for json to be input. I suggest several options for parsing.
def text = jsonSlurper.parseText("""{ "key": "807-000" } """)
Or did you mean that before the dash is the key, and after it is the value? If so then you can try this:
def map = "807-000".split("-").toSpreadMap()
map.each {row ->
def parsedText = jsonSlurper.parseText("""{ "${row.key}": "${row.value}" } """)
println(parsedText)
}
output is = [807:000]
How can I use jsonSlurper.parseText to parse "807-000" that has dash
in it with groovy ?
I am not sure what the challenge actually is. Something I can think of is possibly you are having trouble using Groovy property access to retrieve the value of a key when the key has a hyphen in it. You can do that by quoting the property name:
String jsonString = '''
{"807-000":"Eight O Seven"}
'''
def slurper = new JsonSlurper()
def json = slurper.parseText(jsonString)
// quote the property name which
// contains a hyphen...
String description = json.'807-000'
assert description == 'Eight O Seven'

How to use map in groovy

Here is how my script currently looks like -
baseList = readFile('text2.txt').split('\n') as List
def cleanList = []
for (i=0; i < baseList.size; i++) {
if (baseList[i].contains('=')){
cleanList += baseList[i]
}
}
print(cleanList)
This gives following output-
[Pipeline] echo
[version.build=874, version.maintenance=0, version.major=1, version.minor=37]
I want these values to go into another variable called "svnTag"
def svnTag="ccsmp_v_${version.major} ${version.minor} ${version.maintenance} ${version.build}"
So that when I print svnTag, it output something like this-
SVN_TAG=ccsmp_v_1.37.0.846
You are not using a Map, but a List of String, where each element is in the form of <key>=<value>
If you want to parse your file to a Map, you could use something like:
def baseList = readFile('text2.txt').split('\n') as List
def map = [:]
for (el in baseList) {
if (el.contains('=')) {
def parts = el.split('=')
map[parts[0]] = parts[1]
}
}
Then you can use the values from the map with:
def svnTag="ccsmp_v_${map['version.major']} ${map['version.minor']} ${map['version.maintenance']} ${map['version.build']}"
If your file is a valid property file (all lines are in form of key=value), you can use the readProperties step that will create a Properties object for you (that is in fact a Map):
def map = readProperties file: 'text2.txt'

How can I call a function on shell.parse() without first assigning to a variable? for example shell.parse().someFunction()

Right now I'm doing this:
//main.groovy
def func = shell.parse( new File('func.groovy') )
func.someMethod('sdfsdfsdfsdf')
//func.groovy
def someMethod(deploymentFolder) {
return deploymentFolder
}
I want to make the snippet in main.groovy a one-liner but this does not work:
def func = shell.parse( new File('func.groovy') ).someMethod('sdfsdfsdf')
and this doesn't work either:
def func = shell.parse( new File('func.groovy') ) someMethod('sdfsdfsdf')
Is there a way to call a function directly on what shell.parse returns like this?
Edit
I am calling this in a collect call which seems to change things
So this is not working:
list = arrList.collect { file ->
return shell.parse( new File(file) ).someMethod('sdfsdfsdf')
}
someMethod() returns an arrayList. after collect though list seems to contain the right number of nested lists but they are all null.
Yet doing this actually works:
myarr = []
list = arrList.collect { file ->
tempVar = shell.parse( new File(file) )
myarr += tempVar.someMethod('sdfsdfsdf')
}
I'm not sure what the difference is. I thought collect would do the same thing. It seems to almost do the same thing but the lists it concatenates are all null.
Your first attempt there is right and works as suspected:
def shell = new GroovyShell()
println(["func.groovy"].collect{ it as File }.collect{ shell.parse(it).someMethod('sdfsdfsdfsdf') })
// ⇒ [sdfsdfsdfsdf]

Binding Groovy TemplateEngine with any variable names

I am trying to write a method that:
Loads a template file (*.tpl) from the local file system
Parameterizes that template file with a list of supplied variables
Best attempt thus far:
String loadParameterizedTemplateByName(String templateName,
String... variables) {
InputStream inputStream =
this.class.classLoader.getResourceAsStream(templateName)
StringWriter writer = new StringWriter()
IOUtils.copy(inputStream, writer, 'UTF-8')
String templateBody = writer.toString()
def binding = variablesAsBinding(variables) // ?!?!
engine.createTemplate(templateBody).make(binding).toString()
}
So for instance say I have the following HelloEmail.tpl template file:
HelloEmail.tpl
==============
Hello ${firstName},
You are awesome! ${someGuy} even says so!
Sincerely,
${me}
Then my desired invocation of this would be:
String firstName = 'John'
String someGuy = 'Mark'
String me = '#smeeb'
String parameterizedTemplate =
loadParameterizedTemplateByName('HelloEmail.tpl', firstName, someGuy, me)
So that the final result is that parameterizedTemplate string has a value of:
println parameterizedTemplate
// Prints:
Hello John,
You are awesome! Mark even says so!
Sincerely,
#smeeb
The trick here is that the method needs to be able to use any list of supplied variables against any supplied template file!
Is this possible to accomplish via reflection? Meaning the TemplateEngine just looks as the supplied list of String variables, and substitutes them for variables of the same name (as found in the template)?
You can pass a Map like so:
import groovy.text.markup.MarkupTemplateEngine
import groovy.text.markup.TemplateConfiguration
String loadParameterizedTemplateByName(Map variables, String templateName) {
def engine = new groovy.text.SimpleTemplateEngine()
this.class.getResource(templateName).withReader { reader ->
engine.createTemplate(reader).make(variables)
}
}
def result = loadParameterizedTemplateByName('/mail.tpl', firstName:'Tim', someGuy:'StackOverflow', me:'smeeb')
assert result == 'Hello Tim,\n\nYou are awesome! StackOverflow even says so!\n\nSincerely,\nsmeeb'

Groovy load .csv files

How to read and import .csv file in groovy on grails. I have .csv file with data and
need to import in to db using user interface .
There are as always different possibilities to work with CSV files in Groovy.
As Groovy is fully interoperable with Java, you can use one of the existing CSV libararies, e.g. OpenCSV.
Depending on the complexity of the CSV file you are using, you can also use the standard file/string handling possibilities of Groovy:
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/mydb",
"user", "pswd", "com.mysql.jdbc.Driver")
def people = sql.dataSet("PERSON")
new File("users.csv").splitEachLine(",") {fields ->
people.add(
first_name: fields[0],
last_name: fields[1],
email: fields[2]
)
}
EDIT:
Kelly Robinson just wrote a nice blog post about the different possibilities that are available to work with CSV files in Groovy.
EDIT #2:
As Leonard Axelsson recently released version 1.0 of his GroovyCVS library, I thought I should definitely add this to the list of options.
Using xlson's GroovyCSV:
#Grab('com.xlson.groovycsv:groovycsv:1.3')
import static com.xlson.groovycsv.CsvParser.parseCsv
for(line in parseCsv(new FileReader('countries.csv'), separator: ';')) {
println "Country=$line.COUNTRY, Capital=$line.CAPITAL"
}
The field names are taken from the header of the CSV file.
If the CSV file has no header, you can specify the field names programmatically.
With Apache Commons-CSV
#Grab('org.apache.commons:commons-csv:1.2')
import org.apache.commons.csv.CSVParser
import static org.apache.commons.csv.CSVFormat.*
import java.nio.file.Paths
Paths.get('countryInfo.txt').withReader { reader ->
CSVParser csv = new CSVParser(reader, DEFAULT.withHeader())
for (record in csv.iterator()) {
println record.dump()
}
}
Commons-CSV has nice API and I recommend that.
With GroovyCSV:
#Grab('com.xlson.groovycsv:groovycsv:0.2')
import com.xlson.groovycsv.CsvParser
def csv = '''Name,Lastname
Mark,Andersson
Pete,Hansen'''
def data = new CsvParser().parse(csv)
for(line in data) {
println "$line.Name $line.Lastname"
}
(Taken from it's samples)
Last resort: Regular expression.
Here's how I parsed a file that might contain a quoted escaped string in it's fourth column:
File detailedStatsFile = new File("stats.csv");
detailedStatsFile.eachLine { line, number ->
// Number Of Executions, Total Milliseconds, Milliseconds per execution, "Type"
def match = line =~ /([^,]*?),\s*([^,]*?),\s*([^,]*?),\s*(?:([^",]+)|(?:"((?:[^\\"]++(?:\\")?)++)"))$/; //"
if (!match.matches())
continue;
def numberOfExecs = Integer.valueOf(match.group(1));
def totalMillis = Integer.valueOf(match.group(2));
def detailedStatName = match.group(4);
if (detailedStatName == null)
detailedStatName = match.group(5).replaceAll('\\"','"');
Example using opencsv
#Grab('com.opencsv:opencsv:4.0')
import com.opencsv.CSVReader
import com.opencsv.CSVWriter
class TestCsvReader {
static main(args) {
def csv = '''"a","b","c"
"d","e","f"
'''
def Reader csvFileReader = new StringReader(csv)
def Writer csvFileWriter = new PrintWriter(System.out)
def CSVReader reader = new CSVReader(csvFileReader)
def CSVWriter writer = new CSVWriter(csvFileWriter)
reader.iterator().each { fields ->
writer.writeNext(fields)
}
reader.close()
writer.close()
}
}
Example from production code done by SAP developers in SCPi SAP Cloud Platform Integration groovy iFlow:
String[] parseCSVLine(String line) {
// Create a pattern to match breaks
Pattern p =
Pattern.compile(",(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))");
// Split input with the pattern
String[] fields = p.split(line);
for (int i = 0; i < fields.length; i++) {
// Get rid of residual double quotes
fields[i] = fields[i].replace("\"", "");
}
return fields;
}
Usage:
cols = parseCSVLine(line)
value = cols[0]
value = cols[1]
I prefer a slight tweak on the accepted answer: zip the columns and values together, as opposed to indexing each one by number. The result is slightly shorter code.
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/mydb", "user", "pswd", "com.mysql.jdbc.Driver")
def people = sql.dataSet("PERSON")
def columns = ['first_name', 'last_name', 'email']
new File("users.csv").splitEachLine(",") {values ->
people.add([columns, values].transpose().collectEntries())
}

Resources