stevedlawrence commented on code in PR #878:
URL: https://github.com/apache/daffodil/pull/878#discussion_r1033959326
##########
daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala:
##########
@@ -1236,8 +1266,77 @@ object Main {
}
}
- // Required to avoid "match may not be exhaustive", but should never
happen
- case _ => Assert.impossible()
+ case Some(conf.exi) => {
+ var rc = ExitCode.Success
+ val exiOpts = conf.exi
+ val channel = exiOpts.output.toOption match {
+ case Some("-") | None => Channels.newChannel(STDOUT)
+ case Some(file) => new FileOutputStream(file).getChannel()
+ }
+ val output = Channels.newOutputStream(channel)
+
+ val inputStream = exiOpts.infile.toOption match {
+ case Some("-") | None => STDIN
+ case Some(file) => {
+ val f = new File(file)
+ new FileInputStream(f)
+ }
+ }
+ val input = new InputSource(inputStream)
+
+ val exiFactory = DefaultEXIFactory.newInstance
+ if (exiOpts.schema.isDefined) try {
+ val grammarFactory = GrammarFactory.newInstance
+ val grammar =
grammarFactory.createGrammars(exiOpts.schema.toOption.get.toString,
DFDLCatalogResolver.get)
+ exiFactory.setGrammars(grammar)
Review Comment:
It's only a few lines, but I'd like to avoid this duplication of this
similar logic in the InfosetHandler. Can we put this logic in a function that
can be shared, maybe in he `EXIInfoseHandler` or some other object? E.g.
```scala
object EXIInfosetHandler {
...
def createEXIFactory(optSchema: Option[URI]): EXIFactory = {
val exiFactory = DefaultEXIFactory.newInstance
if (optSchema.isDefined)
val grammarFactory = GrammarFactory.newInstance
val grammar = grammarFactory.createGrammars(optSchema.get.toString,
DFDLCatalogResolver.get)
exiFactory.setGrammars(grammar)
}
exiFactory
}
}
```
Then the `apply` methods in the `EXIInfosetHandler` look something like
```scala
/** non-schema aware EXI **/
def apply(dataProcessor: DataProcessor): InfosetHandler = {
EXIInfosetHandler(dataProcessor, createExiFactory(None))
}
/** schema aware EXI **/
def apply(dataProcessor: DataProcessor, schemaUri: URI): InfosetHandler = {
EXIInfosetHandler(dataProcessor, createExiFactory(Some(schemaUri))
}
```
And then this new code becomes something like this:
```scala
val exiFactory = try {
EXIInfosetHandler.createEXIFactory(exiOpts.schema.toOption)
} catch {
...
}
```
This ensures we only have a single place where we deal with EXI factory
creation logic.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]