Skip to content

Commit 900166b

Browse files
committed
New sample for Apache Velocity
1 parent eeffa36 commit 900166b

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

kotlin-jvm/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ dependencies {
4242
implementation("io.konform:konform:_")
4343
implementation("it.skrape:skrapeit-core:_")
4444
implementation("it.skrape:skrapeit-http-fetcher:_")
45+
implementation("org.apache.velocity.tools:velocity-tools-generic:_")
46+
implementation("org.apache.velocity:velocity-engine-core:_")
4547
implementation("org.jetbrains.exposed:exposed-core:_")
4648
implementation("org.jetbrains.exposed:exposed-dao:_")
4749
implementation("org.jetbrains.exposed:exposed-java-time:_")
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
@file:Suppress("PackageDirectoryMismatch")
2+
3+
package playground.velocity
4+
5+
import org.apache.velocity.Template
6+
import org.apache.velocity.VelocityContext
7+
import org.apache.velocity.app.VelocityEngine
8+
import playground.shouldBe
9+
import java.io.StringWriter
10+
import java.util.*
11+
12+
/**
13+
* Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code.
14+
*
15+
* [Apache Velocity Engine - User Guide](https://velocity.apache.org/engine/2.3/user-guide.html)
16+
* [Apache Velocity Engine - Developer Guide](https://velocity.apache.org/engine/2.3/developer-guide.html)
17+
*/
18+
fun main() {
19+
println()
20+
println("# Velocity")
21+
22+
val engine = velocityEngine()
23+
helloWorld(engine)
24+
complexExample(engine)
25+
}
26+
27+
28+
fun helloWorld(engine: VelocityEngine) {
29+
val context = VelocityContext()
30+
context.put("name", "Velocity")
31+
val template: Template = engine.getTemplate("hello.vm")
32+
template.apply(context) shouldBe """
33+
<body>
34+
Hello Velocity World!
35+
</body>
36+
""".trimIndent()
37+
38+
}
39+
40+
41+
fun complexExample(engine: VelocityEngine) {
42+
val context = VelocityContext().apply {
43+
put("name", "Velocity")
44+
put("details", true)
45+
put("customer", VelocityCustomer("Jean-Michel", 39))
46+
put("groceries", listOf("apple", "milk"))
47+
}
48+
engine.getTemplate("complex.vm").apply(context) shouldBe """
49+
Hello Velocity
50+
I speak French
51+
Customer Jean-Michel is 41 years old.
52+
Is he old? true enough
53+
Groceries:
54+
- apple
55+
- milk
56+
""".trimIndent()
57+
}
58+
59+
data class VelocityCustomer(val name: String, var age: Int) {
60+
fun isOld() = age >= 40
61+
}
62+
63+
64+
private fun Template.apply(context: VelocityContext) : String {
65+
val sw = StringWriter()
66+
merge(context, sw)
67+
return sw.toString().trim()
68+
}
69+
70+
private fun velocityEngine(): VelocityEngine {
71+
val properties = Properties().also {
72+
it.setProperty(
73+
"resource.loader.file.path",
74+
"/Users/jmfayard/Documents/GitHub/kotlin-libraries-playground/kotlin-jvm/src/main/resources"
75+
)
76+
}
77+
val engine = VelocityEngine(properties)
78+
engine.init();
79+
return engine
80+
}

kotlin-jvm/src/main/kotlin/playground/_main.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ fun main() {
4141
playground.skrapeit.main()
4242
playground.sqldelight.main()
4343
playground.statemachine.main()
44+
playground.velocity.main()
4445
/**
4546
* Keep the list sorted to minimize merge conflicts on pull-requests!
4647
*/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#* @vtlvariable name="customer" type="playground.velocity.VelocityCustomer" *#
2+
#* @vtlvariable name="name" type="java.lang.String" *#
3+
## This is a comment
4+
#* this is
5+
a multi-line
6+
comment
7+
*#
8+
Hello $name
9+
#set($language = "French")
10+
#if($details)
11+
I speak $language
12+
#end
13+
#set($customer.Age = 41)
14+
Customer $customer.Name is $customer.Age years old.
15+
Is he old? #if($customer.isOld())true enough#{else}no way!#end
16+
Groceries:
17+
#foreach($grocery in $groceries)
18+
- $grocery
19+
#end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#set( $foo = "Velocity" )
2+
<body>
3+
Hello $foo World!
4+
</body>

versions.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ version.okhttp3=4.9.1
204204

205205
version.org.amshove.kluent..kluent=1.65
206206

207+
version.org.apache.velocity..velocity-engine-core=2.3
208+
209+
version.org.apache.velocity.tools..velocity-tools-generic=3.1
210+
207211
version.org.jetbrains..markdown-jvm=0.2.3
208212
## # available=0.2.4
209213

0 commit comments

Comments
 (0)