@@ -26,23 +26,30 @@ import java.io._
2626import java .nio .file .Path
2727import org .apache .daffodil .api ._
2828import scala .jdk .CollectionConverters ._
29+ import cats .effect .IO
30+ import cats .syntax .all ._
31+ import org .apache .daffodil .api .exceptions .ExternalVariableException
2932
3033object Support {
3134 /* Daffodil DataProcessor wrapper methods */
32- def dataProcessorWithDebugger (p : DataProcessor , debugger : Debugger , variables : Map [String , String ]): DataProcessor =
33- p.withDebugger(debugger)
34- .withExternalVariables(variables.asJava)
35- .withValidation(" daffodil" )
35+ def dataProcessorWithDebugger (
36+ p : DataProcessor ,
37+ debugger : Debugger ,
38+ variables : Map [String , String ]
39+ ): IO [DataProcessor ] = {
40+ val base = p.withDebugger(debugger)
3641
37- /* Daffodil infoset wrapper methods */
38- def getInputSourceDataInputStream (data : InputStream ): InputSourceDataInputStream =
39- Daffodil .newInputSourceDataInputStream(data)
40- def getInfosetOutputter (infosetFormat : String , stream : OutputStream ): InfosetOutputter =
41- infosetFormat match {
42- case " xml" => Daffodil .newXMLTextInfosetOutputter(stream, true )
43- case " json" => Daffodil .newJsonInfosetOutputter(stream, true )
44- case other => throw new IllegalArgumentException (s " unsupported infosetFormat: $other" )
45- }
42+ variables.toList
43+ .foldLeftM(base) { case (dp, (k, v)) =>
44+ IO (dp.withExternalVariables(Map (k -> v).asJava)).handleErrorWith {
45+ case e : ExternalVariableException =>
46+ IO .println(s " [WARNING] Skipping unknown external variable ' $k': ${e.getMessage}" ) *>
47+ IO .pure(dp)
48+ case e => IO .raiseError(e)
49+ }
50+ }
51+ .map(_.withValidation(" daffodil" ))
52+ }
4653
4754 /* Daffodil ProcessorFactory wrapper methods */
4855 def getProcessorFactory (
@@ -56,9 +63,21 @@ object Support {
5663 .withTunables(tunables.asJava)
5764 .compileFile(schema.toFile(), rootName.orNull, rootNamespace.orNull)
5865
66+ /* Method to convert java list of diagnostics to a sequence of diagnostics */
5967 /* Method to convert java list of diagnostics to a sequence of diagnostics */
6068 def parseDiagnosticList (
6169 dl : java.util.List [org.apache.daffodil.api.Diagnostic ]
6270 ): Seq [org.apache.daffodil.api.Diagnostic ] =
6371 dl.asScala.toSeq
72+
73+ /* Daffodil infoset wrapper methods */
74+ def getInputSourceDataInputStream (data : InputStream ): InputSourceDataInputStream =
75+ Daffodil .newInputSourceDataInputStream(data)
76+
77+ def getInfosetOutputter (infosetFormat : String , stream : OutputStream ): InfosetOutputter =
78+ infosetFormat match {
79+ case " xml" => Daffodil .newXMLTextInfosetOutputter(stream, true )
80+ case " json" => Daffodil .newJsonInfosetOutputter(stream, true )
81+ case other => throw new IllegalArgumentException (s " unsupported infosetFormat: $other" )
82+ }
6483}
0 commit comments