added env variable and java properties substitution in configuration attributes
All checks were successful
CI / build (push) Successful in 3m29s

This commit is contained in:
2025-01-16 21:11:35 +08:00
parent 3b7030c302
commit 241d95fe1c
14 changed files with 105 additions and 68 deletions

View File

@@ -6,8 +6,9 @@ plugins {
}
dependencies {
compileOnly project(':gbcs-api')
compileOnly catalog.slf4j.api
implementation project(':gbcs-api')
implementation catalog.slf4j.api
implementation catalog.jwo
}
publishing {

View File

@@ -3,6 +3,7 @@ module net.woggioni.gbcs.base {
requires java.logging;
requires org.slf4j;
requires kotlin.stdlib;
requires net.woggioni.jwo;
exports net.woggioni.gbcs.base;
}

View File

@@ -1,5 +1,9 @@
package net.woggioni.gbcs.base
import net.woggioni.jwo.CollectionUtils.mapValues
import net.woggioni.jwo.CollectionUtils.toUnmodifiableTreeMap
import net.woggioni.jwo.JWO
import net.woggioni.jwo.MapBuilder
import org.slf4j.LoggerFactory
import org.slf4j.event.Level
import org.w3c.dom.Document
@@ -12,6 +16,8 @@ import org.xml.sax.SAXParseException
import java.io.InputStream
import java.io.OutputStream
import java.net.URL
import java.util.Collections
import java.util.TreeMap
import javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD
import javax.xml.XMLConstants.ACCESS_EXTERNAL_SCHEMA
import javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING
@@ -95,6 +101,22 @@ class Xml(val doc: Document, val element: Element) {
}
companion object {
private val dictMap: Map<String, Map<String, Any>> = sequenceOf(
"env" to System.getenv().asSequence().map { (k, v) -> k to (v as Any) }.toMap(),
"sys" to System.getProperties().asSequence().map { (k, v) -> k as String to (v as Any) }.toMap()
).toMap()
private fun renderConfigurationTemplate(template: String): String {
return JWO.renderTemplate(template, emptyMap(), dictMap)
}
fun Element.renderAttribute(name : String, namespaceURI: String? = null) = if(namespaceURI == null) {
getAttribute(name)
} else {
getAttributeNS(name, namespaceURI)
}.takeIf(String::isNotEmpty)?.let(Companion::renderConfigurationTemplate)
fun Element.asIterable() = Iterable { ElementIterator(this, null) }
fun NodeList.asIterable() = Iterable { NodeListIterator(this) }