11package at.ac.uibk.dps.cirrina.io.description
22
33import at.ac.uibk.dps.cirrina.csml.description.Csml
4- import org.pkl.config.java.ConfigEvaluator
4+ import java.net.URI
5+ import java.util.*
6+ import java.util.regex.Pattern
7+ import org.pkl.config.java.ConfigEvaluatorBuilder
58import org.pkl.core.ModuleSource
9+ import org.pkl.core.SecurityManager
10+ import org.pkl.core.module.ModuleKey
11+ import org.pkl.core.module.ModuleKeyFactory
12+ import org.pkl.core.module.ResolvedModuleKey
13+ import org.pkl.core.util.IoUtils
614
7- /* * CsmlParser, used to parse a Pkl file into a Csml object. */
15+ /* * CsmModuleKey, used to load CSM modules from the classpath. */
16+ class CsmModuleKey (private val uri : URI ) : ModuleKey, ResolvedModuleKey {
17+
18+ /* * Get the original module key. */
19+ override fun getOriginal (): ModuleKey = this
20+
21+ /* * Get the URI of the module. */
22+ override fun getUri (): URI = uri
23+
24+ /* * Load the source code of the module. */
25+ override fun loadSource (): String =
26+ IoUtils .readClassPathResourceAsString(javaClass, " /pkl/csm/${uri.schemeSpecificPart} .pkl" )
27+
28+ /* * Resolve the module. */
29+ override fun resolve (securityManager : SecurityManager ): ResolvedModuleKey {
30+ securityManager.checkResolveModule(uri)
31+ return this
32+ }
33+
34+ /* * Csm modules are always hierarchical. */
35+ override fun hasHierarchicalUris (): Boolean = true
36+
37+ /* * Csm modules cannot be globbed. */
38+ override fun isGlobbable (): Boolean = false
39+ }
40+
41+ /* * Factory for CsmModuleKey, converts csm: modules to CsmModuleKey. */
42+ class CsmModuleKeyFactory : ModuleKeyFactory {
43+ /* * Converts a URI into a CsmModuleKey if the URI scheme is csm. */
44+ override fun create (uri : URI ): Optional <ModuleKey > =
45+ Optional .ofNullable(
46+ uri.takeIf { it.scheme.equals(" csm" , ignoreCase = true ) }?.let { CsmModuleKey (it) }
47+ )
48+ }
49+
50+ /* * Csml parser, used to parse a Pkl file into a Csml object. */
851object CsmlParser {
952 /* *
1053 * Parse a Pkl module at a path. Returns an instance of Csml upon success. Any errors will result
@@ -16,7 +59,17 @@ object CsmlParser {
1659 */
1760 fun parse (mainModulePath : String ): Csml {
1861 try {
19- ConfigEvaluator .preconfigured().use { evaluator ->
62+ // Create a preconfigured ConfigEvaluatorBuilder
63+ val builder = ConfigEvaluatorBuilder .preconfigured()
64+
65+ // Mark the CSM module as an allowed module
66+ builder.allowedModules.add(Pattern .compile(" csm:.*" ))
67+
68+ // Add the CsmModuleKeyFactory to the builder
69+ builder.evaluatorBuilder.addModuleKeyFactory(CsmModuleKeyFactory ())
70+
71+ // Build and load the main module
72+ builder.build().use { evaluator ->
2073 return evaluator.evaluate(ModuleSource .file(mainModulePath)).`as `(Csml ::class .java)
2174 }
2275 } catch (e: Exception ) {
0 commit comments