@@ -26,23 +26,26 @@ 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 (p : DataProcessor , debugger : Debugger , variables : Map [String , String ]): IO [DataProcessor ] = {
36+ val base = p.withDebugger(debugger)
3637
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 " )
38+ variables.toList
39+ .foldLeftM(base) { case (dp, (k, v)) =>
40+ IO (dp.withExternalVariables( Map (k -> v).asJava)).handleErrorWith {
41+ case e : ExternalVariableException =>
42+ IO .println( s " [WARNING] Skipping unknown external variable ' $k ': ${e.getMessage} " ) *>
43+ IO .pure(dp )
44+ case e => IO .raiseError(e )
45+ }
4546 }
47+ .map(_.withValidation(" daffodil" ))
48+ }
4649
4750 /* Daffodil ProcessorFactory wrapper methods */
4851 def getProcessorFactory (
@@ -56,9 +59,21 @@ object Support {
5659 .withTunables(tunables.asJava)
5760 .compileFile(schema.toFile(), rootName.orNull, rootNamespace.orNull)
5861
62+ /* Method to convert java list of diagnostics to a sequence of diagnostics */
5963 /* Method to convert java list of diagnostics to a sequence of diagnostics */
6064 def parseDiagnosticList (
6165 dl : java.util.List [org.apache.daffodil.api.Diagnostic ]
6266 ): Seq [org.apache.daffodil.api.Diagnostic ] =
6367 dl.asScala.toSeq
68+
69+ /* Daffodil infoset wrapper methods */
70+ def getInputSourceDataInputStream (data : InputStream ): InputSourceDataInputStream =
71+ Daffodil .newInputSourceDataInputStream(data)
72+
73+ def getInfosetOutputter (infosetFormat : String , stream : OutputStream ): InfosetOutputter =
74+ infosetFormat match {
75+ case " xml" => Daffodil .newXMLTextInfosetOutputter(stream, true )
76+ case " json" => Daffodil .newJsonInfosetOutputter(stream, true )
77+ case other => throw new IllegalArgumentException (s " unsupported infosetFormat: $other" )
78+ }
6479}
0 commit comments