Robert
Robert

Reputation: 10963

Avoid new break line in groovy template

I have YAML file with my configuration name applications.yaml, this data will be my bindings:

applications:
- name: service1
  port: 8080
  path: /servier1
- name: service2
  port: 8081
  path: /service2

Then I have a template file applications.config:

<% applications.each { application ->  %>
ApplicationName: <%= application.name %>
<% } $ %>

And putting all together:

@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml

Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)

String template_content = new File('applications.config').text
def binding = [applications: data.applications]

def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
println template.toString()

The issues is now: the output of this process is:


ApplicationName: service1

ApplicationName: service2

But I want this:

ApplicationName: service1
ApplicationName: service2

I do not know why are those extra spaces there. I will like to remove those but I do not see how or when or what is putting those new or breaking lines.

Thank you.

Upvotes: 11

Views: 1807

Answers (3)

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

The only way to avoid the newlines is to fix your template like below

<% applications.each { application ->  %>ApplicationName: <%= application.name %>
<% } $ %>

Results

$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2

or handle the newlines in code like below

@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml

Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)

String template_content = new File('applications.config').text
def binding = [applications: data.applications]

def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
def output = template.toString().replaceAll(/^\n|\n$/, "").replaceAll(/\n{2,}/,"\n")
println output

Results

$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2

Upvotes: 0

Bhanuchander Udhayakumar
Bhanuchander Udhayakumar

Reputation: 1646

What I observed (from answer, comments and some practice) is : All new Lines, we created inside the file applications.config are considered as new line in the output. As daggett said it is a default thing.

So Here I just want to show the possible config file format, where can be applied some conditional logic as u asked and looks ok for me. ex : if()

application.config :

<% applications.each { application ->
 if (application.valid) {%>\
Type :<%=application.valid%>
ApplicationName:<%=application.name%>
Path:<%=application.path%>
Port:<%=application.port%>
<%} else{%>\
--------------------
Found invalid Application : <%= application.name %>
--------------------\
<%}}%>

application.yaml

applications:
- name: service1
  port: 8080
  path: /servier1
  valid: true
- name: service2
  port: 8081
  path: /service2
  valid: false

code.groovy

@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml

Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)

String template_content = new File('applications.config').text

def binding = [applications: data.applications]
def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
println template.toString()

Output :

Type :true
ApplicationName:service1
Path:/servier1
Port:8080
--------------------
Found invalid Application : service2
--------------------

Upvotes: 5

daggett
daggett

Reputation: 28634

look at your template for example from notepad++ (JSP language):

enter image description here

at the end of first line <% applications.each { application -> %> there is a CRLF that will be printed out before each Application

and in the next line another CRLF that will be printed after each Application.

so you have two CRLF between each two apps

Upvotes: 2

Related Questions