Create dynamic xml parametrized block with groovy - groovy

I have a comma-separated list/array as input to the groovy script. based on the number of elements in the array, I need to create parameterized XML block like below.
eg if the input is CAT,DOG
I need to create an XML block like below
<v2:values>
<v2:item>CAT</v2:item>
<v2:item>DOG</v2:item>
</v2:values>
if the input is CAT,DOG,ROSE
I need output as
<v2:values>
<v2:item>CAT</v2:item>
<v2:item>DOG</v2:item>
<v2:item>ROSE</v2:item>
</v2:values>
if Input is empty I need output block as empty
I wrote a groovy script like this
if(input.contains(","))
{
String[] param_array = input1[0].split(',')
res="<v2:values>"
for (parameter in param_array)
{
after that, I am not sure how do I iterate over the array and put values for other XML tags. Is there any way to do that?

You can use StreamingMarkupBuilder to generate the XML into a String
It won't be pretty-printed, but it will be valid XML as requested:
import groovy.xml.*
def input = 'cat,mouse,dog'
String xml = new StreamingMarkupBuilder().bind {
'v2:values' {
input.split(',').collect { item ->
'v2:item'(item)
}
}
}
println xml
If you need it pretty printed, you will need to know where the schema for v2 is, as XmlUtil.serialize needs the xml to validate
Edit
If you need it pretty printed, you can switch to MarkupBuilder
import groovy.xml.*
def input = 'cat,mouse,dog'
def writer = new StringWriter()
new MarkupBuilder(writer).'v2:values' {
input.split(',').collect { item ->
'v2:item'(item)
}
}
println writer.toString()

Related

How to add custom import statements in the generated java file in xtext using jvmmodelInferrer?

I have written a grammar for my domain specific language in xtext and I am using jvmmodelInferrer to generate java code. I can generate fields and custom methods but how can I add custom import statements like 'import java.util.*' in the generated java file without the user having to explicitly write the import statement?
you dont generate import strings. you just use rich strings in a proper way and everything happens automatically
def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
for (greeting : element.greetings) {
acceptor.accept(greeting.toClass("demo." + greeting.name)) [
members += greeting.toMethod("demo", Void.TYPE.typeRef) [
body = '''
«JFrame» f = null;
«"java.util.List".typeRef("java.lang.String".typeRef)» l = null;
return;
'''
]
]
}
}

Using 'eachWithindex' groovy instead of For loops to read in json data

I'm having trouble with converting an existing FOR loop to use the 'eachWithIndex' groovy function.
I'm not sure how to go about doing this successfully, examples I've seen are quite straightforward on tutorials but I'm not sure how to make them work with groovy json files.
The current solution uses a For loop which reads in data from 3 different groovy files
Add_Page1_data = data.get("json_Page1")
Add_Page2_data = data.get("json_Page2")
Add_Page3_data = data.get("json_Page3")
for (int i=0; i< Add_Page1_data.size(); i++) {
execute(w, Add_Page1_data[i],
Add_Page2_data[i],
Add_Page3_data[i])
}
I then use the following code to populate data inside windows by accessing the page objects of the file before :
def execute(w, data1) {
def page1 = new PO_Add_Page1(wrapper: w)
page1.typeInSomeText(data1.sometext)
}
PageObject file looks like this:
class PO_Add_Page1 {
def typeInSomeText(val) {
wrapper.findWithLabel("Some Text . .").rightEditable().type(val)
}
Json file used contains multiple records like this:
{
"json_Page1": [
{
"sometext": "text1"
},
{
"sometext": "text2"
},
The json is fed in as the "data" which maps to elements on the page using the page object groovy file.
I wanted to be able to repeat the same functionality in a more groovy idiomatic way.
Any help would be much appreciated.

Collect closure in groovy

I am new to functional programming paradigm and hoping to learn the concepts using groovy. I have a json text containing a list of several person objects like the following:
{
"persons":[
{
"id":1234,
"lastname":"Smith",
"firstname":"John"
},
{
"id":1235,
"lastname":"Lee",
"firstname":"Tommy"
}
]
}
What I am trying to do store them in list or array of Person groovy class like the following:
class Person {
def id
String lastname
String firstname
}
I would like to do this using a closure. I tried something like:
def personsListJson= new JsonSlurper().parseText(personJsonText) //personJsonText is raw json string
persons = personsListJson.collect{
new Person(
id:it.id, firstname:it.firstname, lastname:it.lastname)
}
This didn't work. Does collect operations supposed to behave this way? If so then how do I write it?
Try
personsListJson.persons.collect {
new Person( id:it.id, firstname:it.firstname, lastname:it.lastname )
}
And as there is a 1:1 mapping between the json and the constructor parameters, you can simplify that to:
personsListJson.persons.collect {
new Person( it )
}
But I'd keep the first method, as if the Json got an extra value in it (maybe out of your control) then the second method would break
You can try it-
List<JSON> personsListJson = JSON.parse(personJsonText);
persons = personsListJson.collect{
new Person(id:it.id, firstname:it.firstname, lastname:it.lastname)
}

Groovy MarkupBuilder name conflict

I have this code:
String buildCatalog(Catalog catalog) {
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.catalog(xmlns:'http://www.sybrium.com/XMLSchema/NodeCatalog') {
'identity'() {
groupId(catalog.groupId)
artifactId(catalog.artifactId)
version(catalog.version)
}
}
return writer.toString();
}
It produces this xml:
<catalog xmlns='http://www.sybrium.com/XMLSchema/NodeCatalog'>
<groupId>sample.group</groupId>
<artifactId>sample-artifact</artifactId>
<version>1.0.0</version>
</catalog>
Notice that the "identity" tag is missing... I've tried everything in the world to get that node to appear. I'm ripping my hair out!
Thanks in advance.
There might be a better way, but one trick is to call invokeMethod directly:
String buildCatalog(Catalog catalog) {
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.catalog(xmlns:'http://www.sybrium.com/XMLSchema/NodeCatalog') {
delegate.invokeMethod('identity', [{
groupId(catalog.groupId)
artifactId(catalog.artifactId)
version(catalog.version)
}])
}
return writer.toString();
}
This is effectively what Groovy is doing behind the scenes. I couldn't get delegate.identity or owner.identity to work, which are the usual tricks.
Edit: I figured out what's going on.
Groovy adds a method with a signature of identity(Closure c) to every object.
This means that when you tried to dynamically invoke the identity element on the XML builder, while passing in a single closure argument, it was calling the identity() method, which is like calling delegate({...}) on the outer closure.
Using the invokeMethod trick forces Groovy to bypass the Meta Object Protocol and treat the method as a dynamic method, even though the identity method already exists on the MetaObject.
Knowing this, we can put together a better, more legible solution. All we have to do is change the signature of the method, like so:
String buildCatalog(Catalog catalog) {
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.catalog(xmlns:'http://www.sybrium.com/XMLSchema/NodeCatalog') {
// NOTE: LEAVE the empty map here to prevent calling the identity method!
identity([:]) {
groupId(catalog.groupId)
artifactId(catalog.artifactId)
version(catalog.version)
}
}
return writer.toString();
}
This is much more readable, it's clearer the intent, and the comment should (hopefully) prevent anyone from removing the "unnecessary" empty map.

External Content with Groovy BuilderSupport

I've built a custom builder in Groovy by extending BuilderSupport. It works well when configured like nearly every builder code sample out there:
def builder = new MyBuilder()
builder.foo {
"Some Entry" (property1:value1, property2: value2)
}
This, of course, works perfectly. The problem is that I don't want the information I'm building to be in the code. I want to have this information in a file somewhere that is read in and built into objects by the builder. I cannot figure out how to do this.
I can't even make this work by moving the simple entry around in the code.
This works:
def textClosure = { "Some Entry" (property1:value1, property2: value2) }
builder.foo(textClosure)
because textClosure is a closure.
If I do this:
def text = '"Some Entry" (property1:value1, property2: value2)'
def textClosure = { text }
builder.foo(textClosure)
the builder only gets called for the "foo" node. I've tried many variants of this, including passing the text block directly into the builder without wrapping it in a closure. They all yield the same result.
Is there some way I take a piece of arbitrary text and pass it into my builder so that it will be able to correctly parse and build it?
Your problem is that a String is not Groovy code. The way ConfigSlurper handles this is to compile the text into an instance of Script using GroovyClassLoader#parseClass. e.g.,
// create a Binding subclass that delegates to the builder
class MyBinding extends Binding {
def builder
Object getVariable(String name) {
return { Object... args -> builder.invokeMethod(name,args) }
}
}
// parse the script and run it against the builder
new File("foo.groovy").withInputStream { input ->
Script s = new GroovyClassLoader().parseClass(input).newInstance()
s.binding = new MyBinding(builder:builder)
s.run()
}
The subclass of Binding simply returns a closure for all variables that delegates the call to the builder. So assuming foo.groovy contains:
foo {
"Some Entry" (property1:value1, property2: value2)
}
It would be equivalent to your code above.
I think the problem you described is better solved with a slurper or parser.
See:
http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper
http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlParser
for XML based examples.
In your case. Given the XML file:
<foo>
<entry name='Some Entry' property1="value1" property2="value2"/>
</foo>
You could slurp it with:
def text = new File("test.xml").text
def foo = new XmlSlurper().parseText(text)
def allEntries = foo.entry
allEntries.each {
println it.#name
println it.#property1
println it.#property2
}
Originally, I wanted to be able to specify
"Some Entry" (property1:value1, property2: value2)
in an external file. I'm specifically trying to avoid XML and XML-like syntax to make these files easier for regular users to create and modify. My current solution uses ConfigSlurper and the file now looks like:
"Some Entry"
{
property1 = value1
property2 = value2
}
ConfigSlurper gives me a map like this:
["Some Entry":[property1:value1,property2:value2]]
It's pretty simple to use these values to create my objects, especially since I can just pass the property/value map into the constructor.

Resources